stable collisions
This commit is contained in:
@@ -5,20 +5,17 @@ namespace PhysicsEngine
|
||||
{
|
||||
public class Solver
|
||||
{
|
||||
// Optional: set a default number of sub‑steps (e.g. 8 for better stability)
|
||||
private const int DefaultSubSteps = 8;
|
||||
|
||||
public void Step(float dt, List<Body> bodies, List<Constraint> constraints, List<GlobalForce> globalForces, int subSteps = DefaultSubSteps)
|
||||
{
|
||||
// Avoid division by zero or negative subSteps
|
||||
if (subSteps <= 0) subSteps = 1;
|
||||
|
||||
float subDt = dt / subSteps;
|
||||
|
||||
for (int step = 0; step < subSteps; step++)
|
||||
{
|
||||
// 1. Accumulate global forces (gravity, drag, etc.)
|
||||
// They are applied each sub‑step using the sub‑step dt.
|
||||
// accumulate global forces
|
||||
foreach (var body in bodies)
|
||||
{
|
||||
if (body.IsStatic) continue;
|
||||
@@ -29,29 +26,29 @@ namespace PhysicsEngine
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Numerical Integration (Velocity & Position updates)
|
||||
// velocity & position updates
|
||||
foreach (var body in bodies)
|
||||
{
|
||||
if (body.IsStatic) continue;
|
||||
|
||||
// Linear motion
|
||||
// linear motion
|
||||
Vector2 acceleration = body.ForceAccumulator * body.InverseMass;
|
||||
body.Velocity += acceleration * subDt;
|
||||
body.Position += body.Velocity * subDt;
|
||||
|
||||
// Angular motion
|
||||
// angular motion
|
||||
float angularAcceleration = body.TorqueAccumulator * body.InverseInertia;
|
||||
body.AngularVelocity += angularAcceleration * subDt;
|
||||
body.Rotation += body.AngularVelocity * subDt;
|
||||
|
||||
// Clear accumulators for the next sub‑step
|
||||
// clear
|
||||
body.ClearForces();
|
||||
}
|
||||
|
||||
// 3. Resolve Collisions
|
||||
// collisions
|
||||
CollisionEngine.ResolveCollisions(bodies, subDt);
|
||||
|
||||
// 4. Resolve constraints
|
||||
// constraints
|
||||
foreach (var constraint in constraints)
|
||||
{
|
||||
constraint.Solve();
|
||||
|
||||
Reference in New Issue
Block a user