Quote:Original post by Buckeye
In your Integrate function, you set the shape's centre according to force_Accumulated. Where/how is force_Accumulated set?
Are you talking about these lines in the Integrate() method?
// Update the shape shape.Position = position; shape.Rotation = rotation;
The force_Accumulated is only set inside the rigid body class via the following methods:
public void AddForce(Vector2 force) { // Accumulation stage must take place before the particle is integrated [Integrate()] force_Accumulated += force; } public void AddForce(Vector2 force, Vector2 point) { isAwake = true; // Accumulation stage must take place before the particle is integrated [Integrate()] force_Accumulated += force; point -= position; // Position is always at centre of mass //torque_Accumulated += force.Cross(point); }
Currently only gravity and drag are affecting the rigid body:
class Particle2D_Gravity : IRigidBody2D_ForceGenerator { Vector2 gravity; // Holds the acceleration due to gravity // Create the generator with the given acceleration public Particle2D_Gravity(Vector2 gravity) { this.gravity = gravity; } // Applies the gravitational force to the given particle public override void UpdateForce(RigidBody2D body, float duration) { if (body.IgnoreGravity || !body.HasFiniteMass) { return; } // Apply the mass-scaled force to the particle body.AddForce(gravity * body.Mass); } } class Particle2D_Drag : IRigidBody2D_ForceGenerator { float k1; // Holds the velocity drag coefficient float k2; // Holds the velocity squared drag coefficient public Particle2D_Drag(float k1, float k2) { this.k1 = k1; this.k2 = k2; } // Applies the drag force to the given particle public override void UpdateForce(RigidBody2D body, float duration) { if (!body.HasFiniteMass) { return; } Vector2 force = body.Velocity; // Calculate the total drag coefficient float drag_Coefficient = force.Length(); drag_Coefficient = k1 * drag_Coefficient + k2 * drag_Coefficient * drag_Coefficient; // Calculate the final force and apply it if (drag_Coefficient != 0) { // Force must be normalised for drag equation to be correct force.Normalize(); } force *= -drag_Coefficient; body.AddForce(force); } }
The acceleration of the body is as follows:
// Update acceleration from accumulated forces // F = M * A // A = F / M Vector2 acceleration = force_Accumulated * mass_Inverse;
Thanks for coming to my hair's rescue Buckeye. I've been tearing it out for most of the day trying to figure this one out!