Constrain Object to Axis

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
damagex
Posts: 4
Joined: Mon Jul 26, 2010 7:39 am

Constrain Object to Axis

Post by damagex »

Is there a way to constrain on object in the physics space to only move on 2 axis? Example would be a box falling down a hill, I would like the box to roll down the hill (x axis and y axis) but no be allowed to move left/right (z axis).
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Constrain Object to Axis

Post by Norbo »

You could use a PointOnPlaneJoint. If you also want rotation to only occur around the plane's normal, you could either use a RevoluteAngularJoint or adjust the entity's local inverse inertia tensor. By setting a row in the local inverse inertia tensor to zero, the entity gains infinite inertia around the axis associated with that row. If you only wanted to rotate around the X axis, then setting rows 2 and 3 to all zeroes would do it. The inertia tensor approach is a little bit faster than the RevoluteAngularJoint approach, but it is less flexible.

It's also possible to fake the PointOnPlaneJoint by changing the velocity directly. All the constraint does is determine the velocity of the entity moving away from the plane and fights it. If your plane normal was along the Z axis, you could simply set the entity's velocity along Z to 0. To avoid drifting away from the plane, you could also add a springy component that changes the velocity a bit based on the distance from the plane to the entity's center. Using this fake version will probably be fine; the Joint version is more stable when the plane constraint is fighting other interactions, but most '2D' games don't have many interactions along the third dimension.

The PointOnPlaneJoint and RevoluteAngularJoint are both two-body constraints. You can attach them to a kinematic anchor entity somewhere or just pass 'null' in for the second connection parameter. The engine will automatically use a special static 'world' entity when given null.
damagex
Posts: 4
Joined: Mon Jul 26, 2010 7:39 am

Re: Constrain Object to Axis

Post by damagex »

The PointOnPlaneJoint sounds like a workable approach, I'll give that a try, thanks!
Post Reply