Some beginner questions.

Started by
3 comments, last by ELgitaro 16 years, 3 months ago
Hi guys, I am trying to write a code to display a mesh and move the camera. I would like to use the arrow keys and pan the camera. I am using DirectInput8 and using "GetDeviceState" to read the keys. However, when I press a key, it is read more than 100 times per frame or more and when I translate the camera, it flies away:) I searched the forum but couldn't find a solution to this problem. So: 1) what is the way to read the input from the keyboard? One thing that comes to my mind is to use a timer to update the input, say, 20 times per second, utilizing the WM_TIMER message, but I am unsure if this is the way to do it. 2) I also would like to read mouse input to rotate the mesh. Thus, I don't know if the same thing is going to happen. I mean the mesh is going to rotate 100 times. 3) Another noob question: What is the way to translate or rotate a pre-existing mesh which is already rendered? I couldn't find a specific function in the SDK like mesh->Rotate(D3DX_PI/4). A solution might be to transform the world matrix, rotate or translate the mesh object and transform the world matrix back to its initial value. Again, I am not sure if this is to way to do this, because it may slow down the game frame rate a lot considering the transformations for each object in the scene. Thanks a lot in advance!!!
Advertisement
1) It sounds like you are moving the camera whenever you detect a key press?
If so, that's bad ;)
Instead, when you detect a key-press, you should save the state of that key in an array (or similar). Then during your update/render cycle you can check the state of the keys using this array. Also, in your main update function, you should know the amount of time that has elapsed since the last update, and use this time value to scale the distance that the camera moves.
e.g. Here's a really quick and dirty example of what I mean:
bool g_keys[256];void OnKeyPress( char k, bool downOrUp ){ g_keys[k] = downOrUp;}void main(){  bool quit = false;  float lastTime = 0;  while( !quit )  {    if( g_keys['Q'] )      quit = true;    float time = GetTime();    float delta = time - lastTime;    lastTime = time;    Update( delta );    Render();  }}void Update( float delta ){  const float speed = 1.0f;  if( g_keys['W'] )    camera.position.x += speed * delta;}

Thanks for the reply Hodgman. That explains the answer for my first and second question. What about my 3rd question?
Save the world matrix, either manually or in a D3DXMatrixStack, transform until happy, pop/restore the world matrix.

To make it is hell. To fail is divine.

Thanks Zao for the reply. I will see what I can do.

This topic is closed to new replies.

Advertisement