Sign in to follow this  

How to control an object in 3D space

This topic is 4342 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I think this is a basic question but here goes. I'm trying to move a simple object around the screen but I want it to move according to it's own axis. The object is a little car object and I want it to be able to turn and then translate in the direction it's facing. I believe I need to use vectors for this so I have written my own vector class but I'm not really sure how to use it to achieve the effect I'm looking for. Do I have to use the objects local coordinate system or something? Steve

Share this post


Link to post
Share on other sites
Ok, I guess I'll post some code:

The problem I have is when i translate (from the origin) along an axis and then try to rotate around the objects centre of gravity. Instead the object rotates in a big circle around the centre of the world coordinate system. I remember having a similar problem ages ago but I can't remember how I fixed it.

This is my main render function:

// This is the main drawing function which is called every frame...
void Render(CLOCK *time)
{
// First we need to clear the screen and the depth buffer...
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Reset the Modelview Matrix (start with a nice clean slate)
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

/****** STAGE 1: CALCULATE THE VIEWING TRANSFORMATION! ******/
glTranslatef(0.0f, 0.0f, -15.0f);

/****** STAGE 2: CALCULATE THE MODELLING TRANSFORMATION! ******/
Draw3D();
Draw2D(time);

/****** STAGE 3: FLUSH THE OPENGL GRAPHICS PIPELINE ******/
glFlush();
}



This is the Draw3D() function:

void Draw3D(void)
{
glDisable(GL_CULL_FACE);

glPushMatrix();

QuatRotation(); // Perform object rotation

// We need to find the direction vector from the object position to it's facing directon
// To do this we take the end point (N) and subtract the starting point (C).

//CVector N = camera_quat.GetAxis();

//CVector Delta = (N - position); // Calculate our new direction vector

//position += (Delta * 0.01f); // Increase the camera position in the direction we're facing
// N += (Delta * speed); // Increase the view position in the direction we're facing

// First we translate into position...
glTranslatef(position.x, position.y, position.z);


// Here we pass the 'modelview projection matrix' to our Cg program
cgGLSetStateMatrixParameter(modelViewProj, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);

// Next we enable our Cg vertex profile and bind the vertex program
cgGLEnableProfile(shader->vertexProfile);
cgGLBindProgram(basicProgram);

// Next we enable our Cg fragment profile and bind the fragment program
cgGLEnableProfile(shader->fragmentProfile);
cgGLBindProgram(fragmentProgram);

// Finally we display the light object
glBegin(GL_QUADS);

/*glColor3f(1.0f, 1.0f, 1.0f);
glVertex3f(0.0f, 0.0f, -1.0f); // LEFT
glVertex3f(-0.5f, 1.0f, 1.0f);
glVertex3f(-0.5f, -1.0f, 1.0f);

glColor3f(1.0f, 1.0f, 1.0f);
glVertex3f(0.0f, 0.0f, -1.0f); // RIGHT
//glColor3f(red, green, blue);
glVertex3f(0.5f, -1.0f, 1.0f);
glVertex3f(0.5f, 1.0f, 1.0f);*/


glColor3f(red, green, blue);
glVertex3f(0.5f, 0.8f, -1.0f);
glVertex3f(-0.5f, 0.8f, -1.0f); // TOP
glVertex3f(-0.5f, -0.8f, -1.0f);
glVertex3f(0.5f, -0.8f, -1.0f);

/*glColor3f(1.0f, 1.0f, 1.0f);
glVertex3f(0.0f, 0.0f, -1.0f); // BOTTOM
//glColor3f(red, green, blue);
glVertex3f(0.5f, -1.0f, 1.0f);
glVertex3f(-0.5f, -1.0f, 1.0f);*/


glEnd();

// Disable the vertex/fragment profiles so we can render the point light using ordinary T&L
cgGLDisableProfile(shader->fragmentProfile);
cgGLDisableProfile(shader->vertexProfile);

glPopMatrix();

glEnable(GL_CULL_FACE);
}

Share this post


Link to post
Share on other sites
I believe in the Draw3D function, you want to preform the rotations AFTER you translate to the correct position. So QuatRotation(), should go after glTranslatef(position.x, position.y, position.z);

Right now, you are not rotating around the cars axis, but whatever position is specified when you call glRotate in quatrotation(), and then, when you translate to your postition where the car is, it is still rotating around the old axis. You want to have your axis always be the car.

Share this post


Link to post
Share on other sites

This topic is 4342 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this