You are on page 1of 4

About

BASIC

Go Native!

Platforms

Videos

Documentation

Gallery

FAQ

Tutorials

FREE Trial

ORDER

AGK Bitesize
Lesson Two - An introduction to AGK Physics
by Steven Holding
Introduction

Table of Contents

In this tutorial we will take a look at the physics system incorporated into AGK. Using this system we can recreate a realistic set of physical reactions and quickly create an effective game engine with very little coding. Arguably the most widely known 2D physics based game is "Angry Birds" and we can easily code a similar basic game engine in AGK physics, even in a "Bitesize" tutorial. We'll start by positioning some ordinary sprites where we want them, for the time being we'll confine them to the limits of our screen but it's worth increasing the size of our screen first to give us a little more visible room.

Lesson One The AGK World Lesson Tw o An introduction to AGK Physics Lesson Three Let's get Physical

Start by creating a new project and then open the "setup.agc" file. Edit line 13 so that it says "width=1024". Also edit line 16 so that it says "height=600". Then open the "main.agc" file and change the display aspect (line 6) like this:

S e t D i s p l a y A s p e c t (1 0 2 4 . 0/6 0 0 . 0)

Now we have a larger, wider screen to play with let's add some dynamic sprites to knock down. Add the following code after the display aspect line:

r e ml o w e rl e v e l f o rs = 1t o4 s p r=c r e a t e S p r i t e ( 0 ) s e t S p r i t e V i s i b l e ( s p r , 0 ) s e t S p r i t e S i z e ( s p r , 1 , 2 0 ) s e t S p r i t e P o s i t i o n ( s p r , 7 0+s * 5 , 8 0 ) r e mt u r np h y s i c so n ! s e t S p r i t e P h y s i c s O n ( s p r , 2 ) i fs < 4 s p r=c r e a t e S p r i t e ( 0 ) s e t S p r i t e V i s i b l e ( s p r , 0 ) s e t S p r i t e S i z e ( s p r , 5 , 1 . 5 ) s e t S p r i t e P o s i t i o n ( s p r , 7 0 . 5+s * 5 , 7 9 . 2 5 ) r e mt u r np h y s i c so n ! s e t S p r i t e P h y s i c s O n ( s p r , 2 ) e n d i f n e x t

You will notice that we create a sprite, set its size and set its position. Then we "set sprite physics on". The "2" means we are creating a "dynamic" sprite which will react to hitting other physics sprites. We are also setting each sprite's visibility to zero; this is because we will be viewing this exercise in debug mode to avoid the need for any media. We turn debug mode on by adding this line before our game loop:

S e t P h y s i c s D e b u g O n ( )

Here is what you should see when you compile and run the code so far:

There are three settings (1 to 3) which are "static", "dynamic" and "kinematic". Static sprites will not be affected by other sprites or forces (such as gravity), but will act as a physical barrier to other sprites. Dynamic sprites will react to collisions with static / dynamic / kinematic sprites as well as forces. Kinematic sprites are similar to static sprites but can have their linear and angular velocity set / edited. During a collision, a kinematic sprite will not divert from it's course. We will now create a static sprite which will represent an obstacle in our scene. Add this code after the last line we added:

r e mo b s t a c l e s p r=c r e a t e S p r i t e ( 0 ) s e t S p r i t e V i s i b l e ( s p r , 0 ) s e t S p r i t e S i z e ( s p r , 2 , 1 5 ) s e t S p r i t e P o s i t i o n ( s p r , 5 0 , 8 5 ) r e mt u r np h y s i c so n ! s e t S p r i t e P h y s i c s O n ( s p r , 1 )

You may notice when you run the code that static sprites are shown in green in debug mode:

Now we need something to throw at our dynamic sprites! Another dynamic object would be ideal so let's use a circular object and position it where we would like to throw it from.

r e mc i r c u l a ro b j e c t s p r=c r e a t e S p r i t e ( 0 ) s e t S p r i t e V i s i b l e ( s p r , 0 ) s e t S p r i t e S i z e ( s p r , 2 , 1 ) s e t S p r i t e P o s i t i o n ( s p r , 5 , 9 5 ) r e mt u r np h y s i c so n ! s e t S p r i t e P h y s i c s O n ( s p r , 2 ) s e t S p r i t e S h a p e C i r c l e ( s p r , 0 , 0 , 1 ) r o c k=s p r

I have assigned the name "rock" for this sprite as we will need to make sure we are looking at the correct sprite in a short while.

We also have a new command "setSpriteShapeCircle". Physics-enabled sprites require a "shape" to define their bounds. The "0,0" parameters are the offset of the shape from the sprite's centre and the "1" is the radius of the circle (1 percent of the width of the screen). Sprites can be circular, rectangular (including square) or polygon shaped. Polygon (or irregular) shapes can be automatically created by AGK based on your sprite image or defined by you (for more complex shapes) but for now we will stick with simple shapes. Your circular sprite should look like the image here. All we need to do now is to make it possible to throw our sprite over the obstacle to try to hit our targets. This can be done in a number of ways but perhaps the simplest is to just drag the sprite. We can create "joints" in AGK which are similar to connections we make in real life. A "revolute" joint for example is similar to loosely bolting two lengths of wood together so that they can "hinge". For the purpose of this exercise a "mouse" joint is similar to a revolute joint fixed to a point in world space and can be used to "grab" objects with the mouse. We will use input from the mouse to pick up and throw our object as follows. Read through the following subroutine and add it after the loop.

m o u s e G r a b : r e mg e tt h ep o i n t e r / m o u s ep o s i t i o n p x #=g e t P o i n t e r X ( ) p y #=g e t P o i n t e r Y ( ) r e mf i n do u ti ft h ep o i n t e r / m o u s eh a sJ U S Tb e e np r e s s e d i fg e t P o i n t e r P r e s s e d ( ) = 1 r e mc h e c kp o s i t i o no fr o c k i fg e t S p r i t e X ( r o c k ) < 2 0a n dg e t S p r i t e Y ( r o c k ) > 8 0 r e mc h e c ki ft h e" r o c k "i su n d e rt h em o u s e i fg e t S p r i t e H i t T e s t ( r o c k , p x # , p y # ) = 1 r e mi ti ss ol e t ' sp i c ki tu p m o u s e J o i n t=c r e a t e M o u s e J o i n t ( r o c k , p x # , p y # , 1 0 0 0 0 ) e n d i f e l s e r e mr e s e tt h e" r o c k "p o s i t i o n s e t S p r i t e P o s i t i o n ( r o c k , 5 , 9 5 ) r e ms t o pt h e" r o c k "f r o mm o v i n g s e t S p r i t e P h y s i c s V e l o c i t y ( r o c k , 0 , 0 ) s e t S p r i t e P h y s i c s A n g u l a r V e l o c i t y ( r o c k , 0 ) e n d i f e l s e r e mc h e c ki ft h em o u s ej o i n te x i s t s i fm o u s e J o i n t > 0 r e mc h e c ki ft h ep o i n t e r / m o u s ei ss t i l lb e i n gp r e s s e d i fg e t P o i n t e r S t a t e ( ) = 1 r e mi ti ss ol e t ' su p d a t et h ep o s i t i o no ft h ej o i n t r e ms ot h e" r o c k "w i l lf o l l o wb u to n l yi ft h em o u s e r e mn o tt o of a rf r o mt h es t a r t ,i nw h i c hc a s ei ti s r e mt h r o w nb yd e l e t i n gt h ej o i n t ! i fp x # < 2 0a n dp y # > 8 0 r e mu p d a t et h em o u s ej o i n tp o s i t i o n s e t J o i n t M o u s e T a r g e t ( m o u s e J o i n t , p x # , p y # ) e l s e r e md e l e t et h em o u s ej o i n t d e l e t e J o i n t ( m o u s e J o i n t ) m o u s e J o i n t = 0 e n d i f e l s e r e md e l e t et h em o u s ej o i n t d e l e t e J o i n t ( m o u s e J o i n t )

m o u s e J o i n t = 0 e n d i f e n d i f e n d i f r e t u r n

This subroutine will check for the rock being clicked on by the mouse and create a mouse joint if it is. If the mouse is released (or too far from the start position) then the rock is thrown by deleting the joint. If the rock is not in its start position when the mouse is pressed it will be "reset" back to the start position. For this code to run you will need to add "gosub mouseGrab:" in the loop as follows:

r e mAW i z a r dD i dI t ! d o g o s u bm o u s e G r a b : S y n c ( ) l o o p

Try running the code and see how it works. This is a very simple start to an "Angry Birds" style game, by adding media and checking the collision data stored by AGK we could quickly get something closer to what would be required for the full engine but hopefully this example gives you a better idea how to use just a few commands to make a simple game engine using AGK physics. The full code and a compiled version of this month's example can be downloaded here. As an extra challenge, why not try to build a more complex structure to demolish, using the simple techniques learned in this tutorial.

App Game Kit Copyright 2011-2013 The Game Creators Ltd. All Rights Reserved.

Join us on Facebook

Follow us on Twitter
Terms & Conditions Website T&Cs Privacy Policy Refunds Policy AUP Find us on Google+ App Game Kit and the AGK logo are trade marks of The Game Creators Ltd. All other logos and brand names are trademarks or registered trademarks of their respective ow ners.

Join our mailing list

You might also like