Super-Basic Physics Engine

Started by
5 comments, last by jamesleighe 12 years, 10 months ago
So, I simply need to be able to walk on land... The player being represented by an axis-aligned bounding box, and the terrain will be just a bunch of triangles.

I found out how I can detect if a triangle is inside the AABB, but I don't know what to do after that... Or if that's even a good way to do things.

Halp!
Advertisement
I am not sure exactly what you're describing, but do you mean like:


if (onGound) // If there's triangles inside AABB
{
if (verticalSpeed < 0)
{
verticalSpeed = 0;
}
}
else
{
verticalSpeed -= 1;
}


I am not sure if I understood you correctly, I apologize if this wasn't any help
"Rodimus and Unity" - My developer journal
That's not really good enough, I could collide with a wall for example. Also, if I step and find out I collided, then I just set the vertical velocity to zero I would be stuck etc...

Somthing a little more robust than that is definately required but thanks.

EDIT: also the terrain could slope etc. etc. there are a ton of reasons I can't use somthing quite that simple.
A simple solution is to find the minimum translation distance and move the player by that amount for each triangle. Assuming the terrain doesn't curve past vertical you only need to test the bottom 4 vertices and the bottom 4 edges and the bottom face. For the vertices you'll test them against the triangle's "voronoi" regions. Okay imagine each triangle as a triangular prism in the direction of the normal. The vertices will be inside of a triangular prism or inside of the region between two triangle prisms. Now to find the case that the vertex is inside of a triangle you just project the point along the normal and do a point in triangle test and then do a distance from a point to a plane (the triangle plane). That distance represents the minimum translation distance that would separate the player's AABB from the terrain. If the point is between two prisms you do a simple distance from point and 3D line test.

The other test you'll need to perform is an edge to edge test between the triangle edges. This will be a distance between 2 3D line segments.

The last test is the triangle vertices vs the bottom plane which is similar to the AABB vertex vs triangle test except you'll test to see if the vertex on the terrain is inside of the AABB by using a point in AABB test then if so find the minimum translation distance to push the point out of the bottom. (hint: it's just triangleVertexZ - bottomZ). Then just add that MTD to the AABB's position and it will separate the two.

Now I explained the basic concept and I don't want to bore you with the details if you're already good at a math or googling since these distance tests are easily found online. If there's something confusing ask.
You can do simple stuff by casting rays against the terrain mesh and seeing if the rays intersect the mesh, and where

For instance, take the object's current position, and the new position (that you'd like to try and move to). Start a raycast from some distance above the new position, and cast straight down. That'll give you the height of the terrain at the new position.

If your desired position ends up below the terrain, you're trying to walk up a slope, and the difference in height from your current position and new terrain height is the slope - if that difference is too great, you may want to not allow movement ("too steep").

If the terrain height ends up below your new desired position, you may have just walked off of a cliff (transition to "falling" state + animation?).

If the difference is within some small threshold, you might just want to snap to it, so as to instantly follow the curvature of the terrain

That should give you a pretty basic system to start with.

EDIT: I should mention this assumes your terrain is a heightmap. If that's not the case, you'll need something more complex.
Thanks everyone, I will digest this and try a few things and surely be back with more questions!
The missing feature in my basic physics seems to be a method for calculating the minimum distance between the AABB and a triangle.

I guess I need to find the closest feature. How would I go about calculating the closest feature between the AABB and a triangle? (and visa-vursa etc.)?

EDIT: Keep in mind the triangles may be arbitraraly oriented so the algorithm must be somewhat robust.

This topic is closed to new replies.

Advertisement