# 3D moving

• 03-31-2005
3D moving
ok, this is more the math side of the translating

Now, if i remember correct from pre-cal..

you use A and D to rotate around the Y axis, and you get an angle..

and, if you want it to move in a certain angle, you have to add an X and a Z. And, i remember that (on a 2d plane) COS is the x and SIN is the y

well why doesn't moving around just work if you do

x+=cos(yrotation);
z+=sin(yrotation);

and then glTranslatef(x,0,z); (since, you don't want to move up or down the y axis, just forward)

BUT that does not work at all, the moving is completely off and i have no idea why

here are the actual equations.. if it helps at all
Code:

```                if (keys['W'])  //if pressing forward                 {                                                 xmov += sin(yrot) / 1000;    //figure out how much to move x                         zmov += cos(yrot) / 1000;    //figure out how much to move z                 }                 if (keys['S'])    // if pressing back                 {                         xmov -= sin(yrot) / 1000;    //figure out how much to move x                         zmov -= cos(yrot) / 1000;    //figure out how much to move z                 }                 if (keys['A'])    // if rotating left                 {                         if (yrot > 359.9)    //if the yrot is getting too big                                 yrot = 0;          //then reset it back to 0                         yrot+=0.14f;    //rotate it .14 deg                 }                 if (keys['D'])    //if rotating right                 {                         if (yrot < 0.1)    //if the yrot is getting too small                                 yrot = 359.9;      //then reset it                         yrot-=0.14f;    //rotate it .14 deg                 }```
and the translation

Code:

```        glTranslatef(xmov,0,zmov);      //translate it with the distances         glRotatef(yrot,0.0f,1.0f,0.0f);    //rotate it```
the rotation works, but the moving is weird.

**i'm moving a box i've created using 6 squares

and heres the question: why doesn't that work, and how do you actually do it?
• 03-31-2005
Perspective
what your doing doesnt really make sense.. the amount you translate should not depend on which way your box is facing (ie: the rotation). Try just incremeanting the translation variables by a constant amount.

edit: also, remember the cos and sin functions are expecting values in radians, not degrees! :)
• 03-31-2005
Quote:

Originally Posted by Perspective
edit: also, remember the cos and sin functions are expecting values in radians, not degrees! :)

OH MY GOD!

:) thank you very much, it actually works now...

geeze, thats all i needed, to convert deg to radians. wow, and i have been going crazy trying to figure how and why my equations are wrong

^edit

oh yeah, im getting a lot of 'warnings' and i like to have 0 errors and 0 warnings

Quote:

C:\Documents and Settings\Sebastian\My Documents\lesson06\lesson6.cpp(529) : warning C4305: '+=' : truncation from 'const double' to 'float'
Code:

`                                zmov += 0.005 * (float)cos(yrot*pio180);`
how to get rid of these warnings?
• 03-31-2005
CodeMonkey
Float constant, I think.
Code:

`zmov += 0.005f * (float)cos(yrot*pio180);`
• 03-31-2005
Hey, dif question

how do you set how much fps you want?

well what i mean is, on my comp, i get 999 fps. on a friend's, he gets lower..

so when i move the box on my comp, it goes nice and normal speed.

but on my friend's computer, the box moves sooooo slow, hes like wtf

how can i equal it out?
• 03-31-2005
Perspective
*click* , ah, i see what your doing. Ignore my "what your doing doesnt really make sense" comment.

As for controlling FPS, theres a good thread about that in the game dev forum.. don't have a link off hand, just search for it.
• 03-31-2005
k time for my new project:

im going to make a huge plane with grass texture, a small runway, and hell, why not, a small building/tower.

and im going to make it seem like a first person airplane simulation thing, with no hud though.

(as an expiriment, say, with camera)

well, the way camera works is you just move the whole world differantly. right? so when you press left, instead it rotates right, if you press forward, move the world backwards, etc

well, it sounded easy... but, of course, something goes wrong. the turning/rotating works fine and is ok, until i start to move forward/backward, and it seems like its not rotating around me anymore but still the point in the middle.

and it also doesn't move around me anymore, i don't know how it moves.

well, heres my code (again)

the input
Code:

```                        if (keys[VK_UP])                         {                                                         xmov -= 0.05f * (float)sin(yrot*pio180);                                 zmov -= 0.05f * (float)cos(yrot*pio180);                         }                         if (keys[VK_DOWN])                         {                                 xmov += 0.05f * (float)sin(yrot*pio180);                                 zmov += 0.05f * (float)cos(yrot*pio180);                         }                         if (keys[VK_LEFT])                         {                                 yrot-=0.54f;                         }                         if (keys[VK_RIGHT])                         {                                 yrot+=0.54f;                         }```
the translating/rotatin
Code:

```int DrawGLScene(GLvoid)                                                                        // Here's Where We Do All The Drawing {         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);        // Clear The Screen And The Depth Buffer         glLoadIdentity();                                        glTranslatef(0.0f,0.0f,0.0f);// Reset The View         glTranslatef(-xmov,-ymov,-zmov);         glRotatef(yrot,0.0f,1.0f,0.0f);                 glBindTexture(GL_TEXTURE_2D, texture[0]); // the GRASS texture!                 glBegin(GL_QUADS);                 glNormal3f( 0.0f, 0.0f, 1.0f); // i have no idea what this does, but it was in the tutorial so i decided to try                 glTexCoord2f(0.0f, 0.0f); glVertex3f(-512.0f, -1.0f,  -512.0f);                 glTexCoord2f(256.0f, 0.0f); glVertex3f(512.0f, -1.0f,  -512.0f);                 glTexCoord2f(0.0f, 256.0f); glVertex3f(512.0f, -1.0f,  512.0f);                 glTexCoord2f(256.0f, 256.0f); glVertex3f(-512.0f, -1.0f,  512.0f);                 glEnd();                 glBindTexture(GL_TEXTURE_2D, texture[1]); // the concrete/runway  texture!                 glBegin(GL_QUADS);                 glTexCoord2f(0.0f, 0.0f); glVertex3f(-10.0f, -0.9f,  -10.0f);                 glTexCoord2f(64.0f, 0.0f); glVertex3f(10.0f, -0.9f,  -10.0f);                 glTexCoord2f(0.0f, 64.0f); glVertex3f(10.0f, -0.9f,  128.0f);                 glTexCoord2f(64.0f, 64.0f); glVertex3f(-10.0f, -0.9f,  128.0f);                 glEnd();         return TRUE;                                                                                // Keep Going }```
• 04-01-2005