Jump to content
  • Advertisement
Sign in to follow this  
ELgitaro

Some beginner questions.

This topic is 3991 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!!!

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
Thanks for the reply Hodgman. That explains the answer for my first and second question. What about my 3rd question?

Share this post


Link to post
Share on other sites
Save the world matrix, either manually or in a D3DXMatrixStack, transform until happy, pop/restore the world matrix.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!