#### Archived

This topic is now archived and is closed to further replies.

# DirectInput to Torque (c#)

This topic is 5365 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I''m trying to take some direct input an convert it to torque acting on a rigid body. I''m pretty sure my physics update method is correct, based on other threads I read here:
		public override void UpdatePhysics(float dt)
{
Vector3 accel,aAccel;
Matrix rotation;

accel = netForce * (1/mass);

aAccel = Vector3.TransformCoordinate(netTorque, InvInertia);

currentPosition += currentVelocity * dt + accel * dt*dt*0.5f;

currentAngularVelocity.X = currentAngularVelocity.X < 0 ? Math.Max(currentAngularVelocity.X, -maxRotationalVelocity[0]) : Math.Min(currentAngularVelocity.X, maxRotationalVelocity[0]);
currentAngularVelocity.Y = currentAngularVelocity.Y < 0 ? Math.Max(currentAngularVelocity.Y, -maxRotationalVelocity[1]) : Math.Min(currentAngularVelocity.Y, maxRotationalVelocity[1]);
currentAngularVelocity.Z = currentAngularVelocity.Z < 0 ? Math.Max(currentAngularVelocity.Z, -maxRotationalVelocity[2]) : Math.Min(currentAngularVelocity.Z, maxRotationalVelocity[2]);

currentOrientation += Quaternion.RotationYawPitchRoll(currentAngularVelocity.X * 0.5f * dt, currentAngularVelocity.Y * 0.5f * dt, currentAngularVelocity.Z * 0.5f * dt) * currentOrientation;
currentOrientation.Normalize();

currentVelocity += accel * dt;

rotation = Matrix.RotationQuaternion(currentOrientation);
invInertia = rotation * Matrix.Invert(inertiaTensor) * Matrix.Invert(rotation);

Runtime.Listener.WriteLine(String.Format("Throttle: {9}; Position: {0} {1} {2}; Velocity: {3} {4} {5} km/s; AngularVelocity: {6} {7} {8}", currentPosition.X, currentPosition.Y, currentPosition.Z, currentVelocity.X, currentVelocity.Y, currentVelocity.Z, currentAngularVelocity.X, currentAngularVelocity.Y, currentAngularVelocity.Z, CurrentThrottle), Runtime.Listener.ListenLevel.Screen);

netTorque = netForce = Vector3.Empty;
}

To move the ship about a particular axis, first I loop through each engine, which has a function... pitch, roll, forward, etc:
		public void Pitch(int inputState)
{
foreach (Engine e in engines)
{
if (e.direction == EngineDirection.Pitch)
{
float force;
force  = e.maxThrust * inputState/(float)ActionFormat.AxisMax; // inputState is a number between -ActionFormat.AxisMax and ActionFormat.AxisMax

netTorque.Add(Vector3.Cross(e.offset, new Vector3(0, force, 0))); // I assume the force vector to pitch the ship is going up / down the y axis

}
}
}

I was always a crappy math/physics student unfortunately... any ideas? =P

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633294
• Total Posts
3011242
• ### Who's Online (See full list)

There are no registered users currently online

×