API Docs for: 0.1.0
Show:

CG.B2DWorld Class

Module: CG

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
                        })
                        

Methods

addContactListener

(
  • callbacks
)

Parameters:

addCustom

(
  • obj
)
Object

Custom extended objects can be added to the B2DWorld with this method.

Parameters:

  • obj Object

    object custom B2D object

Returns:

applyImpulse

(
  • body
  • degrees
  • power
)

Parameters:

createBox

(
  • options
)
CG.B2DRectangle

createBox creates a basic Box2D rectangle with some default settings.

Parameters:

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
)
CG.B2DBridge

This method creates a B2D bridge. Just play with the params to get a good result!

Parameters:

Returns:

Example:

b2world.createBridge({
                                               name:'chain',
                                               image:this.asset.getImageByName('chain'),
                                               x: 20,
                                               y: 280,
                                               length: 620,
                                               segments: 27,
                                               segmentHeight: 3
                                            })
                                            

createChainShape

(
  • options
)
CG.B2DChainShape

createChainShape

Parameters:

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
)
CG.B2DCircle

createCircle creates a basic Box2D circle with some default settings

Parameters:

Returns:

CG.B2DCircle:

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
)
CG.B2DLine

createLine creates a basic Box2D line with some default settings.

Parameters:

Returns:

Example:

b2world.createLine({
                                               name:'testline2',
                                               startPoint: new CG.Point(630, 200),
                                               endPoint: new CG.Point(150, 250)
                                            })
                                            

createPolyBody

(
  • options
)
CG.B2DTerrain

createTerrain

Parameters:

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
)
CG.B2DPolygon

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:

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
)
CG.B2DRope

This method creates a B2D rope. Just play with the params to get a good result!

Parameters:

Returns:

Example:

b2world.createRope({
                                               name: 'chain-v',
                                               image: this.asset.getImageByName('chain-v'),
                                               x: 580,
                                               y: 0,
                                               length: 200,
                                               segments: 8,
                                               segmentWidth: 3
                                            

})

deleteBodyAt

(
  • body
)
Boolean

Checks if a B2D body Exists

Parameters:

Returns:

deleteBodyAt

(
  • x
  • y
)
Boolean

Deletes a B2D body at the given x, y position

Parameters:

Returns:

draw

()

getBodyAt

(
  • x
  • y
)

Get a B2D body at the give x, y position.

Parameters:

Returns:

:

getBodySpec

(
  • b
)
Object

Parameters:

Returns:

init

(
  • options
)

Options: name {string} sleep {boolean} scale {number} debug {boolean}

Parameters:

isMouseDown

() Boolean

Returns:

mouseDownAt

(
  • x
  • y
)

Enables dragging B2D objects with the mouse.

Parameters:

mouseUp

()

removeElementByIndex

(
  • index
)

Parameters:

removeElementByUid

(
  • uid
)

Parameters:

update

()