PDA

View Full Version : Make an object face center of rotation



WillsB
03-22-2009, 02:32 AM
Hi all,

It might just be me being really stupid, as its 3AM, and ive been staring at this code for ages - im sure i must be missing something but what Ive got is a simple textured quad, which I have rotating around around a cicle (2d orbit), and I want the quad to always face the center of rotation. Ive tried all manner of things.

I tried rotating to face the center then translating, translating then rotating, Pushing a new matrix and then loading the identity then doing my roration, then translating it however this results in the objects orbiting the camera and you cannot rotate to see the obejct when it is behind you.

Heres my code:


gl.glPushMatrix();
//gl.glLoadIdentity();
float sunTranslateAmount;
sunTranslateAmount = simDaylightTimer.getSimTimeInSecs();
gl.glBindTexture(GL.GL_TEXTURE_2D, crate);
gl.glTexEnvf(gl.GL_TEXTURE_ENV,gl.GL_TEXTURE_ENV_M ODE, gl.GL_MODULATE);
gl.glRotatef(-90.0f,0.0f,1.0f,0.0f);
//gl.glRotatef(180+sunRotateDueToTimeOfDayFactor,1.0 f,0.0f,0.0f);
//gl.glTranslatef(510.0f,130.0f,527.0f);
//gl.glRotatef(90.0f,0.0f,1.0f,0.0f);
// gl.glTranslatef(50.0f,77.0f,0.0f);

//gl.glTranslatef(100*(float)Math.sin(sunTranslateAm ount*Math.PI/180)*5, 100*(float)Math.cos(sunTranslateAmount*Math.PI/180)*5,0.0f);
glut.glutSolidTeapot(100);
//glut.glutSolidTeapot(20);
gl.glBegin(gl.GL_QUADS);
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-25.0f, -25.0f, 25.0f);
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 25.0f, -25.0f, 25.0f);
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 25.0f, 25.0f, 25.0f);
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-25.0f, 25.0f, 25.0f);
gl.glEnd();
gl.glColor3f(1.0f, 0.0f, 0.0f);
gl.glPopMatrix();

Any ideas - im sure it must be really simple and im missing something!.
Thanks
Wills

WillsB
03-22-2009, 01:36 PM
Done it, It was just some maths I hadn't thought enough about. Was tricky but got there in the end :).

Thanks all,

Regards
Wills.