Slopes just plain confuse me...

Started by
13 comments, last by BrianDra2 23 years, 4 months ago
Smooth collision detection isn''t really so bad when you get a few things working..there are many ways you could optimize terrain collision, but here''s a general all purpose method of smooth collision detection that should work in several implementations..

First of all you have to determine which surface you are near enough to clip against..then clip the model position against that surface in 3d space..(you will have to add some height offset to the model unless the origin is at the bottom)..

Let''s assume that the model position is a single vertex(you can easily use this same code for multiple vertices)..
You would also need to determine minimum_clipping_distance..this is how close you can get to a surface before it ''pushes back''..this distance should be slightly larger than the model''s average movement per frame...

Here''s how you would clip against a surface and make it smooth:

Get the distance from the vertex to the plane of the surface..let''s call this distance_from_plane..
If distance_from_plane is greater than your minimum_clipping_distance, there is no collision necessary..
IF NOT
You need to find out how far into this clipping region you are..let''s call this distance_inside_clipping_zone..

distance_inside_clipping_zone = (minimum_clipping_distance-distance_from_the_plane)

that was easy..
now that we know how far into the clipping region we are, we just need to relocate the model..

model.x += surface.xnormal*distance_inside_clipping_zone;
model.y += surface.ynormal*distance_inside_clipping_zone;
model.z += surface.znormal*distance_inside_clipping_zone;

WARNING!!
(you may have to -= if you are colliding with the back of a surface..front/back test is easy to check, just subtract values instead of adding)..
////////////////////////////////////////
WHY THIS IS COOL

No matter how you come at a surface, it will always push you out a consistent distance..add a constant gravity drop and the model will actually slide down ramps on it''s own..when moving forward up a ramp, your forward motion is hampered depending on the angle of the surface..when you walk into a wall you will slide smoothly along..

The math isn''t so bad either..assuming you''ve already got normals for your surfaces, here''s all you have to do:

Calculate the distance from a point to a plane..
Subtract that from the minimum_clipping_distance..
To the model vertex, add the surface normal multiplied by the distance inside the clipping region..

If you need help with any of the math feel free to email me
Enjoy,
-MM


"Like all good things, it starts with a monkey.."
"Like all good things, it starts with a monkey.."
Advertisement
>Get the distance from the vertex to the plane of the >surface..let''s call this distance_from_plane..

I need this clairifed a little more(like actual code would help alot)


Thanks,
Brian Draeger
briandra2@aol.com
Here is what I used

double ret = 0.0f ;

double x1, x2, x3, y1, y2, y3, z1, z2, z3;
x1 = map[Fi][Fj].x; y1 = map[Fi][Fj].y; z1 = map[Fi][Fj].z;
x2 = map[Ci][Fj].x; y2 = map[Ci][Fj].y; z2 = map[Ci][Fj].z;
x3 = map[Ci][Cj].x; y3 = map[Ci][Cj].y; z3 = map[Ci][Cj].z;
double a,c,d,e,f,g,h,i;
a = x - x1;
c = z - z1;
d = x1 -x2;
e = y1 -y2;
f = z1 -z2;
g = x1 -x3;
h = y1- y3;
i = z1 -z3;
ret = (g*e*c +a*h*f - a*e*i - d*h*c)/(g*f - d*i) + y1;
return ret;



Better explain a little
You say you know what face you are on, so you just put the 3 vertices from that face in x1, y1, z1, ...
From there I calculate the plane that face is on, and I check at wich point it intersects wih a line going straight through the point x, z from (a = x - x1;c = z - z1
y goes up in my landscape engine
This is probably the wordst explanation ever but I hope it helps
Heheh .. if you''re using a height map.. which I''m sure you are.. compare the coordinates where YOU are at to the ones in the heightmap and adjust your height accordingly.. NOW you have a VERY crude walking system.

Simply put.. CHEAP and ugly. though it can be refined easily by looking at where you are inside the quad for the terrain (two tris rather). then running some distance and weighting calculations


If you need some more help..
email me at jizzroy@hotmail.com

good luck
I figured out a quick solution to this about a month ago after beefing up my multi-variable calculus

The plane formula does work if you use it correctly. I wasn''t.

The formula Ax+By+Cz=0 is a simplification of the formula A(x0-x1)+B(y0-y1)+C(z0-z1)=0. In other words if you''re just plugging in values for x,y and z into the first formula you''re going to go wrong every time unless they are relative to zero.

If, on the other hand, you use the non-simplified formula, you get the results you want: x0, y0, z0 is the coordinate of any known point on the plane (just use any vertex of the plane you''re on), and x1,y1,z1 is the point you are looking for (use algebra to modify the formula to find y1).

I''ve been using the plane formula successfully for 4 weeks now and it is smooth as silk on landscapes. It also doubles as a very fast method for projectile/landscape collision detection.





This topic is closed to new replies.

Advertisement