Thread: 3D moving

  1. #1
    Flash Animator, OF DOOOOM bluehead's Avatar
    Join Date
    Nov 2001
    Posts
    269

    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?
    Code:
    #if _emo
      #define stereo_type_i_dislike
    #endif

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    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!

  3. #3
    Flash Animator, OF DOOOOM bluehead's Avatar
    Join Date
    Nov 2001
    Posts
    269
    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

    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?
    Last edited by bluehead; 03-31-2005 at 07:28 PM.
    Code:
    #if _emo
      #define stereo_type_i_dislike
    #endif

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Float constant, I think.
    Code:
    zmov += 0.005f * (float)cos(yrot*pio180);
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #5
    Flash Animator, OF DOOOOM bluehead's Avatar
    Join Date
    Nov 2001
    Posts
    269
    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?
    Last edited by bluehead; 03-31-2005 at 08:47 PM.
    Code:
    #if _emo
      #define stereo_type_i_dislike
    #endif

  6. #6
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    *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.

  7. #7
    Flash Animator, OF DOOOOM bluehead's Avatar
    Join Date
    Nov 2001
    Posts
    269
    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
    }
    Code:
    #if _emo
      #define stereo_type_i_dislike
    #endif

  8. #8
    Flash Animator, OF DOOOOM bluehead's Avatar
    Join Date
    Nov 2001
    Posts
    269
    Well, the way I restricted the fps is to put a Sleep(2); function at the end, so no matter how fast it goes, it still waits 2 milliseconds before it loops again... probably not the best way to go about things, but it's a lot better than to have it loop at speed of the computer
    Code:
    #if _emo
      #define stereo_type_i_dislike
    #endif

  9. #9
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    try rotating before translating.

    ::and for future reference, graphics related questions usually get more attention on the game programming board (it should probably be called the Game/Graphics Programming Board)

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    For camera classes and 3D questions check out the game programming board. Search for these in the board search and I think you will find what you want.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Moving objects with mouse in 3D.
    By psychopath in forum Game Programming
    Replies: 15
    Last Post: 07-10-2011, 04:20 PM
  2. Moving an object through 3D space
    By Canadian_coder in forum Game Programming
    Replies: 4
    Last Post: 11-22-2004, 01:04 AM
  3. 3D starfield
    By VirtualAce in forum Game Programming
    Replies: 6
    Last Post: 06-26-2003, 12:40 PM
  4. Moving a 3d object
    By Crossbow in forum Game Programming
    Replies: 4
    Last Post: 08-05-2002, 06:39 PM
  5. 3d engines
    By Unregistered in forum Game Programming
    Replies: 7
    Last Post: 12-17-2001, 11:19 AM