SFML community forums

General => General discussions => Topic started by: ormenbjorn on August 02, 2016, 12:38:14 pm

Title: Gravity simulation
Post by: ormenbjorn on August 02, 2016, 12:38:14 pm
I just came up with a super easy way of simulating gravity:

vel.y = pushVel + gravityPull;
pushVel = vel.y;
rect.move(vel);

PushVel is the jump force when you want to jump(-50 for example) and gravityPull is something between 1-8, depending on how much gravity there should be. This concept works with both platformer gravity and if you want to simulate orbits and stuff!
Title: Re: Gravity simulation
Post by: Hapax on August 03, 2016, 09:08:07 pm
Why do you need PushVel at all? You could achieve the same with just the velocity and gravity:
vel.y += gravityPull;
rect.move(vel);
You would just set vel.y instead of setting PushVel.

gravityPull is something between 1-8
Why 1 and 8?
Title: Re: Gravity simulation
Post by: ormenbjorn on August 06, 2016, 10:42:31 am
Thank you! When i came up with this i did it for simulating orbits and that stuff, so my first thought was to seperate the "throw" velocity and the gravity velocity, because they affect each other. But this does the exact same thing and in less code, so thanks. I said 1-8 as an example.
Title: Re: Gravity simulation
Post by: Elias Daler on August 11, 2016, 11:32:09 am
Btw, you can do better by having gravity as acceleration.

v(t) = v0 + a * t
v(t + dt) - v(t) = v0 + a * (t + dt) - (v0 + a * t) = a * dt


Where a = g or some other value.

So, you can do this:
vel.y += a * dt;
That's pretty close to what you're doing, though this one supports variable time step.