Updated RigidBody to support rotation & angular momentum
by Russell Bishop · in Torque X 2D · 04/19/2008 (10:08 pm) · 3 replies
Main thread, see last few posts:
www.garagegames.com/mg/forums/result.thread.php?qt=73905
I've updated RigidBody to properly support moments of inertia and angular momentum, meaning the rotational component of applied impulses is properly calculated.
The basic problem is that spheres seem to behave correctly, but boxes want to rest on their edges or a corner. Sometimes they will rest flat on a face (like you expect) but usually not. Boxes that aren't square are worse, tending to roll wildly and even clip through the terrain falling to infinity.
I'm posting my changes because I need a second opinion. I'm not a physics expert, so please let me know if you find a fix!
First, in T3DRigidComponent.cs replace the RotationScale property with this code:
Now download this zip file:
www.boneville.net/torque/rigid_torquex_patch.zip
It replaces RigidBody.cs and RigidState.cs
Now load up the FPS demo and select the Physics level. You'll see the issue with the boxes.
www.garagegames.com/mg/forums/result.thread.php?qt=73905
I've updated RigidBody to properly support moments of inertia and angular momentum, meaning the rotational component of applied impulses is properly calculated.
The basic problem is that spheres seem to behave correctly, but boxes want to rest on their edges or a corner. Sometimes they will rest flat on a face (like you expect) but usually not. Boxes that aren't square are worse, tending to roll wildly and even clip through the terrain falling to infinity.
I'm posting my changes because I need a second opinion. I'm not a physics expert, so please let me know if you find a fix!
First, in T3DRigidComponent.cs replace the RotationScale property with this code:
[TorqueCloneIgnore]
// we actually are cloning this, but the debug clone checker doesn't think we are
public float RotationScale
{
get
{
return rotationScale;
} // todo
set
{
rotationScale = value;
} // just let us null it out
}
private float rotationScale = 0.0f;
protected internal override void _PostRegister()
{
if (rotationScale == 0.0f)
{
_rigidBody.SetNullTensor();
}
else
{
//NOTE: kinda hacky right now, but works well enough
if (this.CollisionBody != null && this.CollisionBody.CollisionShapes.Count > 0)
{
if (this.CollisionBody.CollisionShapes[0] is CollisionSphereShape)
{
_rigidBody.SetObjectInertia(); //unit sphere
}
else
{
Box3F box = this.CollisionBody.Bounds;
Vector3 boxSize = box.Max - box.Min;
_rigidBody.SetObjectInertia(ref boxSize);
}
}
else
{
_rigidBody.SetObjectInertia();
}
}
base._PostRegister();
}Now download this zip file:
www.boneville.net/torque/rigid_torquex_patch.zip
It replaces RigidBody.cs and RigidState.cs
Now load up the FPS demo and select the Physics level. You'll see the issue with the boxes.
#2
If anyone from the TorqueX team wants to integrate my changes into the next release, please feel free to do so, the code is based on the rigid body in TGEA.
I finally got vehicles driving around too. It has been a good week.
edit: Please change RigidBody.cs, the IntegrateMomentum(float dt) method. It is missing some parenthesis that cause the momentum drag to be calculated incorrectly (much higher than it should be).
04/24/2008 (3:20 pm)
I apologize, I forgot to update the link in my second post. Please download the v2 version and you'll see that everything works correctly now. If anyone from the TorqueX team wants to integrate my changes into the next release, please feel free to do so, the code is based on the rigid body in TGEA.
I finally got vehicles driving around too. It has been a good week.
edit: Please change RigidBody.cs, the IntegrateMomentum(float dt) method. It is missing some parenthesis that cause the momentum drag to be calculated incorrectly (much higher than it should be).
_state.LinearMomentum += (_state.LinearMomentum * -_property.LinearDrag + ExternalForce) * dt;
_state.AngularMomentum += (_state.AngularMomentum * -_property.AngularDrag + ExternalTorque) * dt;
_UpdateVelocity();
#3
John K.
05/04/2008 (10:50 am)
This really looks great Russell! I've tested the code and checked it into the main branch. This should end up in the next point-release.John K.
Torque Owner Russell Bishop
Default Studio Name
www.boneville.net/torque/rigid_torquex_patch_v2.zip
Try the physics demo now and you'll see the boxes fall and rotate correctly