Back

AddPhysics

Hello World
Content:
- Methods

Adds the Box2D physics engine to your game.

phs = gfx.AddPhysics( gravity, accuracy, sleep )

The method list below shows the functions which are added to game objects when using physics.

Example - Billiard

cfg.Game;

function OnLoad()
{
    gfx.AddPhysics(0);
    gfx.Enclose( -1, "left,top,right,bottom", 1, 0.9, 0 );


    ball1 = gfx.CreateSprite( "/Sys/Img/Forward.png" );
    ball2 = gfx.CreateSprite( "/Sys/Img/Forward.png" );
}

function OnReady()
{
    gfx.AddSprite( ball1, 0.2, 0.6, 0.16 );
    ball1.SetPhysics( 1, "Dynamic", 0.5, 0.8, 0.01 );
    ball1.SetVelocity( 2, -2.3 );
    ball1.SetShape( "round" );

    gfx.AddSprite( ball2, 0.5, 0.6, 0.24 );
    ball2.SetPhysics( 1, "Dynamic", 0.5, 0.8, 0.01 );
    ball2.SetVelocity( 2, 2.3 );
    ball2.SetShape( "round" );

    gfx.Play();
}
    Copy     Copy All       Run      

Example - Collision

cfg.Game;

function OnLoad()
{
    gfx.AddPhysics( 20 );
    gfx.Enclose( -1, "left,top,right,bottom", 1, 0.9, 0.01 );

    bunny = gfx.CreateSprite( "/Sys/Img/Bunny.png", "bunnies" );
    squeak = gfx.CreateSound( "/Sys/Snd/Squeak.mp3" );
}

function OnReady()
{
    gfx.AddSprite( bunny, 0.2, 0.6, 0.05 );
    bunny.SetPhysics( 1, "Dynamic", 0.5, 0.9, 0.1 );
    bunny.SetVelocity( 2, -2.3 );

    gfx.Play();
}


function OnCollide( a, b )
{
    if( a.group == "bunnies" )
        squeak.Play( false, 0, 0 );
}
    Copy     Copy All       Run      

Methods

The following methods are available on the AddPhysics object:

Contains( x, y ) → boolean
GetVelocity( component ) → number
boolean
number
string
number: fraction (0..1)
number: -1..1
string: “x” or “y” or “angular”
string: “moveable” or “fixed”
string: “rect” or “round”
app object: Matrix
phs.AddVelocity
Adds linear and angular velocity to the object
phs.ApplyImpulse
Applies an impulse (impulse=velocity*mass) to the object
phs.Contains
Checks if a given point is in the hitbox
phs.EnablePhysics
Temporally En/Disables physics for the game object.
phs.GetVelocity
Returns the current total linear velocity or the “x”, “y” or “angular” component.
phs.RemovePhysics
Removes physics from the game oblect.
phs.SetMatrix
Applies a 2d transformation matrix to the game object.
phs.SetPhysics
Set the physics properties for the game object.
phs.SetShape
Defines the shape for collision detection of the object.
phs.SetVelocity
Set the velocity of the game object.
phs.UpdatePhysics
Updates the internal position and size properties