The physic engine

Our world is made of physic, everything is physic! Gravity pushes every object to the ground, every step we take involves friction with the surface our shoes come in contact with, wind blows and pushes leaves around and balls bounces from the ground and walls with different elasticity. When applied to a game, physics can add more realism and can add possibilities you thought were too hard to code. PPL comes with a 2D automated physic engine. When I say automated I mean it, you only need to set the mass of a sprite, set the global gravity and the physic will take care of pushing the object down. Objects can bounce back and forth by simply assigning an elasticity value. The physic is far from an advanced one that supports object deformation on collision and such but it can do a very good job simplifying your life while developing your game.
Lets first start by reviewing the principal functions used by the physic engine:

SetSpriteMass(Sprite$, Mass);

The mass is a percentage value that is compared to the other sprites with physic. A sprite with 0.5 mass will weight half the weight of a sprite with a mass of 1.0.

SetSpriteElasticity(Sprite$, Elasticity);

The elasticity value is a percentage compared to the other sprites. The bigger the value the more rebound will be applied when the sprite collides with another sprite.

SetSpriteFriction(Sprite$, Friction);

The friction is the amount of friction in percentage applied to reduce the movement speed of the sprite when it collides with another sprite.

SetGravity(Gravity);

This function will set the global gravity of the sprite engine, the value of the gravity is applied to the sprites movement speed each cycle. Default gravity is around 0.1.

SetFriction(Friction);

Set the global friction that is applied to sprite's movement speed each cycle. The default friction value is 0.00025.

There are options that need to be activated for the physic engine to consider moving the objects around. The first one is the SO_KINETIC to active the physic engine on the sprite itself. The second one is the SO_BOUNCE, it will make the sprite bounce around from other sprites. The bounce force is calculated based on the Elasticity of the sprite. Sprites must have the SO_COLLIDECHECK option set to them for bouncing to occur. You can have your sprites bounce from the screen edges by setting the SO_BORDER option. Most of the sprites you will want to bounce around will be of oval shape, you will want to set the SO_OVAL option for the physic engine to bounce the sprite like a real oval shaped object.

Lets review the bounce.ppl demo that comes with PPL. This demo involves 5 basketball balls bouncing around from the screen edges and from each other. In this example we create 5 sprites with the basketball ball image and the we set the options of each sprite to oval shape, collision checking, pixel checking and border collision check.

// Set global gravity.
SetGravity(0.1);
// Set global friction.
SetFriction(0.005);

i$ = 0;
while (i$ < 5)
// Load ball sprite from disk.
s$ = loadsprite(AppPath$ + "ball.bmp", G_RGB(255, 0, 255), 1, 0, NULL);

// Activate pixel perfect collision detection.
AddSpriteOption(s$, SO_OVAL | SO_CHECKCOLLIDE | SO_PIXELCHECK | SO_BORDER | SO_KINETIC);

// Make the balls collide and never go over another.
SetSpriteCollide(s$, "BALL");
SetSpriteId(s$, "BALL");

repeat
MoveSprite(s$, random(g_width - 40), random(64));
ProcessSprites(1, 0);
until (Collide(s$, SpriteX(s$), SpriteY(s$), nx$, ny$) == NULL);

// Set ball elasticity.
SetSpriteElasticity(s$, 0.01);

// Set some friction when the balls collide.
SetSpriteFriction(s$, 0.01);

// Set sprite's weight.
SetSpriteMass(s$, 0.5);

// Set the maximum velocity to 10 pixels.
SetSpriteVelLimits(s$, 0, 10);

i$++;
end;