Advertisement

Collision Detecting

Started by January 12, 2001 06:12 PM
32 comments, last by Running_Wolf 23 years, 9 months ago
I read the NeHe tutorial on collision detecting. But I don''t understand what he''s saying. Could someone please explain it or is there a web page where I can go to get a better understanding? I just want to do basic detection for planes and spheres.
L.I.G. == Life Is Good
if you have simple planes like x = 0 or y = 0 or z = 0 then to find the distance between a point and the plane is very simple. For example, if your plane is x = 0, then the distance from a point to it is simply the absolute value of the x coordinate of that point. eg (4, 1.5, 9) is 4 units away from the plane x = 0.

however, if you have a plane at arbitrary angles, the math is a bit more complex. Assuming that the plane has the equation
Ax + By + Cz + D = 0, and your point is (Xo, Yo, Zo), the distance between the two is
abs((A*Xo + B*Yo + C*Zo - D)/sqrt(A^2 + B^2 + C^2))

you can arrive at that formula with some vector analysis, ask me if you want me to explain it.

I hope this helps, sorry if it came out confusing.

/riley
--Riley
Advertisement
Yup.. there''s a math geometry page at: http://www.swin.edu.au/astronomy/pbourke/geometry/

It deals with alot of intersections with planes and lines etc.

-- wav
Forgive my ignorance, but isn''t it easier and faster to calculate the distance between a point and a plane using the plane''s normal, one point on the plane, and a dotproduct?
www.mr-gamemaker.com ''s tutorial on the dotproduct covers this and is IMHO a very good read for beginners in the collision detection area. You can find it here: http://64.33.37.114/math_rot3.html
Dirk =[Scarab]= Gerrits
"but isn''t it easier and faster to calculate the distance between a point and a plane using the plane''s normal, one point on the plane, and a dotproduct?"

abs((A*Xo + B*Yo + C*Zo - D)/sqrt(A^2 + B^2 + C^2)) is what you end up with when you do that. The sqrt comes in when you normalize the, err, normal (scale it down so its length is one). The point on the plane you were talking about gets sucked into D. If the point were (Xp, Yp, Zp) then you would end up with A*Xp + B*Yp + C*Zp. From the equation of the plane you know that A*x + B*y + C*z + D = 0, so A*Xp + B*Yp + C*Zp = D.

I love that stuff =)

/riley
--Riley
rileyriley,
I would like it if you would explain it a little bit better. To draw my planes I am using GL_QUADS and four vertices.
L.I.G. == Life Is Good
Advertisement
Running_Wolf: Okay, you have a quad''s verticies labeled P1,P2,P3 & P4.

Firstly, you need to find the normal vector (a vector which is perpendicular to the actual quad). Let N represent the normal vector.

N.x = P1.y*(P2.z - P3.z) + P2.y*(P3.z - P1.z) + P3.y*(P1.z - P2.z)
N.y = P1.z*(P2.x - P3.x) + P2.z*(P3.x - P1.x) + P3.z*(P1.x - P2.x)
N.z = P1.x*(P2.y - P3.y) + P2.x*(P3.y - P1.y) + P3.x*(P1.y - P2.y)

Then you should normalize this vector to make it easier for openGL to digest:

Mag = Sqrt((N.x*N.x)+(N.y*N.y)+(N.z*N.z)) // Length of Vector
N.x = N.x / Mag // Normalize Vector
N.y = N.y / Mag
N.z = N.z / Mag
-------------------------------------------------------------
Next, the equation of a plane.. Ax + By + Cz + D = 0
(where A, B & C represent the plane''s normal)

You then need to find the constant term, D, and you do this by substituting any point on your quad for x,y & z.
D = -(N.x*P1.x + N.y*P1.y + N.z*P1.z)

With the new found value for D, you now can write your plane equation as:

N.x(x) + N.y(y) + N.z(z) + D = 0
--------------------------------------------------------------
So how do you find the distance between a point and the plane on which your quad lies? Substitute a point in for x,y, & z.

Want to find out the distance between the point (0,1,6.5) and your quad''s plane? do this:

Distance = N.x(0) + N.y(1) + N.z(6.5) + D

which, in turn, simplifies to:
Distance = N.y + N.z(6.5) + D

-- Wav
Thanks wavarian,
I have just one question. The normals that you calculated and set for the quads. Is it applicable to normals when I try to do lights or do I have to do a seperate normal calculation for each of my quads?
L.I.G. == Life Is Good
The normals specified may be used for lighting as well. As for your other question, it depends. If you intend to draw exactly the same quad, but only rotate it a certain degree about the y-axis, or translate it, then yes you can use the same normal for the rest of your rotated/translated quads, just as long as you do to the normal just as you do to the quad. eg.

glNormal3f(0,0,1)
glDrawQuad1

glPushMatrix
glRotatef(45,0,1,0) // This line would rotate the Normal AND the quad at the same time.
glNormal3f(0,0,1)
glDrawQuad1
glPopMatrix

--wav
Okay, I got it to tell me when the distance i zero but I have a couple more problems. When I reach 0.5 from the quad I have a bool moveforward or movebackward that is turned to false. But when this happens I get stuck. I can''t go anywhere. How is the best way to make it so that when the player is close to a quad or sphere that they can''t go forward or backward as the case may be but can still go in the other directions. Also, right now I have to check with every single quad or sphere I have. How can I tell where the player is going and what objects are in that direction and just check them? Did that make sense to anyone?
L.I.G. == Life Is Good

This topic is closed to new replies.

Advertisement