View Full Version : distance beetween 2 sphere
10-12-2005, 04:17 PM
Hello, I have created 2 object with :
Obj = gluNewQuadric();
gluSphere(Obj, size, 50, 25);
now I can rotate/traslate and show they with :
glTranslatef(X, Y, Z);
glRotatef(Angle, 0.0, 1.0, 0.0);
How can get the X,Y,Z point after rotation/translation to calculate the distance ?
How can I get the spere on the mouse pointer ?
thank you very much for help
I am not clear about your First question
For the Second question you can call glutMouseFunc and glutMotion callbacks for selection or rotation of sphere
// glutMouseFunc call back routine
void mouse(int button,int state,int x, int y)
if(button == GLUT_LEFT_BUTTON)
if(state == GLUT_DOWN)
rotating = GL_TRUE;
xstart = x;
ystart = y;
rotating = GL_FALSE;
// motionFunc callback
void motion(int x, int y)
yAngle += (x - xStart);
xAngle += (y - yStart);
xStart = x;
yStart = y;
Center for AI and Robotics
Bangalore - 1
10-13-2005, 01:24 PM
Sorry for my english..
I have 12 sphere on the screen. Each sphere has a X,Y,Z when the program start. Each sphere is animated with an individual rotation and translation. So , after few frames I would like to know the X,Y,Z of each sphere to calculate the distance. I dont know how to get the X Y Z coords after a rotation or translation.
And I would like to insert in my program a mouse function, where if I left mouse click , my program give me the pointer to the selected spere, so I can know what spere is selected by the user.
I hope my english is better now.. sorry
Thank you very much
To keep it really simple, and since you're just rotating around the y-axis, we can dispence with the matrices and do something like
x' = x cos(angle) + z sin(angle);
y' = y;
z' = -x sin(angle) + z cos(angle);
This gives you a point rotated around the y-axis by some angle, centered about the world-space origin. If you want to locate the origin of rotation somewhere else, just add something to the rotated point, like an initial point (x0,y0,z0).
x' = x0 + x cos(angle) + z sin(angle);
y' = y0 + y;
z' = z0 - x sin(angle) + z cos(angle);
This is the same result you'd get with the matrices. Here's the equivalent matrix form, with the corresponding opengl calls in order:
glTranslatef(x0,y0,z0); glRotatef(angle,0,1,0); glVertex3f(x,y,z);
( x' ) ( 1 0 0 x0 ) ( cos(angle) 0 sin(angle) 0 ) ( x )
( y' ) ( 0 1 0 y0 ) ( 0 1 0 0 ) ( y )
( z' ) = ( 0 0 1 z0 ) (-sin(angle) 0 cos(angle) 0 ) ( z )
( 1 ) ( 0 0 0 1 ) ( 0 0 0 1 ) ( 1 ). Same result, and a lot easier to work with :-)
If you'd like some good reading on this stuff, google for "linear algebra". And look at the redbook appendices. There you'll find all the opengl transformation matrices and their inverses.
To get the distance between 2 spheres, take the distance between the sphere origins, then subtract the sum of the 2 radii from the result.
d = |origin1 - origin2| - radius1 - radius2.
If d == 0, the spheres touch; if d < 0, they intersect.
10-14-2005, 04:38 PM
Thank you for your answare. I know the math to do it, but if possible I want use the rotation and translation of opengl becouse I would like to learn how I can do it. I would like to know if there is a function to get X,Y,Z origin point after a rotation/translation without writing a 3D engine by myself.
Thank you very much
I would like to know if there is a function to get X,Y,Z origin point after a rotation/translation without writing a 3D engine by myself.There's no such function.
Multiplying a few matrices together is hardly writing a 3D engine ;-) You can grab the current modelview matrix with glGetFloatv(GL_MODELVIEW_MATRIX,m). Keep in mind that the camera transforms are likely on the modelview stack at the time the model transforms are applied, so don't hose your model transforms with the camera stuff.
Powered by vBulletin® Version 4.2.3 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.