To achieve a jump height of 100 pixels in 0.1 seconds, and incorporate realistic-feeling gravity, we can use basic kinematic equations from physics.
✅ Desired Behavior:
- Peak jump height: 100 pixels
- Time to reach peak: 0.1 seconds
We'll calculate:
- Initial upward velocity (
jumpSpeed)
- Gravity value (
gravity)
🧮 Step-by-step Calculation
1. Jump Speed (initial vertical velocity)
Use the following kinematic equation:
[
v = \frac{2 \cdot \text{height}}{\text{time}}
]
This assumes you're calculating the initial velocity required to reach a height given constant acceleration (gravity), and the time provided is half the total jump arc (time going up).
But a more accurate way is to use:
[
\text{jumpSpeed} = g \cdot t
]
Wait — not quite. Let's clarify:
The correct formula for initial velocity when you know max height and gravity is:
[
v = \sqrt{2 \cdot g \cdot h}
]
But we don't yet know g, and we’re solving for both jumpSpeed and gravity.
BUT, we can use another approach:
We know:
[
h = v_{\text{initial}} \cdot t - \frac{1}{2} g \cdot t^2
]
At max height, final velocity is 0, and also:
[
v_{\text{final}} = v_{\text{initial}} - g \cdot t = 0 \Rightarrow v_{\text{initial}} = g \cdot t
]
Substitute into height equation:
[
h = (g \cdot t) \cdot t - \frac{1}{2} g \cdot t^2 = \frac{1}{2} g \cdot t^2
]
So:
[
g = \frac{2h}{t^2}
]
Then:
[
\text{jumpSpeed} = g \cdot t
]
2. Plug in your values:
Gravity:
[
g = \frac{2 \cdot 100}{(0.1)^2} = \frac{200}{0.01} = 20{,}000 \text{ pixels/sec}^2
]
Jump speed:
[
\text{jumpSpeed} = g \cdot t = 20{,}000 \cdot 0.1 = 2{,}000 \text{ pixels/sec}
]
✅ Final Values:
| Parameter | Value |
|---|
jumpSpeed | 2000 pixels/sec |
gravity | 20000 pixels/sec² |
💡 Implementation Tip (in pseudocode):
velocity_y = -jumpSpeed # assuming y increases downward
gravity = 20000
def update():
velocity_y += gravity * delta_time
position_y += velocity_y * delta_time
if position_y >= ground_level:
position_y = ground_level
velocity_y = 0
⚠️ Make sure your game loop uses delta time to make movement framerate-independent!
🔁 Optional Tweaks:
If the jump feels too stiff or snappy, you can:
- Reduce
jumpSpeed slightly (e.g., 1800)
- Reduce
gravity slightly (e.g., 15000–18000)
These values are quite high because we're working in pixels, not meters — many 2D games scale gravity to feel right rather than simulate real-world physics.
Let me know if you'd like help tuning it for feel!