major rework
This commit is contained in:
30
Car simulation/Core/Physics/PhysicsUtil.cs
Normal file
30
Car simulation/Core/Physics/PhysicsUtil.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
namespace Car_simulation.Core.Physics
|
||||
{
|
||||
public static class PhysicsUtil
|
||||
{
|
||||
public const float G = 9.81f;
|
||||
public const float AirDensity = 1.225f;
|
||||
public const float RAD_PER_SEC_TO_RPM = 60f / (2f * MathF.PI);
|
||||
public const float RPM_TO_RAD_PER_SEC = (2f * MathF.PI) / 60f;
|
||||
|
||||
public static float Lerp(float a, float b, float t)
|
||||
{
|
||||
t = Math.Clamp(t, 0f, 1f);
|
||||
return a + (b - a) * t;
|
||||
}
|
||||
|
||||
public static float RPMToOmega(float rpm) => rpm * MathF.PI * 2f / 60f;
|
||||
public static float OmegaToRPM(float omega) => omega * 60f / (2f * MathF.PI);
|
||||
|
||||
public static float CalculateRotationalEnergy(float inertia, float omega)
|
||||
{
|
||||
return 0.5f * inertia * omega * omega;
|
||||
}
|
||||
|
||||
public static float CalculateOmegaFromEnergy(float energy, float inertia)
|
||||
{
|
||||
if (energy <= 0) return 0;
|
||||
return MathF.Sqrt(2f * energy / inertia);
|
||||
}
|
||||
}
|
||||
}
|
||||
25
Car simulation/Core/Physics/ResistanceCalculator.cs
Normal file
25
Car simulation/Core/Physics/ResistanceCalculator.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using Car_simulation.Core.Components;
|
||||
|
||||
namespace Car_simulation.Core.Physics
|
||||
{
|
||||
public class ResistanceCalculator
|
||||
{
|
||||
public float CalculateDragForce(float speed, float dragCoefficient, float frontalArea)
|
||||
{
|
||||
return 0.5f * PhysicsUtil.AirDensity * dragCoefficient * frontalArea * speed * speed;
|
||||
}
|
||||
|
||||
public float CalculateRollingResistanceForce(float mass, float rollingResistanceCoefficient)
|
||||
{
|
||||
return rollingResistanceCoefficient * mass * PhysicsUtil.G;
|
||||
}
|
||||
|
||||
public float CalculateTotalResistanceForce(float speed, float mass,
|
||||
float dragCoefficient, float frontalArea, float rollingResistanceCoefficient)
|
||||
{
|
||||
float dragForce = CalculateDragForce(speed, dragCoefficient, frontalArea);
|
||||
float rollingForce = CalculateRollingResistanceForce(mass, rollingResistanceCoefficient);
|
||||
return dragForce + rollingForce;
|
||||
}
|
||||
}
|
||||
}
|
||||
38
Car simulation/Core/Physics/Vector2.cs
Normal file
38
Car simulation/Core/Physics/Vector2.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
namespace Car_simulation.Core.Physics
|
||||
{
|
||||
public struct Vector2
|
||||
{
|
||||
public float X, Y;
|
||||
|
||||
public Vector2(float x, float y) { X = x; Y = y; }
|
||||
|
||||
public float Length => MathF.Sqrt(X * X + Y * Y);
|
||||
public float LengthSquared => X * X + Y * Y;
|
||||
|
||||
public Vector2 Normalized()
|
||||
{
|
||||
float length = Length;
|
||||
if (length > 0.0001f)
|
||||
return new Vector2(X / length, Y / length);
|
||||
return new Vector2(0, 0);
|
||||
}
|
||||
|
||||
public void Normalize()
|
||||
{
|
||||
float length = Length;
|
||||
if (length > 0.0001f)
|
||||
{
|
||||
X /= length;
|
||||
Y /= length;
|
||||
}
|
||||
}
|
||||
|
||||
public static Vector2 Normalize(Vector2 v) => v.Normalized();
|
||||
|
||||
public static Vector2 operator *(Vector2 v, float s) => new Vector2(v.X * s, v.Y * s);
|
||||
public static Vector2 operator *(float s, Vector2 v) => new Vector2(v.X * s, v.Y * s);
|
||||
public static Vector2 operator /(Vector2 v, float s) => new Vector2(v.X / s, v.Y / s);
|
||||
public static Vector2 operator +(Vector2 a, Vector2 b) => new Vector2(a.X + b.X, a.Y + b.Y);
|
||||
public static Vector2 operator -(Vector2 a, Vector2 b) => new Vector2(a.X - b.X, a.Y - b.Y);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user