views:

739

answers:

6

I'm very new to creating games (and working in OpenGL in general). I'm trying to make a 2D game but have come upon a stumbling point when trying to get my head around collision detection.

Lets say for example I translate the current matrix, the rotate and translate again and then draw a point. How do I know where that point is in relation to anything else I might have drawn?

Is there a way of calculating "absolute" coordinates from the current matrix?

Please explain as simply as possible! thanks :)

A: 

When I learned OpenGL, I used the following 2 resources for everything.

It appears the gametutorials has gone paid-for, selling a CD, but they were well written so it might be worth buying it.

However, NeHe are still free tutorials on the web, including this one for collision detection.

http://www.gamedev.net/reference/list.asp?categoryid=31

gbjbaanb
+2  A: 

It's best to just keep track of every objects absolute position.

Ryan Kearney
This was my first though but there must be a more graceful way of handling this?!
michael
Once you transform the matrix that's it, it's been transformed. I'm not sure what you're trying to do. Are you just randomly translating objects? When you render a frame you know, or should know, exactly where every object is going to render. I've never seen game code that didn't have x, y[, z] coordinates for each object.
Ryan Kearney
A: 

What do you mean by "absolute"?

If you want your point relative to the prior points you can reverse the transforms you just did: translate by the negative of the second translation, rotate in the opposite direction around the same axis by the same amount, then translate by the negative of the first translation. This will give you a transform from your new coordinates to the old system.

Alternatively you could use the transform you already have to take your old points into the new coordinate system.

Andrew Khosravian
But what happens when both my points have been translated?By absolute, I mean axis aligned coordinates which have had no transforms or rotations.
michael
I get the feeling you might be asking the wrong question here. What are you actually trying to accomplish from a high level?
Andrew Khosravian
+3  A: 

OpenGL isn't a game engine but a rendering library. Collision detection on the other hand is a function of your game logic. Therefore, it should have nothing to do with OpenGL, which is just the viewing mechanism for putting a visual representation on the screen - it knows nothing about your game objects nor does it hold any useful persistent data on them. If you do try to store an object's position in a matrix and move it via transformations then you can eventually suffer from the effects of cumulative floating point errors leading to unpredictable behaviour.

What you should do instead is maintain all your object's positions (and orientations, velocities, etc) manually. All movement of objects should be performed by you in the application code. When it comes time to render the objects, you read their current values and pass those in to OpenGL. Typically you take the polygon data for each type object you want to render, rotate it by the object instance's orientation, translate it by its position, then render. Repeat once per frame.

If you're at all familiar with the Model-View-Controller paradigm then your game objects are the Model, and OpenGL provides a View. As such you should manipulate your Model as needed and just have the View read from it when needed.

Kylotan
I guess I need to look into a suitable way to maintain the positions manually then
michael
A: 

Keep track of the object positions in world coordinates, and compare them directly.

To do the display, use a camera model, and apply all your transformations to it.

Jon Seigel
+1  A: 

Hi Michael,

Frustrating, isn't it? All OpenGL and graphics programming beginners have questions similar to yours. Keeping track of the various frames of reference can be tough.

The first thing I would recommend is getting a copy of the OpenGL SuperBible from Amazon or another retailer. It starts at the absolute beginning and progressively builds from there. The earlier poster who recommended the NeHe site is giving good advice too.

As for your question itself, when you create an OpenGL program you can set the "modelview" matrix to be the identity (see the SuperBible for more info) and all coordinates you set in glPoint() calls will be defined relative to that matrix as the frame of reference. If you don't do anything besides call glPoint() from here on out all your objects will be in the same, absolute coordinate system.

It gets tricky when you start calling glTranslate and glRotate and glLoadMatrix, as those functions transform the frame of reference. When that happens all subsequent glPoint() calls are then relative to that new frame of reference.

(As mentioned by another poster, if you want to go back to the earlier frame of reference you have to save the modelview matrix on a stack before transforming it, and then you can "pop" the stack and get back to where you were before.)

It's hard to describe this on a support site, but think of it as a series of mechanical arms in a dentists chair. He can move the tray to be under your chin, then rotate it at an angle to cup your chin. Each independent motion can be thought of as a transformation, implemented in OpenGL as a matrix.

Like I said, it's hard to describe this properly online. Grab the SuperBible book and it'll make a lot more sense. Good luck!

larryq