By utilising the physics commands within AppGameKit Studio it's possible to greatly simplify complex tasks such as collision and physics based movement. Transforming sprites into physics based entities is covered in this example. It's a simple demonstration program that creates physics sprites every so often, sets some properties and leaves it to the physics engine to deal with movement and collision response.
A few steps are carried out in this program:
First we'll load in a backdrop image and display it as a sprite with an alpha value of 40 so it's no so bright:
backdrop = CreateSprite ( LoadImage ( "background1.jpg" ) ) SetSpriteColorAlpha ( backdrop, 40 )
Before moving onto the main loop an image is loaded:
LoadImage ( 2, "silver.png" )
This image is going to be used by all the sprites that get created within the main loop.
The final part before the main loop is to set the gravity vector for all physics based sprites. For this example, gravity is only required on the Y axis at a rate of 0.1 meters per second squared. This is handled with a call to SetPhysicsGravity:
SetPhysicsGravity ( 0, 0.5 )
When our sprites enter the scene they will constantly be affected by the gravity. Increasing the Y value will have the effect of pushing the sprites to the ground much faster.
After every 75 cycles of the main loop the following will take place:
Our sprites will have an angular impulse applied along with a velocity and the setting of resitution upon creation. When an angular impulse gets applied to a sprite it will affect the rotation of the sprite. The reaction to an angular impulse is dependent on the size of our sprites. Larger sprites will rotate slower than smaller sprites given the same impulse.
Velocity being applied to a sprite will have the effect of pushing a sprite towards a particular direction. Directly after our sprites have been created they will have a random velocity applied to them. This will have the effect of shooting our sprites out in random directions on the X and Y axis.
Resitution controls how our sprites react upon collision. If two sprites collided and they had a low restitution they might come together and upon impact stop and lose all of their momentum, after that they might fall to the ground as they are affected by gravity. If these two sprites had a high restitution, upon impact they might go flying off in opposite directions.
time = 0 index = 10
do time = time + 1
if ( time >= 75 ) CreateSprite ( index, 2 ) SetSpritePosition ( index, 50, 0 ) SetSpritePhysicsOn ( index, 2 )
size = 20 + Random ( 0, 30 ) SetSpriteSize ( index, size, size ) SetSpriteShape ( index, 2 ) SetSpritePhysicsAngularImpulse ( index, 10000 + Random ( 0, 350 ) ) SetSpritePhysicsVelocity ( index, 100 + Random ( 0, 100 ), 100 + Random ( 0, 100 ) ) SetSpriteColor ( index, Random ( 0, 255 ), Random ( 0, 255 ), Random ( 0, 255 ) ) SetSpritePhysicsRestitution ( index, 0.5 )
index = index + 1 time = 0 endif
Sync ( ) loop
Everything is now in place. Here's the final code for our program:
SetVirtualResolution ( 320, 480 )
backdrop = CreateSprite ( LoadImage ( "background1.jpg" ) ) SetSpriteColorAlpha ( backdrop, 40 )
LoadImage ( 2, "silver.png" )
SetPhysicsGravity ( 0, 0.5 )
time = 0 index = 10
do time = time + 1
if ( time >= 75 ) CreateSprite ( index, 2 ) SetSpritePosition ( index, 50, 0 ) SetSpritePhysicsOn ( index, 2 )
size = 20 + Random ( 0, 30 ) SetSpriteSize ( index, size, size ) SetSpriteShape ( index, 2 ) SetSpritePhysicsAngularImpulse ( index, 10000 + Random ( 0, 350 ) ) SetSpritePhysicsVelocity ( index, 100 + Random ( 0, 100 ), 100 + Random ( 0, 100 ) ) SetSpriteColor ( index, Random ( 0, 255 ), Random ( 0, 255 ), Random ( 0, 255 ), 255 ) SetSpritePhysicsRestitution ( index, 0.5 )
index = index + 1 time = 0 endif
Sync ( ) loop
This example demonstrates how it’s possible to get a fairly complex situation being played out on screen with a minimal amount of effort.