Skip to main content
gamedev.net
🔒 Locked

3D Collision

Started by dcgeek Dec 28, 2002 at 1:30 PM 4 replies 400+ views
Original Post
dcgeek
dcgeek
Let''s say the user''s view is at 0,0,0 and the center of a box is at 0,0,-1. Then I want to move the box away from me, so I write 0,0,-2. But I don''t want to move the box. I want to move the user''s view. How do I do? And how do I detect when the user''s view hits one of the box''s coordinates and push it 0.2 units away from it?
Reverse_Gecko
Reverse_Gecko
You can''t actually move the cameras veiw, it always remains at (0,0,0), but what you can do is make a camera structure, where the camera has an xyz and rotational values, and before you draw anything each frame just do this:
glRotatef(360-xrot,1.0f,0.0f,0.0f);
glRotatef(360-yrot,0.0f,1.0f,0.0f);
glRotatef(360-zrot,0.0f,0.0f,1.0f);
glTranslatef(-x,-y,-z);

that will move all objects in your world to where they should be relative to the camera. It will appear that the camera is moving, but really its the objects moving.

Then you can do a simple point in box calculation to see if it hits the box. For example:
if((x>=box_min_x)&&(x<=box_max_x)&&(y>=box_min_y)&&(y<=box_max_y)&&(z<=box_max_z)&&(z>=box_min_z))push_the_camera();
Jeff Bland (Reverse_Gecko)reversegecko@gmail.com
HappyDude
HappyDude
To ''move'' the camera, use gluLookAt. Parameters are x, y & z coordinates for camera position, look at, and up vector, in that order.
dcgeek
dcgeek
If I use your example, then I still have to rotate _every_ box
in my 3D world, and let''s say I have 50 boxes, then I have to rotate 50 boxes... That''s too much code...
But If I can move the camera, then I don''t have to worry about the boxes.
dcgeek
dcgeek
If I use your example, then I still have to rotate _every_ box
in my 3D world, and let''s say I have 50 boxes, then I have to rotate 50 boxes... That''s too much code...
But If I can move the camera, then I don''t have to worry about the boxes.
LockePick
LockePick
*takes a deep breath*
There is no camera in OpenGL. The camera is your monitor, after all, it can''t move. So since you can''t move the camera, you can achieve the same effect by moving the world instead.
This is what the MODELVIEW matrix (aka. the camera matrix) does.

Say you have a movie theatre. Inside in an usher, who has been told that he should direct anyone who comes in down a hall to the right and into a specific room.
A person walks in, reaches the usher, and then walks down the hall and into that room.

This is similar to how geometry is handled in OGL.
The MODELVIEW matrix is the usher. It is a bunch of numbers, which give directions.
For this example, the person will be a box.

You tell the box to draw at 0,0,0 (the person walks into the theatre), but before it gets drawn it has to go through the modelview matrix (it has to talk to the usher). The matrix might tell it to move +5 on the x axis and -5 on the z axis (walk down the hall and into a room). So the box will move +5,0,-5 and then be drawn.

So how you handle the camera is by modifying what the usher says. If you "move foward" in a world, what you really want to do is have the usher say "move backwards" so it looks like you are moving in the right direction.

In your case, you don''t have to rotate every box. That''s what the modelview matrix does! All you have to do, is use glRotatef/glTranslatef or gluLookAt to make sure the modelview matrix knows what it''s supposed to do. When you go to draw the next frame, you use glLoadIdentity (which tells the modelview matrix to do nothing), and then give it new information about how it should move things.

Collision detection is a whole other ball-game, I suggest getting used to movement and rendering first.

------------
MSN: nmaster42@hotmail.com, AIM: LockePick42, ICQ: 74128155
"It''s all part of the conspiracy of conspirators conspiring to conspire their own conspiracies..."

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.