Jump to content
  • Advertisement
Sign in to follow this  
samst0r

Moving An Object in the Direction it's Pointing?

This topic is 2792 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'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
Advertisement
With the angle, you can get the facing vector. Multiply it by the desired speed and add that to its current position.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!