Java-Gaming.org
 Featured games (81) games approved by the League of Dukes Games in Showcase (497) Games in Android Showcase (114) games submitted by our members Games in WIP (563) 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 563 times) 0 Members and 1 Guest are viewing this topic.
orange451

Senior Member

Medals: 7
Projects: 1
Exp: 4 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

Senior Member

Medals: 7
Projects: 1
Exp: 4 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

Senior Member

Medals: 7
Projects: 1
Exp: 4 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.

 Add your game by posting it in the WIP section, or publish it in Showcase. The first screenshot will be displayed as a thumbnail.
 BurntPizza (24 views) 2014-09-19 03:14:18 Dwinin (39 views) 2014-09-12 09:08:26 Norakomi (68 views) 2014-09-10 13:57:51 TehJavaDev (93 views) 2014-09-10 06:39:09 Tekkerue (47 views) 2014-09-09 02:24:56 mitcheeb (68 views) 2014-09-08 06:06:29 BurntPizza (51 views) 2014-09-07 01:13:42 Longarmx (38 views) 2014-09-07 01:12:14 Longarmx (44 views) 2014-09-07 01:11:22 Longarmx (40 views) 2014-09-07 01:10:19
 BurntPizza 37x Riven 18x Rayvolution 18x ags1 16x princec 16x basil_ 16x KevinWorkman 15x LiquidNitrogen 12x theagentd 11x kevglass 11x nsigma 11x deathpat 10x HeroesGraveDev 9x The Lion King 7x TehJavaDev 6x PandaMoniumHUN 6x
 List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27Resources for WIP games2014-08-01 16:20:17Resources for WIP games2014-08-01 16:19:50List of Learning Resources2014-07-31 16:29:50List of Learning Resources2014-07-31 16:26:06List of Learning Resources2014-07-31 11:54:12HotSpot Optionsby dleskov2014-07-08 01:59:08
 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