# How to Obtain Coordinates of an Object in OpenGL

I want to be able to get the coordinates of an object (e.g. triangle) after it's been translated and rotated, the reason i want to do this is so that later i can do collision detection and calculate the distance between objects using the coordinates. I think I might have to use gluProject but not sure. Also what are the differences between the different coordinate spaces e.g. world, object etc.

I've got some code below it's a circle in the middle of a square, how would i detect when the circle touches one of the edges, i can move it round using the up,down,left, right keys it just changes the x or y coordinates, but i just want to be able to do some basic collision detection and I don't know how to do it.

``````glPushMatrix();
glColor3f(0.0f, 1.0f, 0.0f);
glTranslatef(0.0f, 0.0f, -5.0f);

glScalef(0.5f, 0.5f, 0.0f);
glBegin(GL_POLYGON);
glVertex3f(-5.0f, -5.0f, 0.0f);
glVertex3f(5.0f, -5.0f, 0.0f);
glVertex3f(5.0f, 5.0f, 0.0f);
glVertex3f(-5.0f, 5.0f, 0.0f);
glEnd();
glPopMatrix();

glPushMatrix();

glColor3f(1.0f, 0.0f, 0.0f);
glTranslatef(x, y, -20.0f);

glBegin(GL_POINTS);
glVertex3f(-5, -5, 10.0f);
glEnd();

gluSphere(qobj, 1.0f, 20, 20);
glPopMatrix();
```
```

Also what are the differences between the different coordinate spaces e.g. world, object etc.

This is mostly a matter of convention, but:

• Model space (= local space) is the coordinate space of a specific model, relative to its "center". If you have a file with a model, the coordinates are centered around some point of it (e.g. it's geometrical center, its base, anything actually).

• Scene space (= world space) is the coordinate space relative to an arbitrary point of your scene

• Eye space (= view space) is the space where the camera is at point `(0,0,0)`, `x` faces right, `y` faces up and `z` faces out of the screen (`-z` = deeper)

• Clip space is where `(-1,-1,*)` is the bottom left corner of the viewport, `(1,1,*)` is the top right corner of the viewport, and the Z coordinate in (-1,1) indicates just the depth (again smaller Z = deeper). (Fragments

• Screen space (= window coordinates) is the same as above, except that the coordinates are rescaled from `-1..1` to pixel-based values matching the range of the current viewport and depth range.

You transform coordinates from model space to scene space by multiplying (in OpenGL conventions usually left-multiplying) by a model matrix (which contains the information on where the model is on the scene). If you have a scene hierarchy, there can be many "stacked" model matrices for an object (placement of the sword relative to an arm, arm relative to a knight, knight relative to the scene).

Then you transform the coordinates to eye space by multiplying by a view matrix (usually connected to a "camera" object).

After that, using a projection matrix you transform those coords to the screen space, so that OpenGL would map these coords to actual screen pixels (depending on the viewport setting).

Some facts:

• Model and view matrices usually contain translation, rotation and/or scaling, while projection matrix usually contains a perspective transformation, which makes the objects further from the screen appear smaller.

• Old OpenGL (2.x and earlier) required you to put the matrices on two "matrix stacks":

• GL_MODELVIEW stack which should contain View*Model (or View*Model1*Model2...*ModelN),
• GL_PROJECTION stack which sould contain only the Projection matrix.

These could just as well be single matrices, not stacks, but the stack (along with `glPushMatrix` and `glPopMatrix`) was introduced to let the programmer "save and load" them easily. Only the "topmost" matrix from each stack is used in calculations.

The projection matrix is usually created with `gluPerspective` or equivalent. The view matrix can be made with `gluLookAt` (or similarly to model matrices), and the model matrices can be easily assembled using `glTranslate`, `glRotate` and `glScale`.

(note: OpenGL 3.1+ removed these features, allowing you to use any matrices and any conventions you prefer)

Knowing that:

I want to be able to get the coordinates of an object (e.g. triangle) after it's been translated and rotated, the reason i want to do this is so that later i can do collision detection and calculate the distance between objects using the coordinates

A reasonable way to calculate all your physics is to do them in scene space.

Hence if you have a model (e.g. a triangle mesh), to obtain the position of any its vertex in scene space, you need to left-multiply it by only the model's model matrix (or in case of the hierarchy, by all its model matrices).

About `gluProject`, in case you wondered- it is a convenience method which allows you to multiply a set of coordinates by the current `PROJECTION*MODELVIEW` and performs viewport transformation to see where it would end up in screen space, and `gluUnProject` does the reverse.