Archived

This topic is now archived and is closed to further replies.

Compaq

Yeah I know it's been asked a lot but....

Recommended Posts

Ok guys. I've seen people ask about collision detection a lot on this forum. And I've tried a fair amount of things. But this is my first time working with 3D spaces and rotating planes. So I figured I'd ask for some help . My first approach was to just translate the coordinates just as glTranslatef() does and then to test whether or not the point was within .2 of the plane on the x, y, z axis. I realize this is quite crude but this is my first time so bear with me. Then I realized that I never rotated the plane. So after some time I figured out how to rotate the plane (I think), using gamedev's resources page. So I did that and I realized that my code was still flawed. I decided to rewrite the code to drop a perpendicular down to the plane from where the player wants to go and see if that's within the minimum distance. For some reason this is not working and my code seems quite hopeless as I figured out the math while I was coding it. So now I was going to take the four endpoints of the plane and compare the distance with where the player is and where the player wants to go. I figure that if the minimum distance to the four vertex points lies on the line drawn from where the player wants to go and where the player is (rather then the endpoints) then the player can't go there because they are crossing a plane. I could easily subtract 0.1 from the value where they are going to account for whether or not the endpoint of the line is on the plane and that would seem ok, but I'm having a little trouble with the translation algorithm (to translate the coordinates of the plane) and an algorithm to find the distances (I guess I would use the distance formula but because the player is always 0,0,0...I believe...then that should be easy). Am I on the right track and if so could someone just help me understand how to translate the points. You don't necessarilly need to write the code for me I just want to understand the mathematics behind it. Thanks a lot. -------------------------- HelloWorld.exe HelloWorld.cpp - 13 Error(s), 23 Warning(s) Edited by - Compaq on January 14, 2002 9:05:18 PM Edited by - Compaq on January 14, 2002 9:05:57 PM

Share this post


Link to post
Share on other sites
If n is a normal vector to a plane and p is a point in the plane then n.p=d where . is the dot product and d is a constant. A point in the plane is p=n*d. That point is the point in the plane closest to the origin. That equation also shows that d is the distance of a plane from the origin in multiples of the normal. If the normal is a unit vector, i.e. magnitude 1, then it is in multiples of the unit length. If it is a unit normal vector then p.n-d is the directed distance of a point from the plane. If p is in the plane then that is of course zero. If it is not zero then it is either negative or positive. If it is negative then it is on one side of the plane, if it is positive it is on the other side.

The parametric equation of a line is p(t)=p0+v*t where p0 is a point on the line and v is the direction of the line. Generally with collision detection p0 is the position at the start of the frame, v is how far the object can move per unit of time and t is the duration of the frame. The intersection is p(t).n-d=0 or (p0+v*t).n-d=0 or p0.n+v.n*t-d=0 or v.n*t=d-p0.n or t=(d-p0.n)/(v.n).

You can find the normal for a plane by taking the cross product of two vectors in the plane. If you have three points in the plane that are not in a line then the displacement vectors from one of the points to each of the other points is two vectors in the plane. There are two directions for the cross product. If you connect the points they form a triangle. If you label one point p1 then you have two choices for which point is p2. The direction of the cross product corresponds to which choice you make.

Edited by - LilBudyWizer on January 14, 2002 9:43:53 PM

Share this post


Link to post
Share on other sites