Back

AddPhysics

JS Py
Hello World
Content:
- Methods

Adds the Box2D physics engine to your game.

phs = gfx.AddPhysics( gravity?, accuracy?, sleep? ) → game object: 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();
}
# cfg.Game

def OnLoad():
    global ball1, ball2
    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")

def 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 );
}
# cfg.Game

def OnLoad():
    global bunny, squeak
    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")

def 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()

def OnCollide(a, b):
    if a.group == "bunnies":
        squeak.Play(False, 0, 0)
    Copy     Copy All       Run      

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

Methods

The following methods are available on the Physics object:

Contains( x, y ) → Boolean
GetVelocity( component ) → Number
Boolean
Number
Number: fraction (0..1)
Number: -1..1
String: “Polygon”
String: “x” or “y” or “angular”
String: “moveable” or “fixed” or “dynamic”
String: “rect” or “round”
String
Number
Object: Matrix
List: [ [x, y] ]
phs.AddShape
Adds linear and angular velocity to the object
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