Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Gravity simulation  (Read 4198 times)

0 Members and 1 Guest are viewing this topic.

ormenbjorn

  • Newbie
  • *
  • Posts: 12
    • View Profile
Gravity simulation
« 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!
« Last Edit: August 02, 2016, 06:29:47 pm by ormenbjorn »

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Gravity simulation
« Reply #1 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?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

ormenbjorn

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Gravity simulation
« Reply #2 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.

Elias Daler

  • Hero Member
  • *****
  • Posts: 599
    • View Profile
    • Blog
    • Email
Re: Gravity simulation
« Reply #3 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.
« Last Edit: August 11, 2016, 11:37:57 am by Elias Daler »
Tomb Painter, Re:creation dev (abandoned, doing other things) | edw.is | @EliasDaler

 

anything