Advertisement

Need some simple movement help.

Started by September 16, 2000 03:16 AM
10 comments, last by ichor 24 years, 2 months ago
Hello all! I''ve been trying to start from scratch making a program that just allows for movement around a cube. Sounds simple enough, really. So far, I''ve got the forward and right movement to work, but when they work, the backward and left don''t work. I''ve taken a look at Tutorial 10, and it didn''t really help me out much... Does anybody have a VERY simple program (as in, the program is tiny) that they could send me? My program is structured as: winmain.cpp, winmain.h opengl.cpp, opengl.h cube.cpp, cube.h Cube is just a simple cube, nothing special. Opengl.cpp initializes and gets OGL running, and winmain is the framework for the application. I thought (and still think) that once I get one cube on the screen and able to move around it, I can take the code there and kind of use it to go further. So far, I haven''t been that lucky. So, this is my desperate plea for help! I''m using the standard opengl stuff; no extra libraries or anything like that. Can anyone help? Some functional code would be best, but any tips are welcomed... If you have some code, you can email it to me at: ichor@sprintmail.com Thanks!
Since I don''t believe in feeding people code (mainly because when you copy and paste someone else''s code you don''t learn) I will give you the things you should have in your program, if they aren''t there already.

First of all, if you have taken a look at NeHe''s tuts, then you would most likely have a bool keys[256] array and a function to capture windows messages and do things with them.

Inside the message capture you need to have a switch/case structure with one of the cases being WM_KEYDOWN that will set the corresponding key in the bool array to be true. Also, you might want one that translates a WM_KEYUP message to make the key involved in the message to be false.

After these have been accomplished, you then add into your WinMain funcion:

[source]
if (keys[VK_LEFT]) // for other VK definitions, see the WinUser.h file in your VC98\Include directory.
execute some statements

etc.

If you don''t know how to get to NeHe''s tuts because you dropped into the forum without going through his page, the url is nehe.gamedev.net

Good luck.
S.
Advertisement
Actually, I already can capture the user input without problems. It''s the transformations and such that I''m not doing right somehow.

Also, how can you "not believe" in sharing code? Think of all the books that are "...by Example" and of that nature. They''re usually some of the best books because they give live code.

So...I''d still appriciate some live code of a simple program that allows movement.
I just don''t believe that code given to you will help you learn if you just copy and paste. Also, it''s better if the code has errors because then you try to fix them. Or at least SHOULD try to fix them on your own. If you already have the capture key messages then this should do it...

    // declare globally in opengl.hextern GLfloat xTrans, yTrans, zTrans;// in opengl.cppGLfloat xTrans = 0.0f;GLfloat yTrans = 0.0f;GLfloat zTrans = 0.0f;// in WinMain funcif (keys[VK_LEFT])  xTrans -= 0.5f;if (keys[VK_RIGHT])  xTrans += 0.5f;if (keys[VK_UP])  yTrans += 0.5f;if (keys[VK_DOWN])  yTrans -= 0.5f;if (keys[''S''])  zTrans -= 0.5f;if (keys[''A''])  zTrans += 0.5f;// in opengl.cpp render funcglTranslatef(xTrans, yTrans, zTrans);    


That should do it...

S.
Hey, thanks for that, but unfortunately, it looks almost exactly like what I have. Here are the differences:

in winmain.cpp, I capture the user input, and do:
moveForward(), moveBackward(), moveLeft(), or moveRight().

Then in opengl.cpp, each of those functions look about the same.


GLvoid moveForward(GLvoid)
{
z += .025f;
glTranslatef(x, y, z);
}

From what I can tell, your method and my method are pretty much the same. moveForward() brings everything closer, but moveBackward() leaves the existing cube , AND moves backwards. Same goes for left and right. moveLeft() works great, moveRight() doesn''t work at all.

Any thoughts on that?
You shouldn''t translate like that. If you have two keys held at the same time, it might not work the way you want it to. Plus it''s redundant in it''s calls. Your functions should only change the values then in the render function before you draw the shapes you should have the translate.

S.
Advertisement
Okay, I changed the increments and tranformation stuff to how you had it... It works exactly as how I had it. I''m really not sure what''s wrong.
Could you send me your code at cansoul@home.com? Maybe I could find it easier that way...

S.
Alrighty, figured out your problem... This is what your code should look like...

    while(!done){...  else  // there are no messages pending, execute de game loop  {    // Draw The Scene.    if ((active && !DrawGLScene()) || keys[VK_ESCAPE])    {      // if ESC was pressed, or DrawGLScene returned FALSE      done=TRUE;    }    else    {      SwapBuffers(hdc);		// Double Buffering    }  }  if (keys[VK_UP])  {    yTrans += .025f;  //increase # to move faster  }  if (keys[VK_DOWN])  {    yTrans -= .025f;  //increase # to move faster  }  if (keys[VK_RIGHT])  {    xTrans += .025f;   }  if (keys[VK_LEFT])  {    xTrans -= .025f;   }  if (keys[''A''])  {    zTrans += .025f;   }  if (keys[''S''])  {    zTrans -= .025f;   }}    
Alrighty, here''s the "why"... within your while(!done) loop you were calling DrawGLScene() too many times... It is called in the if ((active&& !DrawGLScene() ... line. That means you should not have been calling it when you were checking for a key = TRUE.

Secondly, your key checks should be done outside that if/else structure just to make things look nicer. That if structure is to only check if the scene was rendered or an ESC msg was recieved. That''s all it should be doing... rendering and swapping buffers.

S.

This topic is closed to new replies.

Advertisement