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.

Newcomers DBPro Corner / How do you make gravity?

Author
Message
WoW is WOW
21
Years of Service
User Offline
Joined: 21st Jun 2003
Location: Australia
Posted: 20th Oct 2003 11:18
In a game, all things(unless it's a space game) are affected by gravity, so how do you simulate correct gravity in your game.
thanx.
RPG_Freak.
adr
21
Years of Service
User Offline
Joined: 21st May 2003
Location: Job Centre
Posted: 20th Oct 2003 12:21
Every object I want to be affected by gravity has a velocity type in it. You needn't do it like this, but I actually can't imagine doing it any other way ...

define your types ...



and affect the velocity in your main loop...



Basically, the idea here is to create an increasingly negative value, and change your player's height by that amount. Because you're decrementing your y velocity by a constant every time, you get a very simple, but effective downward vertical acceleration effect...

ta da.

Can I ask you a Question?
What is it?
It's an interrogative form of sentence, used to test knowledge. But that's not important right now.
Darkflame
21
Years of Service
User Offline
Joined: 4th Jul 2003
Location:
Posted: 20th Oct 2003 15:38 Edited at: 20th Oct 2003 15:40
Yes thats the best way.

If you dont need accurate gravity you could just subtract a fix amount from a co-ordinate at set times. At no point needing a speed per object..mearly set up a loop to subtract from all objects nesscery.

This would be good enough, for say, Tetris, or other games where you need things to fall but not to accerate when falling.

Then again, true acceleration might add something to these games too
--
adr~ Great sig, Leslie Nielsen rules.

"The man who says it cannot be done is generally interrupted by someone doing it." ~ Elbert Hubbard
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 20th Oct 2003 16:59
"We need to get this man to a hospital"
"Why, what is it"
"It's a large building with patients and doctors, but that's not important right now"



"...And don't call me Shirley".

But to the point...do a search for "Physics", there's a couple of threads on here somewhere

BatVink (formerly StevieVee)
http://facepaint.me.uk/catalog/default.php
GameKit
21
Years of Service
User Offline
Joined: 6th Mar 2003
Location: USA, Staring Blankly at a Computer
Posted: 20th Oct 2003 17:40
If i remember right, perfect gravity would be fall speed * 1.9 every second... so if your game were running at sixty frames per second it would be falls#=falls#*(1.9/60)... so i guess the best way to do it would be... falls#=falls#*(1.9/screen fps())...well...this is how it works in reality, but some games you would want a slower fall rate... in that case just increase 1.9 into 1.95 ect...

try inserting something like this into your main loop...


well... i hope that helps...

If It Wern't For Bad Apples, Good Apples Would Only Be Average...
Hamish McHaggis
21
Years of Service
User Offline
Joined: 13th Dec 2002
Location: Modgnik Detinu
Posted: 20th Oct 2003 19:18
There is a very simple example on my site (click the site button below).

Brains are for idiots.

Athelon XP 1400 Plus - Nvidia Geforce MX400 - 256mb RAM
zircher
21
Years of Service
User Offline
Joined: 27th Dec 2002
Location: Oklahoma
Posted: 21st Oct 2003 03:32
If one G is 32 feet per second per second, why 1.9? Should it not be 9.8 as in meters? [Assuming 1 unit is equal to 1 meter...]
--
TAZ
GameKit
21
Years of Service
User Offline
Joined: 6th Mar 2003
Location: USA, Staring Blankly at a Computer
Posted: 21st Oct 2003 17:25
hmmm... taz, i think your right... its 9.8 instead of 1.9...my bad...

1.9/60=0.031... my code wouldn't work, the player wouldn't fall
9.8 should be correct...

If It Wern't For Bad Apples, Good Apples Would Only Be Average...
Hamish McHaggis
21
Years of Service
User Offline
Joined: 13th Dec 2002
Location: Modgnik Detinu
Posted: 21st Oct 2003 18:39
But often in games, 1 unit doesn't equal 1 metre .

Brains are for idiots.

Athelon XP 1400 Plus - Nvidia Geforce MX400 - 256mb RAM
Algae Man
21
Years of Service
User Offline
Joined: 11th Nov 2002
Location: Canada
Posted: 22nd Oct 2003 01:58
Yes, using '9.8' as the acceleration is really pointless. It could be any arbitrary amount, depending on the scale of your objects.
zircher
21
Years of Service
User Offline
Joined: 27th Dec 2002
Location: Oklahoma
Posted: 22nd Oct 2003 06:34
That's why I put that 'asuming' clause in there. However, you would need to multiply 9.8 by whatever scale that you happen to use. For example, 1.9 would be about right if one unit was equal to 5 meters.
--
TAZ
Darkflame
21
Years of Service
User Offline
Joined: 4th Jul 2003
Location:
Posted: 22nd Oct 2003 14:31
9.81 is essiental because to have accurate gravity it is that much per-second-per-second.

An arbitary amount simply wont do if your dealing with any degree of temporal precision grater then 1 second.

v(t) = v(0)-9.81*(t^2)

v(t) = Velocity at time t.
v(0) = velocity at time 0.
-9.81 = rough value for G. (ie the acceleration).

"The man who says it cannot be done is generally interrupted by someone doing it." ~ Elbert Hubbard

Login to post a reply

Server time is: 2024-09-21 05:21:44
Your offset time is: 2024-09-21 05:21:44