This cart is a simple example of how to add gravity to an object in your game.

Notes: The basic idea is that in addition to storing the position of an object, you also its speed in both the x and y directions. Then, every update, you add just a little bit to the y speed. The result is the appearance of the object having gravity.

To then make it bounce, you just decide where you want a wall to be, and if the object goes past that spot, you reverse the speed in the direction it hit the wall. To make it more realistic, when you reverse the speed's direction, you also make the speed just a little bit smaller, as if the wall absorbed some of the speed.


gravity.p8.png 6 kB

Install instructions

Save this file to your PICO-8 carts folder. Then load it by typing load gravity in the command line mode of PICO-8.