Advertisement

Collision Detecting

Started by January 12, 2001 06:12 PM
32 comments, last by Running_Wolf 23 years, 9 months ago
Yup, the easiest way to do this, is to find exactly where the player is moving before he moves there.

A basic example on how to do this:

Temp_Cam.x = Camera.x + 0.4
if CheckCollision(Temp_Cam.x) then DontDoNextLine
..else Camera.x = camera.x + 0.4

Temp_Cam.y = Camera.y + 0.4
if CheckCollision(Temp_Cam.y) then DontDoNextLine
..else Camera.y = camera.y + 0.4

Temp_Cam.z = Camera.z + 0.4
if CheckCollision(Temp_Cam.z) then DontDoNextLine
..else Camera.x = camera.z + 0.4

Sorry, i have limited time on my hands, if you want a more detailed example, i''d be more than happy to post it here.
wavarian,
Could you post a more detailed example? I understand what you mean I just can''t implement it right. Meaning I get weird things like no matter what I do I can''t move forward and stuff like that. If you want to see the code please e-mail me.
L.I.G. == Life Is Good
Advertisement
I got it working!!! There is just one small problem. It seems that the formula that you gave me is for infinite planes. My plane is not infinite. I wish to be able to walk around it. How do I do that?
L.I.G. == Life Is Good
test if the point where the plane intersection occurs is inside the polygon. theres heaps of info on the web on how to calculate this

http://members.xoom.com/myBollux
zedeek,
Where can I find some of this info?
L.I.G. == Life Is Good
wavarian:
Sorry, I don''t quite understand what you did. Is what you just said and extension to the equation you gave me earlier or does it replace it? In short, I''m just not quite sure how it all relates to each other. Sorry if I am being a little annoying or stupid. I have a hard time grasping some things.
L.I.G. == Life Is Good
Advertisement
>>zedeek,
Where can I find some of this info?<<

goto www.google.com

and type ''point inside polygon''


http://members.xoom.com/myBollux
A simple way of determining if a point P is inside a polygon is to:

1. Draw a line from P to each vertex of the polygon
2. Add up all the P angles of each triangle thus formed
3. If the angles add up to 360, the point is inside the polygon
4. If the angles add up to less than 360, the point is outside the polygon.

Cheers
wavarian: I don''t think that I am implementing it right yet? It doesn''t seem to work. Here is what I am doing. If you want all my code just ask.

I create a quad of:
p1: 0.0, 2.0, 3.0
p2: 0.0, 2.0, 0.0
p3: 0.0, 0.0, 0.0
p4: 0.0, 0.0, 3.0

at creation(in the InitGL function for testing but will be part of the constructor in the final class)

float nx, ny, nz, mag, d
nx = p1.y*(p2.z - p3.z) + p2.y*(p3.z - p1.z) + p3.y*(p1.z - p2.z)
ny = p1.z*(p2.x - p3.x) + p2.z*(p3.x - p1.x) + p3.z*(p1.x - p2.x)
nz = p1.x*(p2.y - p3.y) + p2.x*(p3.y - p1.y) + p3.x*(p1.y - p2.y)
mag = sqrt((nx*nx) + (ny*ny) + (nz*nz))
nx = nx / mag
ny = ny / mag
nz = nz / mag
d = -(nx*p1.x + ny*p1.y + nz*p1.z)

In the Main Function:
float dv1, dv2, dv3
float distance, d2
float x1, x2, x3

Every time through the loop:
dv1 = x - x2
dv2 = y - y2
dv3 = z - z2
(x,y,z being the place to move to and x2, y2, z2 being current position)

d2 = (-d - nx*x2 - ny*y2 - nz*z2) / (nx*dv1 + ny*dv2 + nz*dv3)
x1 = x2 + (d2*dv1)
y1 = y2 + (d2*dv2)
z1 = z2 + (d2*dv3)

if ((x1 > 0.0) && (x1 < 0.0))
if ((y1 > 0.0) && (y1 < 2.0))
if ((z1 > 0.0) && (z1 < 3.0)) {
distance = x1*x2 + y1*y2 + z1*z2 + d
if ((distance < 0.5) && (distance > -0.5))
movef = FALSE;
else
movef = TRUE;
}
Draw Scene


What am I doing wrong?
L.I.G. == Life Is Good
Well firstly, from quick glances at your code, i see that you have:

if ((x1 > 0.0) && (x1 < 0.0))

This is saying that x1 must not be equal to 0.0, try doing it something like this:

if ((x1 >= 0.0) && (x1 <= 0.0)) // Not sure how c++ works with this

so that the point is ''allowed'' to be sitting on the x-axis.

-- Wav

This topic is closed to new replies.

Advertisement