CG.B2DWorld Class
B2DWorld is a Box2dWeb wrapper with basic and easy methods for creating Box2d Objects like lines, circles, rectangles, polybodies, ropes and bridges. Custom B2D Objects that extends one of the basic B2D objects can added to the B2DWorld with the addCustom method. The CG.B2DWorld can attached to an CG.Screen object as layer. The B2DWorld will handle all physics and drawings.
var w = new CG.B2DWorld({
name: 'box2d-world',
scale: 40,
debug: true,
sleep: true
})
Item Index
Methods
Methods
addCustom
-
obj
Custom extended objects can be added to the B2DWorld with this method.
Parameters:
-
obj
Objectobject custom B2D object
Returns:
createBox
-
options
createBox creates a basic Box2D rectangle with some default settings.
Parameters:
-
options
Object
Returns:
Example:
b2world.createBox({
name: 'glowball',
image: this.asset.getImageByName('glowball'),
radius: 22,
x: this.mouse.x,
y: this.mouse.y,
bodyType: box2d.b2BodyType.b2_dynamicBody
})
createBridge
-
options
This method creates a B2D bridge. Just play with the params to get a good result!
Parameters:
-
options
Object
Returns:
Example:
b2world.createBridge({
name:'chain',
image:this.asset.getImageByName('chain'),
x: 20,
y: 280,
length: 620,
segments: 27,
segmentHeight: 3
})
createChainShape
-
options
createChainShape
Parameters:
-
options
Object
Returns:
Example:
chainArray = [
new CG.Point(0, 0),
new CG.Point(50, 10),
new CG.Point(100, 100),
new CG.Point(200, 100),
new CG.Point(250, 50),
new CG.Point(300, 70)
]
b2world.createChainShape({
name: 'chaneshape',
points: chainArray,
x: 0,
y: 200
})
createCircle
-
options
createCircle creates a basic Box2D circle with some default settings
Parameters:
-
options
Object
Returns:
Example:
b2world.createCircle({
name: 'glowball',
image: this.asset.getImageByName('glowball'),
radius: 22,
x: this.mouse.x,
y: this.mouse.y,
bodyType: box2d.b2BodyType.b2_dynamicBody
})
createLine
-
options
createLine creates a basic Box2D line with some default settings.
Parameters:
-
options
Object
Returns:
Example:
b2world.createLine({
name:'testline2',
startPoint: new CG.Point(630, 200),
endPoint: new CG.Point(150, 250)
})
createPolyBody
-
options
createTerrain
Parameters:
-
options
String
Returns:
Example:
var terrainPolys =
[{
outer: [{
x: 0,
y: 100.5
}, {
x: 1024,
y: 100.5
}, {
x: 1024,
y: 768
}, {
x: 0,
y: 768
}],
holes: []
}]
b2world.createTerrain({
name: 'terrain',
image: false
terrainShape: terrainPolys,
x:0,
y:0
})
createPolyBody
-
options
createPolyBody creates a Box2D polybody. A PhysicsEditor json (Lime + Corona JSON Exporter) file is needed for this Box2D object. The polygonshape and some settings like density, bounce and friction are taken from the json file at the moment.
Parameters:
-
options
Object
Returns:
Example:
b2world.createPolyBody({
name: 'powerstar75',
image: this.asset.getImageByName('powerstar75'),
texturepacker: this.asset.getJsonByName('powerstar75'),
x: 200,
y: -150,
bodyType: box2d.b2BodyType.b2_dynamicBody,
bullet: false
})
createRope
-
options
This method creates a B2D rope. Just play with the params to get a good result!
Parameters:
-
options
Object
Returns:
Example:
b2world.createRope({
name: 'chain-v',
image: this.asset.getImageByName('chain-v'),
x: 580,
y: 0,
length: 200,
segments: 8,
segmentWidth: 3
})
draw
()
getBodyAt
-
x
-
y
Get a B2D body at the give x, y position.
Returns:
init
-
options
Options: name {string} sleep {boolean} scale {number} debug {boolean}
Parameters:
-
options
Object