gamvas.physics

Physics functions

Summary
gamvas.physicsPhysics functions
Macros
DYNAMICdefine for a dynamic body
STATICdefine for a static body
KINEMATICdefine for a kinematic body
Variables
pixelsPerMeterSets the pixels per meter ratio.
velocityIterationsSets the Box2D velocity iteration steps
positionIterationsSets the Box2D position iteration steps
debugAlphaThe alpha of physics debug information
debugStrokeWidthThe stroke width if you draw physics debug information
Functions
toScreenCalculates a physics coordinate or size to the screen coordinate or size
toWorldCalculates a screen coordinate or size to a coordinate or size in the physics world
drawDebugDraw the physics debug information.
setGravitySets the physics worlds gravity to a gamvas.Vector2D
resetWorldResets the physics world
getWorldGet the Box2D world object

Macros

DYNAMIC

define for a dynamic body

Dynamic bodies are the objects that are under influence of forces

STATIC

define for a static body

Static bodies are the objects that do never move, like walls or ground objects

KINEMATIC

define for a kinematic body

Kinematic bodies are objects that move, but not by physics engine, but by either player input or AI

Variables

pixelsPerMeter

pixelsPerMeter: 64

Description

Sets the pixels per meter ratio.  The physics engine calculates in meters, so if for example you have a car that is 4 meters long, then you set a bounding box with the with of ‘4’ for the physics engine, ofcourse a car with 4 pixels in size would be only hard so see on the screen, so you can set a pixels per meter ratio.  Default is 64, so the image for your 4 meter long car would have to be 256 pixels in with.

Default

64

velocityIterations

velocityIterations: 10

Description

Sets the Box2D velocity iteration steps

Default

10

See

http://box2d.org/manual.html

positionIterations

positionIterations: 8

Description

Sets the Box2D position iteration steps

Default

8

See

http://box2d.org/manual.html

debugAlpha

debugAlpha: 0.3

Description

The alpha of physics debug information

Default

0.3

debugStrokeWidth

debugStrokeWidth: 2

Description

The stroke width if you draw physics debug information

Default

2

Functions

toScreen

toScreen: function(v)

Description

Calculates a physics coordinate or size to the screen coordinate or size

Parameters

vthe physics value

Examples

var physPos = myActor.body.GetPosition();
var screenX = gamvas.physics.toScreen(physPos.x);

toWorld

toWorld: function(v)

Description

Calculates a screen coordinate or size to a coordinate or size in the physics world

Parameters

vthe screen value

Examples

var startPos = new gamvas.Vector2D(640, 480);
myActor.body.SetPosition(
   new Box2D.Common.Math.b2Vec2(
      gamvas.physics.toWorld(startPos.x),
      gamvas.physics.toWorld(startPos.y)
   )
);

drawDebug

drawDebug: function()

Description

Draw the physics debug information.  Should be used after all objects are drawn.  This is a reduced debug information with simpler handling then the Box2D version, you still can use the Box2D way though.

Example

myState = gamvas.State.extend({
    draw: function(t) {
        this.drawAllObjects(t);
        gamvas.physics.drawDebug();
    }
});

setGravity

setGravity: function(vec)

Description

Sets the physics worlds gravity to a gamvas.Vector2D

Example

myState = gamvas.State.extend({ draw: function(t) { // ajust gravity to camera rotation var rot = this.camera.rotation; var vec = new gamvas.Vector2D(0.  9.8); gamvas.physics.setGravity(vec.rotate(-r)); } });

resetWorld

resetWorld: function(gravx,
gravy,
slp,
listener)

Description

Resets the physics world

Parameters

gravxthe gravity in x direction (meters per second) (optional, default 0)
gravythe gravity in y direction (meters per second) (optional, default 9.8)
slpenable sleeping (optional, default true)
listenera Box2D.Dynamics.b2ContactListener (optional)

When sleeping is enabled, objects that have not moved for a while fall into a sleep state so they are not simulated until a collider hits them.

If no listener is specified (recommende) a default listener is installed, that handles the actor collision.  Usually this should be enough for almost any purpose, but if you are a total Box2D guru, you might use a own listener

Note

Box2D and Gamvas differ in how the y coordinte is handled.  While Box2D tries to resemble real world sitation where a positive y coordinte would go upward, gamvas is a 2D raster graphics game engine, where a positive y coordinate would go downward.

This leaves two special things that you should know.

A) While in real world, gravity is roughly -9.8 (negative) meters per second, in gamvas the default gravity is +9.8 (positive) to make objects fall down

B) While Box2D asks you to use counter clock wise (CCW) polygons, in gamvas you have to specify clock wise polygons

Example

Start with a new empty world with default settings

gamvas.resetWorld();

getWorld

getWorld: function()

Description

Get the Box2D world object

Returns

The Box2D world object

See

https://code.google.com/p/box2dweb/

http://www.box2d.org/

pixelsPerMeter: 64
Sets the pixels per meter ratio.
velocityIterations: 10
Sets the Box2D velocity iteration steps
positionIterations: 8
Sets the Box2D position iteration steps
debugAlpha: 0.3
The alpha of physics debug information
debugStrokeWidth: 2
The stroke width if you draw physics debug information
toScreen: function(v)
Calculates a physics coordinate or size to the screen coordinate or size
toWorld: function(v)
Calculates a screen coordinate or size to a coordinate or size in the physics world
drawDebug: function()
Draw the physics debug information.
setGravity: function(vec)
Sets the physics worlds gravity to a gamvas.Vector2D
A 2D vector class
resetWorld: function(gravx,
gravy,
slp,
listener)
Resets the physics world
getWorld: function()
Get the Box2D world object
Close