Advertisement

Camera collision with walls.

Started by March 23, 2002 05:41 PM
4 comments, last by GekkoCube 22 years, 10 months ago
My camera is adapted from DigiBen''s (gametutorials.com) many camera tutorial/demos. I have a 3d room (basically a skybox). And i would like my camera to not pass thru the walls. Each wall is composed of 2 triangles. (Does it matter if they are drawn CW or CCW?) What kind of math is needed to do this? an example is helpful, and so is a demo, and so would a website with good/simplistic info. many thanks. ~ I am a DirectX to OpenGL convert! ~
Can we say collision detection?

As for the method to use it would depend on the point of view, ie. first person or third person.
"Who are you, and how did you get in here?""I''m the locksmith, and I''m the locksmith."
Advertisement
First of all, you would need to calculate the plane normals of each of the triangles in your box.

Given 3 vertices that define a triangle V1, V2, and V3 arranged in counter-clockwise order, calculate the plane (a vector N and a float DIST) thusly:

A = V2 - V1 // Vector subtraction
B = V3 - V1 // Vector subtraction
N = A x B // Vector cross product
Normalize (N)
DIST = N dot V1 // Vector dot product

Knowing the plane equation of a triangle, you can now figure out which side of the plane a particular point (say, the camera) is on:

Distance_from_plane_to_point = N dot Point - DIST

If the Distance_from.. value is less than 0.0, the point is "behind" the plane. If it''s 0, then it''s lying on the plane; greater than 0, in front of the plane.

Now, if you find your camera is behind the plane and you want to move it so it''s on the plane, use this:

Point = Point + (N * Distance_from_plane_to_point) // Vector/scalar multiply, then vector addition

This simulates the "sliding" motion you would see in a 3D shooter.

Now, using just planes for collision detection assumes that your world is convex (inside of a cube) and no holes. Otherwise, you would need to test your point against each edge of a triangle.

If this seems too confusing, let me know...

Mark Grocki
Lead Developer
2Real Entertainment
http://www.2realentertainment.com
Mark Grocki
Here is an important question.
I am currently drawing every wall clockwise.
So basically if inside the box you can see the polygons.
But if viewed from outside you wouldnt see anything.

You mentioned that the vertices must be coutner clockwise.
But why???

~ I am a DirectX to OpenGL convert! ~
Arsenal''s approach is pretty much what I use, with a few extra things. One is that you can store the plane normals and distances, e.g. work them out once whan you initialise them then keep a copy of them around.

Second don''t just make sure the camera is > 0 from each plane: instead choose a non-zero distance and move the camera if it is within this distance from the plane. If the camera is very close to or on the plane you can have problems with it viewing the plane edge-on, with it being clipped by the near clipping plane as well as texturing and mip-mapping it.
John BlackburneProgrammer, The Pitbull Syndicate
To answer your question GekkoCube:

You care about the order in which you add the vertices, because it affects the face normals. And, the face normals are what defines "inside" and "outside" in the equation. 3D math only knows what''s underground, outside the box, etc., based on your definition of which way the polygon is "facing".

So, you don''t have to add the vertices in a counter-clockwise order, but if you don''t, distances greater than 0 would be "behind" the plane.

This topic is closed to new replies.

Advertisement