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.

DarkBASIC Professional Discussion / Coding gravity with timer() and Newton's laws

Author
Message
HartZa
21
Years of Service
User Offline
Joined: 5th Jun 2003
Location:
Posted: 7th Sep 2004 20:34
First of all, this thread has nothing to do with the Newton SDK!
Now, let's begin...
I have thinked the way of making nice, yet simple gravity using the laws Newton came up with. And by this, i mean the acceleration of objects coming down. All things that drop down, they will increase their speed by 10/s in a second. For example, a rock is dropped from 100m tall building. When it have dropped 5m, one second has gone, and the speed is 10m/s (36km/h). When two seconds have passed, the rock has dropped 15m, and the speed is 20m/s (72km/h), and so on.
When i thinked this through, i wondered how it could be used in the DBPro as a gravity. Then, i noticed the return integer-command TIMER(). If i got it right, timer() returns an integer of 1000th of a second. This means, the acceleration could be made very accurate.
The thing is, i'm not very pro in coding, so how could this be done?
I really hope someone would help me, and i hope we could all learn some physics by this thread. Hope so

Stats: Athlon XP Palomino 2600+ 2088mhz, GF4Ti420 128mb AGP8x, 512mb ddr dual-memory
dark coder
22
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 8th Sep 2004 01:14
its 9.8m/s/s

yes well there is currently a newton physics dll that incorperates all of these complicated physics matters aswell as collision so you should definentry check it out, coding your own physics engine would be rather tedious and time consuming especially when this dll is free i would advise anyone to use it.


Ian T
22
Years of Service
User Offline
Joined: 12th Sep 2002
Location: Around
Posted: 8th Sep 2004 01:15
get timer() once, store it as the base. Every loop, get timer() again, subtract it from the last timer () (or the original base one if it's the first run of the loop), and you have the difference in ms. Multiply it by 0.01 and you've got it in the proper fractional scale. Speed up the gravity of your falling objects by that times 10 every loop and you've got it working .

Nicholas Thompson
20
Years of Service
User Offline
Joined: 6th Sep 2004
Location: Bognor Regis, UK
Posted: 8th Sep 2004 01:15
There are two ways I do it.. Itterative (works by difference of time since last frame) and the other is time since start which I find easier.

TIME SINCE START:


Now that code works on these principles:
newPos = origPos + (Time * Speed)
newPos = origPos + (Time * Speed) + (Time * Time * Acc)

The first forumla represents constant speed and displacement, the second is used for speed, acceleration/retardation and displacement.

In my code, I assume X is positive to the right and Y is positive in the upwards direction (hence gravity being negative as it forces downwards).

Obviously with that code, you can then use the X# and Y# to position what ever you want (particle, objects, etc).
You can also use the second formula where wind is involved in the X. Simply set a variable for wind force and then assume it is an acceleration in a direction.

These get much more complicated when you start to involve things like wind resistance, friction, etc.

The other method is similar, except instead of taking the time since the start, you use time since the last frame and the foruma's are a little different as you need to work out the speed relative for the frame.



This works everything out relative to the last frame, not absolutely since the begining.
This changes the speed each time as the vertical component of the speed decreases due to gravity (in this case).

I think the above code is correct - I did it off the top of my head without checking.

I personally normally go for the first set of formula's as I think it would be more accurate to work out the position based on the time difference since the begining rather than itterative each frame. This is because any errors will build up in the itterative method, however in the absolute method the errors should be kept down. On the other side, the itterative method uses less variables and logically less memory (all be it next to no difference, but if you use this kind of principle and multilpy it by thousands, then you see my point). EG: A particle system with 1,000,000 particles, each particle having 2 excess floats and an excess integer then I think it works out at 6 bytes per particle and therefore 6,000,000 bytes of memory..

Just a thought!
Xander
22
Years of Service
User Offline
Joined: 3rd Mar 2003
Location: In college...yeah!
Posted: 8th Sep 2004 01:16 Edited at: 8th Sep 2004 01:25
Actually, you don't really need much for physics to do acceleration in a game. I figured it out when I was in 8th grade, and still use the same method.

Now, the method is easy, but getting the exact rate of acceleration could be difficult because you may not know the exact scale in your game. Lets just assume that one unit in your game is one inch, okay?

1 unit = 1 inch

We know that the acceleration of gravity is 32 ft/s/s (9.8 m/s/s). Here is a code snippet with gravity being applied to a variable, pos#:



That should work pretty well...Do you understand it?

Well, I didn't get the units perfect, but you will have to adjust them for the scale of your game anyway. Just adjust the acc# constant.

Xander Moser of Bolt Software
Firewall: Your Computer's First Defense - Real Time Strategy game
[href][/href]
Nicholas Thompson
20
Years of Service
User Offline
Joined: 6th Sep 2004
Location: Bognor Regis, UK
Posted: 8th Sep 2004 01:18
darn it all.. you beat me to it!
Xander
22
Years of Service
User Offline
Joined: 3rd Mar 2003
Location: In college...yeah!
Posted: 8th Sep 2004 01:26
Geez, I was fixing up some code for him, and you guys posted before I even got finished

Xander Moser of Bolt Software
Firewall: Your Computer's First Defense - Real Time Strategy game
[href][/href]
HartZa
21
Years of Service
User Offline
Joined: 5th Jun 2003
Location:
Posted: 8th Sep 2004 02:14
For being so accurate: it's 9,8322m/s/s at the poles, and
9,7804m/s/s at equator. There

How the timer() thingy exactly works? I haven't studied it. I just know that it can take time of something...

Bolt: Without testing or deeply looking into your code, how can that be applied to, for example, a simple "cube jumping on a plain object"-test?

Stats: Athlon XP Palomino 2600+ 2088mhz, GF4Ti420 128mb AGP8x, 512mb ddr dual-memory
Nicholas Thompson
20
Years of Service
User Offline
Joined: 6th Sep 2004
Location: Bognor Regis, UK
Posted: 8th Sep 2004 21:49
Firstly, the timer.
It returns a value in milliseconds.. I am not sure if that is the value since the start of the game running or the start of you loggin in to the PC or the date in milliseconds (as Java likes to do it, but in 10ms steps on WinXP, 5 in linux and 50 in Win98). Best way to handle that is to make a note of the start time just before your physics start and then find the difference between now and back then, divide by 1000 or multiply by 0.001 (same thing) and you have your time difference in seconds, hopefully in a float too if everything's gone well

To make a cube bounce on a plain - you need simpel collision detection.. Assuming the plain is flat and horizontal, its a simple:



Hope that all helps!
HartZa
21
Years of Service
User Offline
Joined: 5th Jun 2003
Location:
Posted: 9th Sep 2004 04:31
Umm... Could someone write the whole code? It's much easier to think this if i can test the program and see how it works.
Thanks for the one who makes it

Stats: Athlon XP Palomino 2600+ 2088mhz, GF4Ti420 128mb AGP8x, 512mb ddr dual-memory
Nicholas Thompson
20
Years of Service
User Offline
Joined: 6th Sep 2004
Location: Bognor Regis, UK
Posted: 9th Sep 2004 08:15
Here is an example.. a sphere bouncing on a plain.

Any questions - lob 'em here and I'll try to answer

Login to post a reply

Server time is: 2025-06-02 14:05:11
Your offset time is: 2025-06-02 14:05:11