how to keep the mesh aligned with the plane(s) below

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
igorfk
Posts: 15
Joined: Sat Mar 08, 2008 10:39 pm

how to keep the mesh aligned with the plane(s) below

Post by igorfk »

with the basic collision detection, explained in the tutorials, this is the way I can make some mesh climb a mountain (for example), the mesh stay aligned with the world axis, not realistic at all =).
Image

What's the trick to make the mesh stay aligned with the below plane and pair of planes like this, to be more realistic?
Image

After trying to fully understand euler angles, quarternions and 4 dimension arrays my brain fried.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Well when you do the collision detection in irrlicht with getCollisionPoint it returns a triangle to you which is the triangle the ray collided with. That triangle has a normal. Assuming the normal is correct you can then use that to align your mesh to the surface with some clever maths. I guess if you did a dot product between your mesh's up vector (which you'd have to define and keep updated) and the normal of the triangle you'd be able to work out the angle between the two vectors and know how much to rotate the mesh by to align it.
Image Image Image
igorfk
Posts: 15
Joined: Sat Mar 08, 2008 10:39 pm

Post by igorfk »

Exactly JP

I think this image illustrates better the problem:

when the mesh bounding box is over a single plane it's easy to find the bbox inclination, but when it's over two planes, with diferent angles, that inclination varies with the bbox position. Well, what math formula I have to apply to get those inclination angles?

Image
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

you could cast two rays down, from each side of the box to get two triangles that the box is above. In most cases i guess the triangles will be the same so you just align to that normal. If they're different triangles then you get the case in the above picture you showed where you have to work out an alignment for both. Then what you'd do is add the two normals together, normalise the result and then align to that.
Image Image Image
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Short answer: use a physics engine.

Medium answer: work out where the X and Z of the four corners of your box are (via trig from the Y rotation), find the heights at those points, then calculate appropriate X and Z node rotations and a total height from those points using horrid, horrid maths.

Long answer: too long. People spend man-years on this subject. There are a lot of variables: what happens when your corner points span a hill crest or peak? When do you decide when a corner should be in the air? When do you decide when the whole vehicle should be in the air? Can it rotate with unbounded speed, or do you have to limit the rotation speed? In other words: use a physics engine.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
igorfk
Posts: 15
Joined: Sat Mar 08, 2008 10:39 pm

Post by igorfk »

where (x1,y1) is the contact point on a plane and (x2,y2) on the other plane.

I'll try to use this formula (y2-y1)/(x2-x1) to get the Z axis inclination between those two points. For the X axis inclination I'll do the same but with contact points at (y,z).
I'll have to do like JP said, use two rays down from each side to get the Z axis inclination, and other two for the X axis.
Post Reply