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

JGO Kernel

Medals: 381
Projects: 6
Exp: 7 years

Your face? Your ass? What's the difference?

 « 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...

First Recon. A java made online first person shooter!
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 Kernel

Medals: 381
Projects: 6
Exp: 7 years

Your face? Your ass? What's the difference?

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

That's exactly what I needed! Thanks!

First Recon. A java made online first person shooter!
 Games published by our own members! Check 'em out!
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 Kernel

Medals: 381
Projects: 6
Exp: 7 years

Your face? Your ass? What's the difference?

 « 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)

First Recon. A java made online first person shooter!
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

 EgonOlsen (37 views) 2018-06-10 19:43:48 EgonOlsen (20 views) 2018-06-10 19:43:44 EgonOlsen (39 views) 2018-06-10 19:43:20 DesertCoockie (186 views) 2018-05-13 18:23:11 nelsongames (122 views) 2018-04-24 18:15:36 nelsongames (121 views) 2018-04-24 18:14:32 ivj94 (861 views) 2018-03-24 14:47:39 ivj94 (122 views) 2018-03-24 14:46:31 ivj94 (758 views) 2018-03-24 14:43:53 Solater (139 views) 2018-03-17 05:04:08
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 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