Log In  

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.

Cart #39426 | 2017-04-08 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA

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).


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.

P#39428 2017-04-08 17:07 ( Edited 2017-04-08 21:32)

[Please log in to post a comment]