Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411426 Posts in 69363 Topics- by 58416 Members - Latest Member: JamesAGreen

April 19, 2024, 10:19:41 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperDesignMoving object with preset timelapse
Pages: [1]
Print
Author Topic: Moving object with preset timelapse  (Read 637 times)
DurianOdour
Level 0
*


View Profile
« on: October 23, 2017, 09:44:43 AM »

Hi, I am a newbie game developer.

I have one problem. Consider the following case.

I have a simple game with only one object - a ball. A ball can move only up and down from one side to another.

A User is allowed to set the game time  and ball iteration count. An iteration is when the ball goes up to the topmost point and goes back to the bottommost point.  

It should complete all iterations for the time user has set.  

Here is an example.

Consider that a user has set the game time to 60 seconds, and the ball iterations option to 30.

We can find how much time do we have for an iteration 60/30 = 2 seconds. So the ball should complete the iteration for two seconds.

Let's assume we have a known frame rate and it is 30ms (update and drawing). So a single loop iteration takes 2000ms.

Next find iterations per a complete ball movement 2000/30 = 66 iterations. Also let's imagine that a distance from one side to another is 2000px.

So the ball need to complete 4000px per iteration.  

Finally we can find out our movement delta 4000px/66 = 60 px . So ball will move 60px per a loop iteration.

But what to do if I don't frame rate, but what is more interesting what to do when the framerate is not static and we need to correlate it every time.

Please suggest an algorithm or an approach to solve this problem.

Thanks.
Logged
pmprog
Level 1
*


View Profile WWW
« Reply #1 on: October 30, 2017, 06:34:29 AM »

There's two ways.

1. Make your game run in fixed timesteps (ie, have a game framerate, which isn't directly connected to video framerate)
2. Divide your movement distance (4000px) by the number of ms to complete (2000ms) to give you a distance per ms. Then on each loop, find out how many ms you've been running (by using timers), then your movement delta = px/ms * frame ms
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic