Skip to main content
gamedev.net
🔒 Locked

Movement Problem

Started by mekanikus Aug 1, 2006 at 9:43 PM 3 replies 900+ views
Original Post
mekanikus
mekanikus
hi ^^, the player movement in my game seems not smooth, i have set the keyboard's repeat rate to the fastest. but it didn't solve the problem. somewhere in my wndproc function. wndproc(...) . . . case WM_KEYDOWN: switch(wParam) { case VK_LEFT: Player.xPos -= 7; break; case VK_RIGHT: Player.yPos += 7; break; . . . } how to smooth the movement? thanks
panic1227
panic1227
//try this
//add these in the class of the Player
enum DIRECTION{ NONE,LEFT,RIGHT,UP,DOWN}
DIRECTION direction;

//then do as follow
wndproc(...)
.
.
.
case WM_KEYDOWN:
switch(wParam)
{
case VK_LEFT:
Player.direction = LEFT; //save the direction
break;
case VK_RIGHT:
Player.direction = RIGHT; //same
break;
.
.
.
}
case WM_KEYUP: //must...
Player.direction = NONE;
//....

//in a timer process or time related loop
switch(Player.direction)
{
case LEFT:
Player.xPos -= 7;
break;
//....
}

that's all.
AdamGL
AdamGL
The above post says it all. Basically, the frame rate varies each time, so the movement is processed at variable times. This means you get jagged movement. Find out what speed you want your guy to move, then divide that by the amount of pixels he needs to get to the next tile, and then multiply the seconds per frame by that amount...I think. Physics. If I'm wrong someone correct me. It's late.
bi-FreeSoftware
Limitz
Limitz
Quote:
Original post by AdamGL
The above post says it all. Basically, the frame rate varies each time, so the movement is processed at variable times. This means you get jagged movement. Find out what speed you want your guy to move, then divide that by the amount of pixels he needs to get to the next tile, and then multiply the seconds per frame by that amount...I think. Physics. If I'm wrong someone correct me. It's late.


Yeah thats correct, i actually posted this a while back at another post with a similar problem, maybe it helps...
You could substitute the GetKeyboardState function by your own wndproc loop if you insist :) And substitute "map" by "player".

This is untested code to display the idea behind a simple timebased movement.

Quote:
Original post by Limitz

I use this function get get al the keystates at once asynchronous.
void GetKeyboardState(int* keyboard){  register int i; for (i=0; i<256; i++) {    keyboard = (GetAsyncKeyState(i) ? 1 : 0);  }}


Now this function gives the time that has past between calls in milliseconds. For high end engines you'd use a performance timer, but lets keep it simple for now and use clock(). Be sure to include or
I am assuming that CLOCKS_PER_SEC == 1000, because i don't feel like writing to much code :p

void GetDeltaTime(int* dt){  static clock_t prev = clock(); //one time initialize, first call gives 0  clock_t current = clock();  *dt = current-prev; //set dt to ms that have passed  prev = current;}


Now lets say you want to use the cursorkeys to move your map. So we will be updating (x,y) and we want to move them by 50 pixels per second as long as the key is pressed.

void render(){  static int keyboard[256];  static int deltaTime;  static float pixels_per_ms = 50.0/1000.0;  static float x = 0, y = 0;  //get the keyboard state and delta time (using a pointer)  GetKeyboardState(keyboard);  GetDeltaTime(&deltaTime);  //calculate new coordinate  x += keyboard[VK_RIGHT] *pixels_per_ms*deltaTime;  x -= keyboard[VK_LEFT]  *pixels_per_ms*deltaTime;   y += keyboard[VK_UP]    *pixels_per_ms*deltaTime;  y -= keyboard[VK_DOWN]  *pixels_per_ms*deltaTime;   //now use these x,y to position the map}


This is how it would look like. Once again, i didn't compile it so i might have made a few mistakes, and you should update the getdeltatime function when you feel up to it, however... i hope this takes you in the right direction.

Greetings


Hope this helps...
Greetings.
mekanikus
mekanikus
thx for all of your helps. they help a lot ^^

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.