I'm creating a simple game with space ships made of blocks that are all the same size. After having looked at this: Calculating the rotational force of a 2D sprite I have created the following code for applying a force
public void AddForceAtBlock(Vector2 force, Block block)
{
float mass = 1F*blocks.Count;
this.velocity.X += force.X/mass;
this.velocity.Y += force.Y/mass;
Vector2 momentArm = GetGameWorldCoordinates(block) - centerOfMass;
Vector2 parallelComponent = momentArm * (Vector2.Dot(force, momentArm) / Vector2.Dot(momentArm, momentArm));
Vector2 angularForce = force - parallelComponent;
Vector2 torque = angularForce * momentArm.Length();
float momentOfInertia = 10000F*blocks.Count;
float angularAcceleration = torque.Length()/momentOfInertia;
this.rotationalVelocity += angularAcceleration;
}
However this code is unable to create negative rotational acceleration so I think I did it wrong. Will someone point out to me what's wrong here?
All blocks are 50 units wide, this is the output for a 3 block ship like so: TST where T are thrusters and S is a dull forceless block. Floats are slightly imprecise as is shown in the debug log but it shouldn't matter for videogames.
Right Thruster Stop 1: force = (0, 0.951147) momentArm = (50,0) parallelComponent = (0,0) torque= (0, 47.55) angularAcceleration = 0.001585 centerOfMass = (0,0)
Left Thruster Stop 2: force = (0, 0.951147) momentArm = (-50, 0) parallelComponent = (-8.315191E-08, 7.269371E-15) torque = (4.157595E-06, 47.55735) angularAcceleration = 0.001585245 centerOfMass = (0,0)
As you can see the equation seems to be working but it fails to make the right thruster's angular acceleration negative.