🔒 Locked
speed problem
Advertisement
Original Post
ok well the speed a whitch my enemys are moving is 1 but thats too fast so of course i should make it less then one. there in lies the problem because i think that rects only hold short ints not floats so what should i do??
Or make the movement time based, so instead of saying move 1 unit per frame, you can say more X units per second.
You could also, if you want to keep things the way they are, create some sort of buffer/container that stores the location as a float, and copy that info into your rect struct every frame.
Matt Hughson
You could also, if you want to keep things the way they are, create some sort of buffer/container that stores the location as a float, and copy that info into your rect struct every frame.
Matt Hughson
i definatly dont want EVERY thing that slow if there was a way to optionaly delay certine enemy i would do that as it is it just WAY to slow
i was thinking using the method that you spoke of about the timmer i guess ill do that (i dont know why not i do that with all the other things) thanks alot guys[grin]
I think storing the position as a float, having a speed of 0.5, and then converting it into an integer for display purposes, is probably the best all round bet. Write your program the way you want it, then make it display the images, but don't restrict your internal logic to the resolution of pixels on the screen.
wow this thread was dead for a while thanks for the replys right now im doing a
number ++ every frame
if number = 2
{
number = 0;
move_enemy()
};
(of course that is sudo code
and im almost done (very close) and its worked pretty well so far but i would love to find a more effective method of timing
number ++ every frame
if number = 2
{
number = 0;
move_enemy()
};
(of course that is sudo code
and im almost done (very close) and its worked pretty well so far but i would love to find a more effective method of timing
Quote:
Original post by coderx04 Quote:
Original post by coderx04
Or make the movement time based, so instead of saying move 1 unit per frame, you can say more X units per second.
how to move X units per second actually? using the SDL_DELAY?
No. You need to determine how much time has passed since the last time the unit has moved. You then multiply that number by the 'units per second' variable of the object.
So say your object moves 3 units per second. If it has been 1 second since the last time the function was call, the object will move 3 units (1 * 3). Now if the frame rate is higher, and it's been 0.5 seconds since the last time the function was called, the the object will move 1.5 units (.5 * 3). And finally if the frame rate drops and it has been 2 seconds since the last frame, then the object will move 6 units this frame (2 * 3).
So what happens is the faster your game runs, the slower the object will move, and vise-versa. So no matter what speed the computer runs at, the game will always look the same (movement wise).
Now to actually get the delay... You need 2 functions:
void QueryPreformanceCounter(LARGE_INTEGER *x);
void QueryPreformanceFrequency(LARGE_INTEGER *x);
This is basically all you need. The first function tells you the current time in 'ticks'. While the second function tells you how many 'ticks' the computer running the program can fit into a second. So by dividing the current time in ticks by the number of ticks the computer can fit into a second, you get the current time in seconds!
Now if you do this every frame, and subtract the result of the current time in seconds from the result last frame, you get how much time has past since the last frame in seconds. And that's your multiplier for the stuff from above!
*phew*
Good luck!
Matt Hughson
You need to put in some time functions into your main game loop.
Play around with the 30. It determines how much wait there will be between each new tick. And remember this goes anywhere in your main game loop.
// Provide time delayUint32 now;static Uint32 next_time;now = SDL_GetTicks();if(next_time > now) SDL_Delay(next_time - now);next_time += 30;Play around with the 30. It determines how much wait there will be between each new tick. And remember this goes anywhere in your main game loop.
You are already using SDL, and while I realize that SDL_GetTicks() isn't as accurate as QueryPerformanceCounter, it should be fine for your purposes.
Store your onscreen entities' positions as floats, and convert to ints immediately before drawing. You can also store their movement speeds as floats to give you finer control over how they move.
You'll need two static vars for computing your delta time:
so in your drawing func, do something like:
that will ensure that movement speed will remain constant regardless of CPU speed or framerate, and you avoid waiting X number of ticks which delays EVERYTHING.
Store your onscreen entities' positions as floats, and convert to ints immediately before drawing. You can also store their movement speeds as floats to give you finer control over how they move.
You'll need two static vars for computing your delta time:
so in your drawing func, do something like:
void Draw(void ) { //(this is semi-pseudo code) static Uint32 last = 0; static Uint32 now = SDL_GetTicks(); // delta will be the amount of time passed in seconds (which in most cases will be a partial second. ie, at 60fps, this value will be .01 or thereabouts) float delta = (now - last) / 1000.0f; foreach (entity E) { E.x += E.speed * delta; } // draw entities here last = SDL_GetTicks();}that will ensure that movement speed will remain constant regardless of CPU speed or framerate, and you avoid waiting X number of ticks which delays EVERYTHING.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement