Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Speed up physics?

Author
Message
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 25th Aug 2012 04:01 Edited at: 25th Aug 2012 04:03
Is there a way to change the speed of the physics? I'm probably not explaining that very well, but I have create a simple Newtons Cradle (screeny attached) that works great, but just very slowly. It's like watching it in slow motion.
Any suggestions on how to make it go faster?


Here is the code:



Attachments

Login to view attachments
mikkolah
11
Years of Service
User Offline
Joined: 13th Aug 2012
Location: Finland
Posted: 25th Aug 2012 09:21
You may use fixed time physics step to change simulation speed.

I prefer to use semi-fixed time step with physics, so simulation will look and feel pretty much same on all devices.

Code could be improved with smoothing by predicating next position/angle, but I haven't found it necessary yet
See: http://www.unagames.com/blog/daniele/2010/06/fixed-time-step-implementation-box2d

Here's slightly modified code with semi-fixed time step physics:
Digital Awakening
AGK Developer
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Sweden
Posted: 25th Aug 2012 09:41
Check this out:

StepPhysics

Description

Steps the physics simulation by a defined amount of time. This time value is in seconds and may include fractions of seconds. Stepping the physics simulation by a large time value (greater than say 0.1) may result in undefined behaviour and physics objects moving through each other. It is not required that you call this command in your game loop, if you do not call it, Sync or Update will call it for you with the last frame time to allow the physics simulation to catch up in real time. If you do call StepPhysics then it will not be called for you by Sync or Update for that frame, this allows you to do your own physics timing if you prefer.

By using a fixed time step every frame your physics will perform exactly the same across all devices and all frame rates, but a reducuction in fps will result in the physics appearing to go slower, as it will always step the same amount of time whether the frame was quick or slow to draw. Using a variable step for each frame will keep physics moving at the same speed regardless of frame rate, but it will no longer be deterministic across devices and different frame rates. For example if your game depends on a physics entity falling and bouncing to the same height each time it is run then you should use a fixed time step. If the position of physics objects is not important to you game logic then a variable time step may be best.

Definition

StepPhysics( time )

CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 25th Aug 2012 11:54
Perfect! Thank you

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 25th Aug 2012 18:27
Another option is to use setPhysicsScale at the start of your code (before creating any physics sprites). I prefer to let the physics system step the physics by the amount of actual time that has elapsed for consistency.


this.mess = abs(sin(times#))
mikkolah
11
Years of Service
User Offline
Joined: 13th Aug 2012
Location: Finland
Posted: 25th Aug 2012 19:42
Ok, I didn't know about setPhysicsScale, looks good
Thanks!

However, after fast test it looks like physics behaves differently with different FPS. For example, my character jumps differently depending on FPS, which is not acceptable.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 25th Aug 2012 20:18
Try using the SetSyncRate to set the minimum frame rate that works well for the app. This doesn't guarantee that all devices will display at that rate, but it can help.

Cheers,
Ancient Lady
AGK Community Tester
mikkolah
11
Years of Service
User Offline
Joined: 13th Aug 2012
Location: Finland
Posted: 25th Aug 2012 20:29
Yeah, It seems that best solution is to use fully fixed physics time step and design physics to some FPS, e.g. 60 if physics simulation must be accurate.

This way devices which cannot do 60 FPS will still have exactly same same physics simulation but it just runs slower. It good enough, in my opinion (at least in my case).

So, now I have SetSyncRate(60, 0) and StepPhysics(.035f) which i found best in my case
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 25th Aug 2012 21:31
The timer based stepping looks good to me. The game should run at the same speed regardless of framerate, which is ideal.

Login to post a reply

Server time is: 2024-05-02 11:57:53
Your offset time is: 2024-05-02 11:57:53