# How do I simulate a bouncing ball w/o physics or bullet

From the Asset Store
Bouncing ball
\$9.99 USD
Template for a bouncing ball game, fully documented in comments and video
• I'd like to simulate balls bouncing from one side of the screen to the other and back and forth without using the physics or bullet behaviors. Also, for the balls to maintain a consistent max height when landing on platforms. How can this be done?

• ## Try Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• You can do the motion with vars for velocityX, velocityY and gravity. Then the kinetic motion can be calculated with:

x = x + velocityX*dt

velocityY = velocityY + gravity*dt

y = y + velocityY*dt

To make it bounce there are a few approaches. One simple one it to move horizontally first, then vertically. For either motion the idea is to:

1. move

2. if ball is overlapping wall then unto the move and reverse the velocity.

To make the ball always bounce back up to the same height is a bit trickier. In an idea world we would bounce at the exact point of collision, which can be calculated but even then there will be rounding errors over time.

A simpler idea would be to conserve total energy. AKA total_energy=potential_energy+kinetic_energy

Potential energy (PE) is the height of the object off the ground times gravity, and kinetic energy (KE) is the speed squared divided by two.

PE = -y*gravity

KE = 0.5*speed^2

As the ball moves energy is transferred back and forth between PE and KE, and if energy is conserved then the total energy (E) will always be the same.

E = KE + PE

This is useful because with that we can calculate what the speed is for any y. You can work out the algebra yourself but it comes out to:

speed = sqrt(2*gravity*(y - start_y))

So with that we can correct the speed so the ball always bounces to the same height.

• 3 posts