AppGameKit Classic

AppGameKit Classic

Not enough ratings
A basic introduction to Sprite Physics
By thescenecommander
This guide expands upon the idea of sprite movement by introducing 2D physics and explores the concept of arrays and types (different variable structures).
   
Award
Favorite
Favorited
Unfavorite
2D Physics example
Hi,

Moving on with the theme of sprites, this guide will provide another well documented example that explores the basics of 2D Physics. I'd suggest that you create a new project and cut and paste the code as before; you will need a copy of the balloon media used in the previous examples. You can copy this from the example project SpriteAnim1 as explained in an earlier guide.

In this demo you will learn how to add basic physics to a sprite, and see how force affects movement from a simple mouseclick or screen tap on mobile devices.

As always, I recommend playing with the code, trying out different numbers of balloons (by altering the variable balloonsmax).

You can also alter the Restitution of each sprite (how much they bounce around),full details are contained in the code.

This demo also introduces a new type of variable arrays and shows how to use these to access multiple sprite using a simple loop.

Bouncing balloons in action:


// Project: making a sprite move.
// Created: 2014-11-21

// set window properties
SetWindowTitle( "Adding Simple Physics to a Sprite" )

// set variables
screenwidth=1024
screenheight=768
fullscreen=0

// set display properties for platforms that support windows, e.g., Windows or Mac.

SetWindowSize(screenwidth,screenheight,fullscreen )

// set display properties for mobile devices.

SetVirtualResolution(screenwidth,screenheight)

//Setting up a new type of variable called a type named balloonsdata. Type allow many elements to be associated with a single group.
type balloonsdata
balloonx as integer
balloony as integer
balloonsprite as integer
endtype

// Set the maximum balloons on screen, you can add more or less balloons by changing the 10 to another number
balloonsmax=10

// Create a new variable type called an array. Array's are like single variables by can be reference by a number to alter or reference many at a time, the number in the [] is the number of elements in the array
// in this case 10 or the value of balloonmax. In addition we have associated to type by using as balloonsdata
dim balloons[balloonsmax] as balloonsdata

// For-next loops are an quick an easy way of executing a code loop X number of times. For testloop=1 to 100: next testloop would repeat itself 100 times before continuing.

// This loop is called balloonsetup and is counting from 1 to the maximum number of balloons. (balloonmax)
for balloonsetup=1 to balloonsmax

//balloons[].balloonsprite holds the number of the sprite that will be used by each balloon. This is the value of balloonsetup which increase by one with each loop.
balloons[balloonsetup].balloonsprite=balloonsetup

//Assign a random vaule to the the X and Y cordinates, stored in the array.
//the random value generated by Random is a number between the first and the second number between the brackets
balloons[balloonsetup].balloonx=random(0,990)
balloons[balloonsetup].balloony=random(0,200)

// create a sprite with ID that uses the balloon image
CreateSprite (balloons[balloonsetup].balloonsprite,0)

//Position the sprite using the sprite number and the sprite positions as stored in the array
SetSpritePosition (balloons[balloonsetup].balloonsprite,balloons[balloonsetup].balloonx,balloons[balloonsetup].balloony)

//Set the Animation frames
AddSpriteAnimationFrame(balloons[balloonsetup].balloonsprite,LoadImage("item0.png"))
AddSpriteAnimationFrame(balloons[balloonsetup].balloonsprite,LoadImage("item1.png"))
AddSpriteAnimationFrame(balloons[balloonsetup].balloonsprite,LoadImage("item2.png"))
AddSpriteAnimationFrame(balloons[balloonsetup].balloonsprite,LoadImage("item3.png"))
AddSpriteAnimationFrame(balloons[balloonsetup].balloonsprite,LoadImage("item4.png"))

//Set the sprite to loop through it's animations
PlaySprite (balloons[balloonsetup].balloonsprite)

//This is where we start looking at Sprite Physics.
//Set up the sprite to use physics. Physics will allow sprites to move with more realism with just a few simple commands. However, it isn't always the best way to control movement
//If you don't need complex movement, we might be better with some of the simple technique outlined in my earlier guides.
//The first number is the sprite reference the second the type of physics you want to use: The physics mode to use a sprite, 1=static, 2=dynamic, 3=kinematic
SetSpritePhysicsOn(balloons[balloonsetup].balloonsprite,2)
//Defines wether or not a sprite can have it's rotation controlled by physics or if it's set. By default this demo allows rotation, you can experiment by changing 1 to 0 for a different effect.
SetSpritePhysicsCanRotate(balloons[balloonsetup].balloonsprite,1)
//Set a sprites Restitution or bouncyness. The first number is the sprite reference, and the second the Restitution, this should be between .1 and 1, larger numbers can cause unpredictable results,
//but please feel free to experiment

SetSpritePhysicsRestitution(balloons[balloonsetup].balloonsprite,.3)

//Check to see if the for-next loop is complete. When the NEXT command is reached, the code will loop if the target number hasn't been reached, or continue to the next command if it has.
next balloonsetup

//Set up invisible artifical walls at the edges of the screen that prevent physcis controlled object from leaving the screen.
SetPhysicsWallBottom(1)
SetPhysicsWallLeft(1)
SetPhysicsWallRight(1)
SetPhysicsWallTop(1)
// Main loop - This demo simulates pulses of energy and the effects on physics objects.

do

//Check to see if a pointer has been pressed, this could be a mouse click, a screen tap or other method of control depending on the device used.
if getpointerpressed()=1
//store the mouse pointer
py=getpointerx()
px=getpointery()
//loop through all of the balloon sprites
for forceloop=1 to balloonsmax
//set up an amount of force.
forcex=10000
forcey=10000
//Ensure that force is correctly applied depending on the relative position of the pointer click and the sprite.
if px>getspritex(balloons[forceloop].balloonsprite) then forcex=-10000
if py<getspritey(balloons[forceloop].balloonsprite) then forcey=-10000
//Apply force as an impulse. This is a short about of force applied for 1 second
SetSpritePhysicsImpulse(balloons[forceloop].balloonsprite,px,py,forcex,forcey)
//End loop
next forceloop
//End getpointerpressed
endif
//Update display.
Sync()
loop
2 Comments
Terra162 18 Jul, 2015 @ 10:21pm 
@Ice Abyssinian, The media is only in the projects folder if you install the sample projects. Look at his tutorial on setting up AGK.
Y 17 Mar, 2015 @ 10:39pm 
Great starting place for using the physics system. I couldn't find the media, though, even after looking through the guide "Adding Animation frames to a Sprite."