Constraining rigid body to world axis?

Oct 15, 2009 at 1:20 AM
Edited Oct 15, 2009 at 1:21 AM

Hey, thanks for this awesomely generous contribution to the XNA dev world! I'm having a blast implementing it and I'm learning a lot by reading through the code.

One thing I'm having some trouble with is figuring out how to constrain a rigid body to a world axis. I need to have a rigid body travel forward and back on a fixed world axis as well as rotate around it, using the ApplyTorque and ApplyForce methods, and still have it react to gravity and collisions, but like it's constrained to the axis. Is there a simple way to do this that I'm overlooking, or would I have to derive my own Constraint class?

Thanks for any help!

Coordinator
Oct 17, 2009 at 8:31 PM

Hey Vaaaan, thanks for the kind words.

I would definitely go the derived Constrant class route.  Are you wanting a rigid body to be able to slide on an axis?  If so, a distance constraint and a rotation constraint will get you where you need to go.

A distance constraint will apply a linear impulse to the rigid body to move it back to the axis.  A rotation constraint will apply a torque/angular impulse to line it up with the axis.  I think that will get you where you want.  If you need help with these let me know.  The math can get a bit hairy.  You may be able to base some of your equations on the PointToPointConstraint constraint.

Oct 17, 2009 at 10:41 PM

Yes, I want the rigid body to slide on and rotate around the same axis (so 2 degrees of freedom).

The math in the PointToPoint class is a bit more than I'd like to take on at the moment, but I think I may have found a crude workaround. I'll just make 2 U shapes (out of 3 collision boxes each) to define an axis, which would allow a long skinny rigid body box (a 'rail') to slide up and down the axis, then I could use two P2Pconstraints to hinge the final rigid body to the rail. I'll see how that works and come back with an update.

Oct 18, 2009 at 5:09 AM
Edited Oct 18, 2009 at 5:10 AM

Ok now I have a rail that can slide back and forth smoothly, and I can attach a rigid body to the rail with the PtoP constraint, but it only works in the middle of the rail. I'm trying something like this:

foreach(Rigidbody object in objects)
{
	PointToPointConstraint constraint = new PointToPointConstraint();
	constraint.rigidBody0 = rail; // same rail for each object
	constraint.rigidBody1 = object;
	constraint.offsetPosition0 = Vector3.Forward * object.ZOffset / 2;
	constraint.offsetPosition1 = Vector3.Backward * object.ZOffset / 2 + Vector3.Up * object.DistanceFromAxis;
	Vector3 pos = constraint.rigidBody0.Position + Vector3.Forward * object.ZOffset + Vector3.Down * object.DistanceFromAxis;
	constraint.rigidbody1.UpdateTransform(ref pos);
	this.physics.Constraints.Add(constraint);
}

The objects with zero offset do exactly as expected and rotate around the rail nicely, but the objects with an offset fly to the center and crash against each other, like they want to be constrained to the center of the rail. Any idea what I'm doing wrong? Should I be able to constrain multiple objects to the same rail at different offsets like this?

Coordinator
Oct 20, 2009 at 6:57 PM

First, apply the fix to the PointToPointConstraint in any earlier post.  It fixes the constraint-causing craziness.  It looks like what you have above shoud work (assuming that the code above is psuedo-code). If that doesn't work, paste the real code in a post and I'll take a look again.  Good luck.

Oct 23, 2009 at 7:08 AM

Ahh I didn't see that fix before. It almost works now, but the constraints aren't as solid as I need them to be, I'm having a little better luck with JigLibX (sorry :P)

All the other features (gamestate/cameras/input/etc) work perfect for me though so I will most likely be using them in my game.

Thanks again for the help and the great library.