Confused about gravity and velocity - different units being added together

Hey there! I am getting a little confused about some stuff here.

  1. Why are we choosing to set gravity to 1?
  2. Why are we mixing units up?
    posY += velocity (pixels + pixels/second)
    and
    velocity += gravity (pixels/second + pixels/second^2)
    Is this just the physicist in me being particular about units?
    Also, how does the velocity += gravity work? Does the rectangle fall faster to the ground with each iteration of the loop?
1 Like

Hello!

  1. Since this is our own game world, we’re able to set Gravity to a value of 1, there’s no scientific reason for this.

  2. I don’t have an answer for the mixing of units, that might be better for @StephenUlibarri to answer. That said, you’re correct about how velocity += gravity works. We want Scarfy’s jump velocity to eventually be overcome by gravity and then fall faster towards solid ground each frame. Which is why we add it to his velocity, since velocity is used to change Scarfy’s position on the y-axis and a delta-y that is positive means that Scarfy moves down on the screen.

2 Likes

Hello!
@Tuomo_T has answered your first question, we’ve picked an arbitrary velocity, as our units of distance in this case are pixels, so really, there’s no real need to adhere to realistic values (9.8 m/s^2, for instance).

As for your second question::

We are updating our position in each iteration of the game loop (the while loop). We can think of this as a per-frame update, so really, our velocity is actually in pixels/frame. Since we are updating position each frame, we apply the change in velocity for a single frame.

Think of it in terms of real-life units. If you’re running at 1m/sec, each second, you’d add 1m to your position, wouldn’t you?

So each frame, we add the velocity for that given frame. So we’re not actually mixing units, but rather you could think of it this way: we’re applying the update for this frame, so the current frame “cancels” out that unit of frames in the denominator for the velocity.

Updating the velocity works the same way. Velocity changes each frame, and the acceleration is a rate of change in units of (pixels/frame)/frame. So the update to velocity constitutes a change in the velocity’s number of pixels/frame each frame. So since we’re updating the velocity for the given frame, that /frame denominator is “cancelled out” by the current frame.

And for your last question, that’s correct. Velocity has a constant acceleration applied to it. This is how objects fall in real life - their velocity changes at a constant rate of 9.8m/s per second. So each second, they are now falling 9.8m/s faster than they were the previous second.

This is why it’s so dangerous to jump from extreme heights. If we fell at a constant rate, we’d safely float to the ground… but this isn’t the case. We actually accelerate, and the higher we are, the more time we have to accumulate a more and more dangerous velocity before we… PLOP!

2 Likes

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms