movement

Started by
4 comments, last by the origin 22 years, 2 months ago
i want to move a sphere trough an terrain. the sphere is in 3dspace and i want to move it in viewing direction. when i rotate the sphere now i want to move in the new viewing direction. i made a little test with this code in an normal nehe code but it dont make what i want. glTranslatef(0.0,0.0,-z); gluSphere(q,1,20,20); glTranslatef(0.0,0.0,z); glTranslatef(0.0,0.0,-z); glRotatef(rot,0.0,1.0,0.0); glTranslatef(0.0,0.0,z); glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex3f(-10.0, 0.0, -10.0); glTexCoord2f(1.0f, 0.0f); glVertex3f( 10.0, 0.0, -10.0); glTexCoord2f(1.0f, 1.0f); glVertex3f( 10.0, 0.0, 10.0); glTexCoord2f(0.0f, 1.0f); glVertex3f(-10.0, 0.0, 10.0); glEnd(); glTranslatef(0.0,0.0,-z); glRotatef(-rot,0.0,1.0,0.0); glTranslatef(0.0,0.0,z); if(keys[VK_LEFT]) rot -= .01; if(keys[VK_RIGHT]) rot += .01; if(keys[VK_UP]) z += .01; if(keys[VK_DOWN]) z -= .01; please help me
Advertisement
You need to store the current x,y and modify them by (cos(rot), sin(rot)) when you try to move forward or backward.

Make a right clean schema (on paper!) of the general case (object not at origin, moving in a random direction) and figure out from it the transforms you need to view the object.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
oh i told it in a stange way

i want to make a carrace
and now i have an object on in the space
when i press forwd the object shold move forward.
now i rotate the object (drive left or right)
and now when i pres forwd i must have a new direction to move

but when i use the code i made the object rotates but the direction is still the same (the object drives with sidestep )

and now i need new help but in easy english please.

thanks

Edited by - the origin on February 16, 2002 2:13:28 PM
Check out NeHe''s Game Tutorial 7...
remember sin() and cos() use radians.. you can use this macro I made to convert your args to rads:


/*-------- Defines ---------------------------------------*/
#define PI 3.14159265

/*-------- Macros ----------------------------------------*/
#define NUM_TO_RAD(number) (number*PI)/180

// ..

/*..*/ sin( NUM_TO_RAD(90) ) /*..*/


- Sean
- -=/ Prell =- -
i tried it with sin and cos but i cant get a good movement

may somebody post the view lines of code thtat i can move over the QUAD and when i rotate i get a new direction to move
and when i move again in the new direction i can rotate again and move in the new direction again ?

  ........>O          '           O           '       '  ^            '   '     '             '        '          '    0.....>O       '     \/  

like a car forwd drive right; forwd; left ; forwd and so on.

thanks

Edited by - the origin on February 17, 2002 5:23:14 AM

This topic is closed to new replies.

Advertisement