Physics functions
gamvas. | Physics functions |
Macros | |
DYNAMIC | define for a dynamic body |
STATIC | define for a static body |
KINEMATIC | define for a kinematic body |
Variables | |
pixelsPerMeter | Sets the pixels per meter ratio. |
velocityIterations | Sets the Box2D velocity iteration steps |
positionIterations | Sets the Box2D position iteration steps |
debugAlpha | The alpha of physics debug information |
debugStrokeWidth | The stroke width if you draw physics debug information |
Functions | |
toScreen | Calculates a physics coordinate or size to the screen coordinate or size |
toWorld | Calculates a screen coordinate or size to a coordinate or size in the physics world |
drawDebug | Draw the physics debug information. |
setGravity | Sets the physics worlds gravity to a gamvas.Vector2D |
resetWorld | Resets the physics world |
getWorld | Get the Box2D world object |
pixelsPerMeter: 64
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.
64
toWorld: function( v )
Calculates a screen coordinate or size to a coordinate or size in the physics world
v | the screen value |
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: function()
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.
myState = gamvas.State.extend({ draw: function(t) { this.drawAllObjects(t); gamvas.physics.drawDebug(); } });
setGravity: function( vec )
Sets the physics worlds gravity to a gamvas.Vector2D
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: function( gravx, gravy, slp, listener )
Resets the physics world
gravx | the gravity in x direction (meters per second) (optional, default 0) |
gravy | the gravity in y direction (meters per second) (optional, default 9.8) |
slp | enable sleeping (optional, default true) |
listener | a 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
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
Start with a new empty world with default settings
gamvas.resetWorld();
getWorld: function()
Get the Box2D world object
The Box2D world object
Sets the pixels per meter ratio.
pixelsPerMeter: 64
Sets the Box2D velocity iteration steps
velocityIterations: 10
Sets the Box2D position iteration steps
positionIterations: 8
The alpha of physics debug information
debugAlpha: 0.3
The stroke width if you draw physics debug information
debugStrokeWidth: 2
Calculates a physics coordinate or size to the screen coordinate or size
toScreen: function( v )
Calculates a screen coordinate or size to a coordinate or size in the physics world
toWorld: function( v )
Draw the physics debug information.
drawDebug: function()
Sets the physics worlds gravity to a gamvas.Vector2D
setGravity: function( vec )
Resets the physics world
resetWorld: function( gravx, gravy, slp, listener )
Get the Box2D world object
getWorld: function()