collision detection on floors

Started by
0 comments, last by bilstonwarrior 20 years, 2 months ago
hi i have a sort of hilly terrain in my 3D directx game. I have got the collision detection working and it can check if the player has collided with the plane (of the floor triangles). what i have a problem with however is that if the player is walking up a hill then he falls thru the floor. I need to make it so he can walk all over the polys and not go thru the collision detection. imagine the lines below is the floor and a start of a hill. _________/ the player gets to here: ________X/ then moves one more: _________/X and is now "below" the hill poly. and just drops down from then on. How do i make him climb the hill? thanks in advance!
Advertisement
welcome to the magic world of collision detection

well, it''s more collision response, if you already have a way to check of the player hit the hill triangle or not.

you need to push the player away from the triangle. There are many ways to do it, depending on your collision detection algorithm and what it returns.

I''ll use a simple one, which returns the point of collision on the triangle, and one on the player.

once you have the two points (Pplayer, Ptriangle), to push the sphere away from the triangle, you simply do

Player.Position += (Ptriangle - Pplayer);

in any case, if you don;t have the collision detection ready, here is an example of an algorithm for sphere / triangle collision detection

first, find the point on the triangle the closest to the sphere centre.

check that point is inside the sphere. if not, no collision

if the point is inside, calculate the point on the sphere by projecting the point on the triangle onto the sphere surface.


//--------------------------------------------------------------// Sphere : C, r// Triangle V[3]// results : Psphere, Ptriangle//--------------------------------------------------------------bool SphereTriCollide(const Vector& C, float r, const Vector* V, Vector& Psphere, Vector& PTri){    //-----------------------------------------------------------    // calculate closest point on triangle to the sphere,     // and also the distance squared of those points    //-----------------------------------------------------------    float s, t; // barycentric coordinates of the closest point    float d2 = PointTriangle_DistanceSquared(C, V, &s, &t);    if (d2 > r2) // closest point outside sphere, no collision        return false;     //-----------------------------------------------------------    // calculate the point on the triangle, using the     // barycentric coordinates calculated    //-----------------------------------------------------------    Ptri = V[0] + s * (V[1] - V[0]) + t * (V[2] - V[0]);    Vector D = Ptri - C;    float d  = sqrt(d2);    D       /= d; // normalise D    //-----------------------------------------------------------    // calculate point on sphere surface    //-----------------------------------------------------------    Psphere = Ptri + D * (r - d);    return true;}


once you find a collision, push the player by (Ptri - Psphere);

here is the calculation for finding the closest point on a triangle to another point in space

http://www.gamedev.net/community/forums/topic.asp?topic_id=208178

and you can see the results here

Everything is better with Metal.

This topic is closed to new replies.

Advertisement