PDA

View Full Version : Retrieve the center of a sphere

remedios79
12-19-2002, 04:08 PM
Hi everybody!

I'm painting a sphere in a 3D environment.
At the beginning my sphere center is in (0, 0, 0) and then I apply a glTranslate(x,y,z) where x, y, and z are based on the movement of the mouse (through the motion function).

Now, I want to retrieve where is the center of my sphere in world coordinates
(if I know where is the center I can look inside an octree and find what's inside the sphere).

How can I find the coordinates in the world system?

Thank you,
Remedios

kjackson
12-19-2002, 04:12 PM
In the motion function are you just translating over and over again? The RedBook says not to do that, you can get rounding errors. But anyway, I would suggest for your problem, don't translate when the mouse moves, instead, just add/subtract to two variables, x and y. Then in your rendering loop, just glLoadIdentity() and then glTranslatef(x, y). In this case, you can always find out where you are, because you are keeping track of it. And, it avoids just translating and translating, and never starting with a 'fresh' identity matrix.

remedios79
12-19-2002, 04:17 PM
I'm using three variables x, y, z, every time I enter the motion function I add new quantity to these variables, and then I apply only one glTranslate inside the DisplayFunction.

I'd like to know how to make the sphere moving as if it was "attached" to the mouse... but this is another problem.

I need to know the coordinates of the sphere center in the world system, and I have x,y,z .

How can I extract the coordinates?
Thank you

nexusone
12-20-2002, 04:57 AM
If you are doing only a translate to the sphere, then then x,y,z location is now the center of the sphere.

If I have a sphere it center 0,0,0 and I translate it 5, 0, 0.
Then its new center is 5,0,0 and I base this on the fact the each time we update the sphere location we do a glLoadMatrix() call at the start of the display() routine.

If we do not reset the matrix each time then the Translate becomes additive, take the example of translate 5,0,0.
First pass through display(): 5,0,0
Second pass: 10,0,0
Third pass: 15,0,0
etc.

Look at my clock example on my website, object motion and animation example:
http://www.angelfire.com/linux/nexusone/index.html

Originally posted by remedios79:
I'm using three variables x, y, z, every time I enter the motion function I add new quantity to these variables, and then I apply only one glTranslate inside the DisplayFunction.

I'd like to know how to make the sphere moving as if it was "attached" to the mouse... but this is another problem.

I need to know the coordinates of the sphere center in the world system, and I have x,y,z .

How can I extract the coordinates?
Thank you

remedios79
12-20-2002, 09:21 AM
Thank you very much.

Now I wonder, if I apply other trasformations like glRotate and glScale, however, the center of my sphere is always in the last column of the modelview matrix?

I guess it is, but I'd like to be sure.

Thanks,

Remedios

jebus
12-20-2002, 11:06 AM
rotate and scale wont change your sphere's position. only translate.

jebus

Deiussum
12-20-2002, 11:52 AM
Originally posted by jebus:
rotate and scale wont change your sphere's position. only translate.

jebus

Not necessarily true. Depends on the order that he does the rotate/translate/scale

For instance if he does
glTranslatef(0, 1, 0);
glRotatef(90, 0, 0, 1);

The position will still be 0, 1, 0. But if he were to do
glRotatef(90, 0, 0, 1);
glTranslatef(0, 1, 0);

The position would now be -1, 0, 0 (or was that 1, 0, 0. I never remember if a positive rotate is CW or CCW)

remedios79
12-20-2002, 03:09 PM
So, what is the correct way to retrieve the center of my sphere?

If
glTranslate(x, y, z)
then the center of the sphere is exactly
C = (x, y, z)

But if
glRotate(a, axX, axY, axZ)
glTranslat(x, y, z)
how can I calculate the center C?

Thank you!

nexusone
12-21-2002, 05:39 AM
Then you have to use to math and 3D rotation is not a simple answer.

But take the distance of translation and the angle.

In 2D it is sometime like this:

x= distance * cos(angle);
y= distance * sin(angle);

Originally posted by remedios79:
So, what is the correct way to retrieve the center of my sphere?

If
glTranslate(x, y, z)
then the center of the sphere is exactly
C = (x, y, z)

But if
glRotate(a, axX, axY, axZ)
glTranslat(x, y, z)
how can I calculate the center C?

Thank you!