Skip to main content
GameDev.net gamedev.net
🔒 Locked

time-based movements

Started by s_cloudx Feb 5, 2003 at 8:02 PM 8 replies 2.4k views
Original Post
s_cloudx
s_cloudx
I''m having trouble trying to achieve the same movement in my two machines (a new one, and an old one). I read somewhere that the best way to synchronize them is to use time-based movements. The problem is, I don''t understand how it works. The problem I''m having the most trouble on is on my character''s jumping. Can anyone please help?
--------------------------Sony "Mr. Valdez" ValdezPinoyforum.net Technical EditorPinoyforum.net - the breeding ground of the Filipino Computer Talents
stustill
stustill
Well, time based movement is when you measure how long it was since you last moved the screen entities and based on the time you move them varying distances

The distance you move them is calculated by multiplying the speed at which you want them to move per second, by the time interval between the current time and the last time.

For example for something that should be moving 10 pixles/second, the start time is measured, and the scene rendered to the screen. Then the time is checked again, and the difference between the first and second time is calculated. Assuming the first time was 0 seconds, and the new time is 0.1 seconds, the time difference is 0.1 seconds. This is multiplied by the speed of 10, to give 1 pixel, so we redraw the scene moving the entity one pixel. Please note this is not a realistic example, but it should (I hope) explain the concept!

Now, next time we come to draw to screen, lets say the time is now 0.3 seconds. The difference since last time is therefore 0.2 seconds. Multiplying this by the speed of 10 gives 2 pixels.

From this we can see that although the time difference between the first and second iteration of the loop was doubled, we moved the entity twice the distance to make up for it.

As I say this is a basic example, but should highlight the concepts that you will require to implement time based movement into your game

Stuart
s_cloudx
s_cloudx
Okay, I understand some parts, but I''m still having trouble with my avatar''s jumping. If the fps is low, he jumps real high. If the fps is high, he jumps low. Help!
--------------------------Sony "Mr. Valdez" ValdezPinoyforum.net Technical EditorPinoyforum.net - the breeding ground of the Filipino Computer Talents
GeniX
GeniX
He jumps higher because the way you are doing your physics for him falling is possibly not taking into account the time that has gone by.

A possibly way of handling this is to take your time delta (how big your time step is) and calculate how far upwards the player must go based on his current velocity, and then also calculate how far down he should fall based on gravity.

The two can be subtracted to get his displacement. I think the following formula is correct (off the top of my head - if it doenst work, go look for a physics website):

displacement = initial velocity * time + 0.5 * gravity * time * time

(or: s = ut + 1/2(a)(t*t) )

Note that this forumla calculates how far up he moves (ut), and then adds on the displacement from gravity (the 0.5 * a * t * t). It assumes gravity is a negative number, and also that time is positive.

--

The only thing I can think of to look out for is that if your time jump is big enough, the displacement could tell the player to move through the floor. You will want to clamp his position to the floor if he tries to fall below it.

Hope this helps.



regards,

GeniX

www.cryo-genix.net

[edited by - GeniX on February 6, 2003 10:39:23 PM]
s_cloudx
s_cloudx
It doesn''t seem to work. When I press the jump button, I put my characer''s gravity to -0.42f and his accelerationg to 3.2f. But it''s still don''t work. Am I missing something?
--------------------------Sony "Mr. Valdez" ValdezPinoyforum.net Technical EditorPinoyforum.net - the breeding ground of the Filipino Computer Talents
MARS_999
MARS_999
Go here and get this code and use it for your timers
http://www.gamedev.net/reference/articles/article1382.asp
It works good for what I am doing. Plus it''s in a nice tidy class instead of twenty different functions and variables.
Trienco
Trienco
quote:
Original post by s_cloudx
It doesn''t seem to work. When I press the jump button, I put my characer''s gravity to -0.42f and his accelerationg to 3.2f. But it''s still don''t work. Am I missing something?


? gravity is an acceleration, while his movement is a velocity.
after a time step you would have a lot to calculate, as every single moment a changing velocity was applied. usually that should shout integration at you, but after some math you''ll end up with
genix formula and the result is what you add to the position.
how about outputting velocity and gravity*time*time each frame and have a look at the values? velocity should be getting smaller (and doing so faster each time), while the second part should (obviously) become bigger.

f@dzhttp://festini.device-zero.de
MARS_999
MARS_999
Ah yes, Calculus the math of infinite possibilities!!! Oops can you say dy/dx and 4x^2 = 8x!!!
s_cloudx
s_cloudx
quote:
Original post by Trienco
? gravity is an acceleration, while his movement is a velocity.



I stand corrected.

Anyways, I figured it out. I just put a new variable for the jumping character, JumpTime. When the jump button is pressed, JumpTime = 0, and then incremented by the timestep.

Thanks for the help!

--------------------------Sony "Mr. Valdez" ValdezPinoyforum.net Technical EditorPinoyforum.net - the breeding ground of the Filipino Computer Talents

Topic Locked

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

Sign in to reply to this topic.