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

Get help using Construct 2

Post » Tue Dec 16, 2014 7:42 am

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?
B
10
S
4
G
3
Posts: 103
Reputation: 2,984

Post » Tue Dec 16, 2014 10:39 pm

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.
You do not have the required permissions to view the files attached to this post.
B
91
S
31
G
103
Posts: 5,238
Reputation: 67,762

Post » Fri Dec 19, 2014 1:14 am

@R0J0hound Thank you kindly!
B
10
S
4
G
3
Posts: 103
Reputation: 2,984


Return to How do I....?

Who is online

Users browsing this forum: alextro, stevenode and 9 guests