Interesting article about gravity in games

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Interesting article about gravity in games

Post by kikito » Wed Dec 19, 2012 11:53 am

substitute541 wrote:Just a note. It is still inaccurate for VERY low FPS. Just look at the new algorithm's picture, the 3 FPS example didn't even reach the correct position.
The point of the article was not "making your game perfectly accurate with very low FPS". It was "make your game more accurate in general". The "exactitude of the aproximation" is dependent on the framerate, the velocity of the object in question, and the distance travelled. What the article says is that fast, force-driven things, moving long distances, will be affected by framerate no matter what. In a combat game with canons, a decrease from 120 to 115 FPS can make a shot fail by several meters (provided that on the speed and the distance the canonballs have to travel are high enough). With this method, a decrease from 120 to something like 20FPS would still hit the target correctly.

Yeah, with 3FPS it will fail. But that's not the point.
When I write def I mean function.

User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Interesting article about gravity in games

Post by vrld » Wed Dec 19, 2012 4:18 pm

Apart from the variable timestep, this method looks a lot like leapfrog integration. The idea behind that method is to interleave evaluation of position and velocity. Position is evaluated at time t, whereas velocity is evauated at time t + 0.5:

Code: Select all

x = x + v * dt + .5 * a * dt * dt
local a_last, a = a, acceleration(x)
v = v + .5 * (a_last + a) * dt
This gives a very stable result (preserves energy) and is reversible (you can calculate this backwards and reach your starting point). However, it only works for fixed time steps.

I've attached a simple test showing different integration methods. You can see that both variable and fixed timestep overshoot the true motion when position and velocity is evaluated at the same time. Interleaving evaluation approximates the parabola relatively closely (ignore that approximations are longer than the true curve - this is an artifact of large delta times).
Attachments
integration.love
(852 Bytes) Downloaded 72 times
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine

User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Interesting article about gravity in games

Post by Roland_Yonaba » Wed Dec 19, 2012 5:17 pm

Kikito, that was a very interesting find.
I like this kind of lectures actually, as i'm fascinated on how maths/physics equations applied to games/simulation can produce astonishing results :)

Actually I have been working on a little demo yesterday, and today. The main idea was
  • Propose a model/template showing how one can use physics-based movement and include them easily in a game (platformers-like) to achieve realistic movement and jump.
  • Show the difference between various time - integration methods, with a constant acceleration. The first one presented in Niksula's post is Implicit Euler, the most simple one. The second method looks like Verlet (also called Leapfrog, as Vrld said).
So in this demo, you can move those characters with directional arrows, make them jump with space key, cap down/cap up the framerate between 300 and 3 fps, and see the results on the characters jump curve. As said in Niksula's post, the lower the framerate is, the lower the jump curve gets with Implicit Euler integration method.

Others integrators I tried to include are Explicit Euler, Improved Euler, Heun, 4th-order Runge Kutta and Verlet.
And for those who might be asking, the physical mode I set takes into account mass property, and damping. Velocity is clamped to a max value, see player.lua.

The git repository can be found here: Character-physics
I attached a love file for anyone who's willing to try. I may have made wrong things, in this case i'll be happy to have feedback.
Attachments
test.jpg
Screenshot
test.jpg (45.13 KiB) Viewed 1631 times
character-physics-demo.love
Character-Physics-demo
(13.81 KiB) Downloaded 73 times

User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Interesting article about gravity in games

Post by kikito » Wed Dec 19, 2012 5:42 pm

I forgot to mention this on my previous comment: I'm loving the demos you guys are coming up with. And then I saw Roland's, which is great!

Bringing this article up here has been certainly worth it! :awesome:

(Roland, I suggest you start with the framerate capped, so that the difference is visible right at the beginning).
When I write def I mean function.

User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Interesting article about gravity in games

Post by Roland_Yonaba » Wed Dec 19, 2012 6:00 pm

kikito wrote:Roland, I suggest you start with the framerate capped, so that the difference is visible right at the beginning.
Great Idea :awesome:
Attachments
character-physics-demo.love
Framerate capped to 3fps at the beginning.
(13.82 KiB) Downloaded 61 times

User avatar
Puzzlem00n
Party member
Posts: 171
Joined: Fri Apr 06, 2012 8:49 pm
Contact:

Re: Interesting article about gravity in games

Post by Puzzlem00n » Thu Dec 20, 2012 2:53 am

Excellent read, really. Reminds me of an almost, but not quite, entirely unrelated question I had on entities updating at different rates despite being fed the same dt... I should really make a thread for it already.
I LÖVE, therefore I am.

User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Interesting article about gravity in games

Post by BlackBulletIV » Thu Dec 20, 2012 11:04 am

kikito wrote:Yeah, with 3FPS it will fail. But that's not the point.
At 3 FPS I think player error would be an even bigger concern; you could hardly hit anything at such an abominable framerate, even if the physics were accurate.

User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Interesting article about gravity in games

Post by Roland_Yonaba » Thu Dec 20, 2012 11:08 am

Hi all,
It's me, bothering the whole world again.
So I spent a little bit more time on it, and I added some juicy features to the original demo.
Curve plotting, in fact.
So you can compare the accuracy of the different time-integration methods at various framerates when making a jump.
Attachments
character-physics-demo-r2.love
Demo - r2
(14.75 KiB) Downloaded 196 times
50fps.jpg
50fps
50fps.jpg (17.3 KiB) Viewed 1592 times
3fps.jpg
3fps
3fps.jpg (19.62 KiB) Viewed 1592 times

User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Interesting article about gravity in games

Post by micha » Thu Dec 20, 2012 12:44 pm

Roland, I am courious: In the first picture of your post I can see the different paths traveled by the blocks. I expected them to be parabolas (and hence symmetric), but they don't seem to be.

Why is that? Did you implement some sort of air friction?

User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Interesting article about gravity in games

Post by Roland_Yonaba » Thu Dec 20, 2012 1:02 pm

Totally!
Damping, in fact, and it acts upon velocity:

Code: Select all

vel = vel + acc*dt * (damping ^ dt)
See integrators :crazy:

Post Reply

Who is online

Users browsing this forum: casual2 and 19 guests