Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (580) Games in Android Showcase (162) games submitted by our members Games in WIP (632) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Proper Way To Handle Gravity?  (Read 805 times) 0 Members and 1 Guest are viewing this topic.
orange451

JGO Coder

Medals: 20
Projects: 2
Exp: 5 years

 « Posted 2013-12-04 17:02:45 »

I have been thinking about a proper way to handle gravity in a 3d shooter.

Imagine your character is 16 units in height. The average human is say... 2 meters tall. This means that 1 meter is 8 units. the ratio is 1:8.

If gravity is expressed as 9.81 m/s^2 I believe then it would be equal to 78.48 units/s^2

However, this is only your acceleration per second...

If a game is running at 60 frames per second, this is where I become quite shaky, because you cannot just divide the gravity (78.48) by 60, and treat it as normal...

Assuming this conventional way of handling gravity is correct:
 1  2 `velocity.z += gravity;z -= velocity.z;`

How would you take into account there being 60 calculations per second?

Yes, I have tried dividing the 78.48 by 60, but it does not feel correct...

Working on a nice FPS in lwjgl http://i.imgur.com/q4uFqBS.png
Feel free to message me if you're interested!
Danny02
 « Reply #1 - Posted 2013-12-04 18:27:47 »

First of all, why uses something as abstract as "units" anyways if you want to convert to meters.

Now about your code, no it is not correct and yes you can just divide the gravity by 60, because thats is normal.

 1  2  3  4  5  6  7  8  9  10  11 `g = 9.81 m/s^2v (m/s)//you can not add a acceleration to an velocity so ...Vd = A * tdm/s = m/s^2 * sA: in your case gtd: your time deltaVd: is your velocity delta`

Also, the following formula is more correct for a constant acceleration like gravity:
 1  2 `val deltaVel = GRAVITY * timeDeltaentityVelocity = (2 * entityVelocity + deltaVel) / 2`
orange451

JGO Coder

Medals: 20
Projects: 2
Exp: 5 years

 « Reply #2 - Posted 2013-12-05 04:15:39 »

That's exactly what I needed! Thanks!

Working on a nice FPS in lwjgl http://i.imgur.com/q4uFqBS.png
Feel free to message me if you're interested!
Damocles
 « Reply #3 - Posted 2013-12-05 06:06:33 »

2 meters tall on average?
Are you from Holland?

xsvenson
 « Reply #4 - Posted 2013-12-05 09:31:20 »

Also, don't forget

fun > correctness as such "pseudo realism" > "realism"

This is especially true if Your game has a physics simulator.
Noone plays Your game if it's not fun, but noone cares if You cheat on gravity and nudge the objects onto right path from time to time

“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
tyeeeee1
 « Reply #5 - Posted 2013-12-05 18:28:26 »

2 meters tall on average?
Are you from Holland?

People are pretty close to that in Canada as well.
kramin42
 « Reply #6 - Posted 2013-12-05 19:55:35 »

Also, the following formula is more correct for a constant acceleration like gravity:
 1  2 `val deltaVel = GRAVITY * timeDeltaentityVelocity = (2 * entityVelocity + deltaVel) / 2`

All this does is halve the gravity? The velocity verlet works well:
 1  2  3  4 `// acc_i is the acceleration before the timestep, acc_f is the acceleration after. If they are the same (constant acceleration) then use acc = acc_i = acc_f and:// vel += acc;pos += vel*t + 0.5*acc_i*t*t;vel += 0.5*(acc_i + acc_f)*t;`

Edit: forgot a t in the velocity formula.

"All science is either physics or stamp collecting." - Ernest Rutherford.
CodeGolf4k
M4nkala
orange451

JGO Coder

Medals: 20
Projects: 2
Exp: 5 years

 « Reply #7 - Posted 2013-12-06 02:01:18 »

All this does is halve the gravity? The velocity verlet works well:
 1  2  3  4 `// acc_i is the acceleration before the timestep, acc_f is the acceleration after. If they are the same (constant acceleration) then use acc = acc_i = acc_f and:// vel += acc;pos += vel*t + 0.5*acc_i*t*t;vel += (acc_i + acc_f) / 2;`

His was actually spot on.
I checked using d = (1/2g)*(t^2)

Working on a nice FPS in lwjgl http://i.imgur.com/q4uFqBS.png
Feel free to message me if you're interested!
kramin42
 « Reply #8 - Posted 2013-12-06 03:26:02 »

His was actually spot on.
I checked using d = (1/2g)*(t^2)

I'm not sure exactly how you checked it but his method:

dv = g*t
v = (2*v+dv)/2

is equivalent to:

v = (2*v+g*t)/2 = v + (g/2)*t

i.e. in 1 unit of time the velocity is changing by g/2, not g.

"All science is either physics or stamp collecting." - Ernest Rutherford.
CodeGolf4k
M4nkala
Danny02
 « Reply #9 - Posted 2013-12-06 08:37:48 »

arg sry, yes you are right I messed up, should have checked what I was writing.

The correct way of doing things with const acceleration is:
 1  2  3 `val oldVel = velocityvelocity += gravityAcc * dTimeposition += (oldVel + velocity) / 2 * dTime`

and found the old blog post I tried to remember the first time
http://lolengine.net/blog/2011/12/14/understanding-motion-in-games
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 Waterwolf (22 views) 2015-05-20 15:01:45 chrislo27 (26 views) 2015-05-20 03:42:21 BurntPizza (60 views) 2015-05-10 15:53:18 FrozenShade (46 views) 2015-05-07 09:11:21 TheLopais (209 views) 2015-05-06 13:36:48 TheLopais (193 views) 2015-05-06 13:35:14 TheLopais (200 views) 2015-05-06 13:33:39 TheLopais (217 views) 2015-05-06 13:32:48 TheLopais (215 views) 2015-05-06 13:31:28 ClaasJG (237 views) 2015-04-30 20:33:25
 Spasi 31x BurntPizza 17x theagentd 13x DavidBVal 13x ra4king 12x EgonOlsen 11x Husk 10x KevinWorkman 9x princec 8x scanevaro 8x KaiHH 7x opiop65 7x revers 6x Riven 6x SauronWatchesYou 5x Drenius 5x
 List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org