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.

Code Snippets / [DBP] Time Based Physics for All Situations

Author
Message
Hamish McHaggis
21
Years of Service
User Offline
Joined: 13th Dec 2002
Location: Modgnik Detinu
Posted: 29th Dec 2004 22:15
After discussing the matter with Ric on the DBPro board, we discovered that time based physics gets rather complicated when you get past damping movements of objects (even that took a while to figure out), so I set of to find a better solution, and found this. RK4 integration. Although it sounds quite complicated, it isn't actually that hard to impliment (though understanding it is another matter, I certainly don't ).

It works by taking four measurements, using the integration function included in the code, and weighing the values to make a good estimate of what the position and velocity of the object will be after dt time. The great thing about it is that the movement is all based on forces, which can all be calculated in one line. If you write...

accel = 10

It will handle it for you, and make the object accelerate at 10 units. If you write...

accel = -vel*damping

It will handle it. If you write...

accel = -pos*spring - vel*dampings - force

It will handle it. I have even included air resistance in the demo, like this...

accel = -pos*spring - vel^2*airRes*vel/abs(vel) - vel*damping - force

It handles it very well, so long as the frame rate is above around 10. My demo is in one dimension, but two dimensions could be built in with a bit of tweeking. here is the code, press space to apply a force left...



Isn't it? Wasn't it? Marvellous!
Baggers
20
Years of Service
User Offline
Joined: 31st May 2004
Location: Yonder over dem dere hills
Posted: 30th Dec 2004 21:13
Heya man, looks interesting only thing is i copy the code into the editor click run and i get a "constant name 'vb' cannot share the name of a reserved word or command."
is this a stupid mustake of mine or shall i just go through and chang all the 'vb's into another name.
Thanks
Baggers
Hamish McHaggis
21
Years of Service
User Offline
Joined: 13th Dec 2002
Location: Modgnik Detinu
Posted: 30th Dec 2004 21:17
Are you using U5.7? Cos that problem was fixed in that upgrade. Otherwise, just make the constants variables, not constants.

Isn't it? Wasn't it? Marvellous!
Baggers
20
Years of Service
User Offline
Joined: 31st May 2004
Location: Yonder over dem dere hills
Posted: 2nd Jan 2005 23:40
Yeah i got 5.7 ill have a go at the suggestion, thanks.
Baggers
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 3rd Jan 2005 23:57
Great job Hamish. Still trying to get my head around the code, but hey - it works.

If anyone is still wondering 'what's the point of this?', then put simply, it ensures that your game will play in an identical way on someone elses computer.

The normal 'delta time' method which is often used to do this is probably fine in most cases, but if you are using values which need to be very precise, and where any inaccuracies would be compounded every loop, this is the method to use.




Login to post a reply

Server time is: 2024-11-23 17:26:37
Your offset time is: 2024-11-23 17:26:37