Energy based changes

This commit is contained in:
max
2025-12-18 02:30:17 +01:00
parent c22452c66c
commit fa68f73055
5 changed files with 220 additions and 295 deletions

View File

@@ -4,27 +4,43 @@
{
// Physical properties
public float Radius { get; set; } = 0.3f; // meters
public float Inertia { get; set; } = 2.0f; // kg·m² per wheel
public float WheelInertia { get; set; } = 2.0f; // kg·m² per wheel
public float CarMass { get; set; } = 1500f; // kg - Car mass integrated into wheel system
public int WheelCount { get; set; } = 4;
public int DrivenWheels { get; set; } = 2; // 2WD
// State
public float WheelEnergy { get; set; } = 0f; // Joules
public float TotalEnergy { get; set; } = 0f; // Joules (rotational + translational)
public float AngularVelocity => GetOmega();
public float RPM => GetRPM();
public float Speed => GetSpeed();
public float CarSpeed => GetCarSpeed(); // Now returns actual car speed
public float ResistanceTorque { get; set; } = 0f;
// Calculations
public float GetTotalRotationalInertia()
{
return WheelInertia * WheelCount;
}
public float GetEquivalentCarInertia()
{
// Convert car mass to equivalent rotational inertia at wheels
// I = m * r² (from v = ω * r, so KE_translational = 0.5 * m * v² = 0.5 * m * (ωr)² = 0.5 * m * r² * ω²)
return CarMass * Radius * Radius;
}
public float GetTotalInertia()
{
return Inertia * WheelCount;
// Total inertia = rotational inertia of wheels + equivalent inertia of car mass
return GetTotalRotationalInertia() + GetEquivalentCarInertia();
}
public float GetOmega()
{
if (WheelEnergy <= 0 || GetTotalInertia() <= 0) return 0f;
return MathF.Sqrt(2f * WheelEnergy / GetTotalInertia());
if (TotalEnergy <= 0 || GetTotalInertia() <= 0) return 0f;
return MathF.Sqrt(2f * TotalEnergy / GetTotalInertia());
}
public float GetRPM()
@@ -32,27 +48,46 @@
return AngularVelocity * PhysicsUtil.RAD_PER_SEC_TO_RPM;
}
public float GetSpeed()
public float GetCarSpeed()
{
// v = ω * r (no slip assumed for base calculation)
return AngularVelocity * Radius;
}
public float GetRotationalEnergy()
{
// Just the energy from wheel rotation
float omega = GetOmega();
return 0.5f * GetTotalRotationalInertia() * omega * omega;
}
public float GetTranslationalEnergy()
{
// Just the energy from car motion
float speed = GetCarSpeed();
return 0.5f * CarMass * speed * speed;
}
public float GetEnergyFromSpeed(float speed)
{
// Calculate total energy for given car speed
// Total energy = rotational energy of wheels + translational energy of car
float omega = speed / Radius;
return 0.5f * GetTotalInertia() * omega * omega;
float rotationalEnergy = 0.5f * GetTotalRotationalInertia() * omega * omega;
float translationalEnergy = 0.5f * CarMass * speed * speed;
return rotationalEnergy + translationalEnergy;
}
public void SetSpeed(float speed)
{
WheelEnergy = GetEnergyFromSpeed(speed);
TotalEnergy = GetEnergyFromSpeed(speed);
}
// Apply work to the wheels
// Apply work to the entire system (wheels + car)
public void ApplyWork(float work)
{
WheelEnergy += work;
WheelEnergy = Math.Max(WheelEnergy, 0);
TotalEnergy += work;
TotalEnergy = Math.Max(TotalEnergy, 0);
}
public void ApplyTorque(float torque, float deltaTime)
@@ -70,8 +105,7 @@
if (MathF.Abs(omega) < 0.1f)
{
// Check if we have enough torque to overcome static friction
// For now, just return without applying resistance to allow startup
// Static friction - return without applying resistance to allow startup
return;
}
@@ -86,7 +120,7 @@
}
float energyNew = 0.5f * GetTotalInertia() * omegaNew * omegaNew;
WheelEnergy = Math.Max(energyNew, 0);
TotalEnergy = Math.Max(energyNew, 0);
}
}
}