Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (517)
Games in Android Showcase (123)
games submitted by our members
Games in WIP (578)
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 604 times)
0 Members and 1 Guest are viewing this topic.
Offline orange451

JGO Coder


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

Working on a nice FPS in lwjgl Smiley http://i.imgur.com/q4uFqBS.png
Feel free to message me if you're interested!
Offline 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 Smiley 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^2

v (m/s)

//you can not add a acceleration to an velocity so ...
Vd = A * td
m/s = m/s^2 * s

A: in your case g
td: your time delta
Vd: is your velocity delta


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

JGO Coder


Medals: 13
Projects: 2
Exp: 4 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! Smiley

Working on a nice FPS in lwjgl Smiley http://i.imgur.com/q4uFqBS.png
Feel free to message me if you're interested!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Damocles
« Reply #3 - Posted 2013-12-05 06:06:33 »

2 meters tall on average?
Are you from Holland?

Offline 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
Offline 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.
Offline 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 * timeDelta
entityVelocity = (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
Offline orange451

JGO Coder


Medals: 13
Projects: 2
Exp: 4 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)

Working on a nice FPS in lwjgl Smiley http://i.imgur.com/q4uFqBS.png
Feel free to message me if you're interested!
Offline 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
Offline 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 = velocity
velocity += gravityAcc * dTime
position += (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.

DarkCart (13 views)
2014-10-31 21:44:48

DarkCart (16 views)
2014-10-31 21:43:57

TehJavaDev (40 views)
2014-10-27 03:28:38

TehJavaDev (30 views)
2014-10-27 03:27:51

DarkCart (44 views)
2014-10-26 19:37:11

Luminem (26 views)
2014-10-26 10:17:50

Luminem (30 views)
2014-10-26 10:14:04

theagentd (36 views)
2014-10-25 15:46:29

Longarmx (64 views)
2014-10-17 03:59:02

Norakomi (62 views)
2014-10-16 15:22:06
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!