This is a simple demo of storing movement in speed and angle values instead of speed-x and speed-y. It shows a ball bouncing around inside a box.

Controls:

up/down: change movement speed

left/right: change movement angle

x: pause movement

z: randomize the movement angle

Introduction to pico-8 angles:

right: 0

up: 0.25

left: 0.5

down: 0.75

The speed of the ball is stored as ball.speed and the angle is stored as ball.angle. When the ball moves, the program uses sin() and cos(), along with the speed and angle, to calculate how far to move the ball in each direction (x/y movement).

dx=ball.speed*cos(ball.angle) dy=ball.speed*sin(ball.angle) |

The advantage of storing the speed and angle speed-x and speed-y is that they are easier to change directly than manipulating x-speed and y-speed. For example, when the ball bounces off a wall, you can just change the angle and know that the speed will stay the same.

This demo calculates speed-x and speed-y every frame, which isn't really necessary. In a real program, you would probably want to store all four values: speed, angle, speed-x and speed-y, and only recalculate speed-x and speed-y when you change the speed or angle.

In the bounce_ball() function, the angle you provide is the angle of reflection. For example, a vertical line has an angle of 0.25 (it goes straight up) and a horizontal line has an angle of 0.5 (it goes straight left). You can tweak these angles if you want the ball to bounce off at a different angle.

[Please log in to post a comment]