how to keep an entity on a track

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
ron
Posts: 44
Joined: Sat Dec 11, 2010 7:25 pm

how to keep an entity on a track

Post by ron »

i am making a side scrolling game where the box is my hero's entity
as its a side scrolling game so i want my character to be on one track, or else the character will fall of the track,
wht should i do
wht if a add two box entities beside the character which will acts like walls and i will give them the same translation as the character translation, so tht the walls can also translate with the character and the character wont fall of the track
is there any alternative for this?
ron
Posts: 44
Joined: Sat Dec 11, 2010 7:25 pm

Re: how to keep an entity on a track

Post by ron »

ron
Posts: 44
Joined: Sat Dec 11, 2010 7:25 pm

Re: how to keep an entity on a track

Post by ron »

and wht if i have a circular track?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: how to keep an entity on a track

Post by Norbo »

You could use a PointOnPlaneJoint where the Plane is anchored to the world (by passing null in for the plane connection). Another option would be a LinearAxisLimit, which allows a range of values.

A circular track would be trickier. You could bound it with collision geometry would 'work,' though it may be easier just to modify the plane/axis limit over time such that it always restricts the correct axis.
ron
Posts: 44
Joined: Sat Dec 11, 2010 7:25 pm

Re: how to keep an entity on a track

Post by ron »

any demo?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: how to keep an entity on a track

Post by Norbo »

Here's a limit which restricts an entity to between -3 and 3 along the z axis.

Code: Select all

            LinearAxisLimit limit = new LinearAxisLimit(entity, null, entity.CenterPosition, entity.CenterPosition, Vector3.Forward, -3, 3);
There are also a variety of general constraint demos in the BEPUphysicsDemos project. They may help with understanding the general setup of joints.
ron
Posts: 44
Joined: Sat Dec 11, 2010 7:25 pm

Re: how to keep an entity on a track

Post by ron »

wht namespace to add for "linear axis limit"?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: how to keep an entity on a track

Post by Norbo »

BEPUphysics.Constraints. FYI, you can right click on the name of a class that's missing a reference, move to Resolve, and easily add the using statement.
ron
Posts: 44
Joined: Sat Dec 11, 2010 7:25 pm

Re: how to keep an entity on a track

Post by ron »

Code: Select all

limit = new LinearAxisLimit(t.boundingbox, null, t.boundingbox.CenterPosition, t.boundingbox.CenterPosition, Vector3.Forward, 1, -1);
my character is translating in the x axis(negative x axis is its forward direction and positive x axis is its backward direction)
as its a side scroller so i have only two ways for its movement(i.e the front and back movement)
i used the above instance "limit" to restrict the character's translation in the z axis

and for checking whether this works i gave an extra condition in the update function i.e.

Code: Select all

if (t.boundingbox.WorldTransform.Translation.Z > 2 || t.boundingbox.WorldTransform.Translation.Z < -2 ) 
            {
                Exit();
            }
but surprisingly the above condition is getting satisfied
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: how to keep an entity on a track

Post by Norbo »

Try this instead:

Code: Select all

 LinearAxisLimit limit = new LinearAxisLimit(null, t.boundingBox, t.boundingBox.CenterPosition, t.boundingBox.CenterPosition, Vector3.Forward, -1, 1);
And ensure that the limit constraint is added to the space.

The difference is which entity owns the axis, and in addition, the bounds have been reversed. The minimum limit must be less than or equal to the maximum; if not, the system simply clamps it to maintain this condition.
Post Reply