Sign in to follow this  
samst0r

Moving An Object in the Direction it's Pointing?

Recommended Posts

Hi,

I've been struggling for hours to get my model to move in the direction its pointing.

Basically I want the model to rotate using 'a' and 'd' which currently works
and then 'w' will move the model in the direction its currently pointing.

Heres my code (using OpenGL and C):

void display(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
// Clear frame buffer and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Set up viewing transformation, looking down -Z axis
glLoadIdentity();
gluLookAt(0, 8, 15, // eye level
0.0, 0.0, -10.0, // center
0.0, 1, 0.0); // up is in postivie Y direction

// Control Camera
glTranslatef(camera_xPosition, camera_zPosition, camera_zPosition);
glRotatef(camera_yRotate, 0, 1, 0);

// Render the scene

SetChromeMaterial();
// ROTATION AND MOVEMENT OF ROBOT
glPushMatrix();
drawRobot(robot_xPosition, robot_yPosition+5, robot_zPosition, robot_xRotate, robot_yRotate, robot_zRotate);
glPopMatrix();
drawFloor();

glFlush();
// Make sure changes appear onscreen
glutSwapBuffers();
}

void drawRobot(GLfloat x, GLfloat y, GLfloat z, GLfloat xang, GLfloat yang, GLfloat zang)
{
glPushMatrix();
glTranslatef(x,y,z);
glRotatef(robot_yRotate, 0, 1, 0);

glPushMatrix();
/* DRAWING STUFF WAS HERE */
glPopMatrix();

glTranslatef(-x,-y,-z);
glPopMatrix();

}


Can anyone point me in the right direction or help?

Thanks

Share this post


Link to post
Share on other sites
Quote:
Original post by samst0r
Hi,

I've been struggling for hours to get my model to move in the direction its pointing.

Basically I want the model to rotate using 'a' and 'd' which currently works
and then 'w' will move the model in the direction its currently pointing.

Heres my code (using OpenGL and C):

void display(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
// Clear frame buffer and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Set up viewing transformation, looking down -Z axis
glLoadIdentity();
gluLookAt(0, 8, 15, // eye level
0.0, 0.0, -10.0, // center
0.0, 1, 0.0); // up is in postivie Y direction

// Control Camera
glTranslatef(camera_xPosition, camera_zPosition, camera_zPosition);
glRotatef(camera_yRotate, 0, 1, 0);

// Render the scene

SetChromeMaterial();
// ROTATION AND MOVEMENT OF ROBOT
glPushMatrix();
drawRobot(robot_xPosition, robot_yPosition+5, robot_zPosition, robot_xRotate, robot_yRotate, robot_zRotate);
glPopMatrix();
drawFloor();

glFlush();
// Make sure changes appear onscreen
glutSwapBuffers();
}

void drawRobot(GLfloat x, GLfloat y, GLfloat z, GLfloat xang, GLfloat yang, GLfloat zang)
{
glPushMatrix();
glTranslatef(x,y,z);
glRotatef(robot_yRotate, 0, 1, 0);

glPushMatrix();
/* DRAWING STUFF WAS HERE */
glPopMatrix();

glTranslatef(-x,-y,-z);
glPopMatrix();

}


Can anyone point me in the right direction or help?

Thanks




What Tiblanc said, in short,


if ( 'W' Pressed )
{
float speed = 5; //or whatever number


//make sure your angle is in Radians
robot_velocity.x = cos( robot_yRotate) * speed;
robot_velocity.y = -sin( robot_yRotate ) * speed;

robot_xPosition += robot_velocity.x;
robot_yPosition += robot_velocity.y;
}






you'll have to convert your angle to radians if your using degrees however, its fairly simple, and a quick Google search will help if you don't already know how

Share this post


Link to post
Share on other sites

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