@@ -33,7 +33,7 @@ const engineName = 'LittleJS';
3333 * @type {string }
3434 * @default
3535 * @memberof Engine */
36- const engineVersion = '1.17.4 ' ;
36+ const engineVersion = '1.17.5 ' ;
3737
3838/** Frames per second to update
3939 * @type {number }
@@ -10800,6 +10800,10 @@ class Box2dObject extends EngineObject
1080010800 * @param {Object } [fixture] */
1080110801 destroyFixture ( fixture ) { this . body . DestroyFixture ( fixture ) ; }
1080210802
10803+ /** Destroy all fixture from the body */
10804+ destroyAllFixtures ( )
10805+ { this . getFixtureList ( ) . forEach ( fixture => this . destroyFixture ( fixture ) ) ; }
10806+
1080310807 ///////////////////////////////////////////////////////////////////////////////
1080410808 // physics get functions
1080510809
@@ -11032,56 +11036,100 @@ class Box2dObject extends EngineObject
1103211036 }
1103311037}
1103411038
11039+ ///////////////////////////////////////////////////////////////////////////////
11040+ /**
11041+ * Box2D Static Object - Box2d with a static physics body
11042+ * @extends Box2dObject
11043+ * @memberof Box2D
11044+ */
11045+ class Box2dStaticObject extends Box2dObject
11046+ {
11047+ /** Create a LittleJS object with Box2d physics
11048+ * @param {Vector2 } [pos]
11049+ * @param {Vector2 } [size]
11050+ * @param {TileInfo } [tileInfo]
11051+ * @param {number } [angle]
11052+ * @param {Color } [color]
11053+ * @param {number } [renderOrder] */
11054+ constructor ( pos , size , tileInfo , angle = 0 , color , renderOrder = 0 )
11055+ {
11056+ const bodyType = box2d . bodyTypeStatic ;
11057+ super ( pos , size , tileInfo , angle , color , bodyType , renderOrder ) ;
11058+ }
11059+ }
11060+
11061+ ///////////////////////////////////////////////////////////////////////////////
11062+ /**
11063+ * Box2D Kiematic Object - Box2d with a kinematic physics body
11064+ * @extends Box2dObject
11065+ * @memberof Box2D
11066+ */
11067+ class Box2dKiematicObject extends Box2dObject
11068+ {
11069+ /** Create a LittleJS object with Box2d physics
11070+ * @param {Vector2 } [pos]
11071+ * @param {Vector2 } [size]
11072+ * @param {TileInfo } [tileInfo]
11073+ * @param {number } [angle]
11074+ * @param {Color } [color]
11075+ * @param {number } [renderOrder] */
11076+ constructor ( pos , size , tileInfo , angle = 0 , color , renderOrder = 0 )
11077+ {
11078+ const bodyType = box2d . bodyTypeKinematic ;
11079+ super ( pos , size , tileInfo , angle , color , bodyType , renderOrder ) ;
11080+ }
11081+ }
1103511082
1103611083///////////////////////////////////////////////////////////////////////////////
1103711084/**
1103811085 * Box2d Tile Layer
1103911086 * - adds Box2d support to tile layers
1104011087 * - creates static box2d fixtures for solid tiles
11041- * @extends TileLayer
11042- * @memberof TileLayers
11088+ * @extends Box2dObject
11089+ * @memberof Box2D
1104311090 */
11044- class Box2dTileLayer extends TileCollisionLayer
11091+ class Box2dTileLayer extends Box2dStaticObject
1104511092{
11046- /** Create a tile layer object
11047- * @param {Vector2 } pos - World space position
11048- * @param {Vector2 } size - World space size
11049- * @param {TileInfo } [tileInfo] - Tile info for layer
11050- * @param {number } [renderOrder] - Objects are sorted by renderOrder
11051- * @param {boolean } [useWebGL] - Should this layer use WebGL for rendering
11052- */
11053- constructor ( pos , size , tileInfo = tile ( ) , renderOrder = 0 , useWebGL = true )
11093+ /** Create a Box2d tile layer object
11094+ * @param {TileCollisionLayer } tileLayer - Tile layer for this object */
11095+ constructor ( tileLayer )
1105411096 {
11055- super ( pos , size . floor ( ) , tileInfo , renderOrder , useWebGL ) ;
11097+ ASSERT ( tileLayer instanceof TileCollisionLayer , 'tileLayer must be a TileCollisionLayer' ) ;
11098+ super ( tileLayer . pos , tileLayer . size ) ;
1105611099
11057- /** @property {Box2dStaticObject } - The box2d object */
11058- this . box2dObject = undefined ;
11100+ /** @property {TileLayer } - The tile layer */
11101+ this . tileLayer = tileLayer ;
11102+ this . addChild ( tileLayer ) ;
11103+ }
11104+
11105+ render ( )
11106+ {
11107+ // do not render fixtures, tile layer handles rendering
1105911108 }
1106011109
1106111110 /** Create box2d collision fixtures for solid tiles
1106211111 * @param {number } [friction]
1106311112 * @param {number } [restitution] */
1106411113 buildCollision ( friction = .2 , restitution = 0 )
1106511114 {
11066- // destroy any existing box2d object
11067- this . box2dObject ?. destroy ( ) ;
11115+ // destroy all fixtures and create new ones
11116+ this . destroyAllFixtures ( ) ;
1106811117
1106911118 // create box2d object for this layer
11070- this . box2dObject = new Box2dStaticObject ( this . pos , this . size ) ;
11071- this . box2dObject . color = CLEAR_BLACK ;
11072- this . box2dObject . lineColor = CLEAR_BLACK ;
11073- this . addChild ( this . box2dObject ) ;
11119+ const size = this . tileLayer . size ;
11120+ this . pos = this . tileLayer . pos . copy ( ) ;
11121+ this . size = size . copy ( ) ;
1107411122
1107511123 // track which tiles have been processed
1107611124 const processed = [ ] ;
11077- const getIndex = ( x , y ) => x + y * this . size . x ;
11125+ const getIndex = ( x , y ) => x + y * size . x ;
1107811126 const isSolidUnprocessed = ( x , y ) =>
1107911127 ! processed [ getIndex ( x , y ) ] &&
11080- this . getCollisionData ( vec2 ( x , y ) ) > 0 ;
11128+ this . tileLayer . getCollisionData ( vec2 ( x , y ) ) > 0 ;
1108111129
1108211130 // combine tiles into larger boxes
11083- for ( let x = 0 ; x < this . size . x ; ++ x )
11084- for ( let y = 0 ; y < this . size . y ; ++ y )
11131+ for ( let x = 0 ; x < size . x ; ++ x )
11132+ for ( let y = 0 ; y < size . y ; ++ y )
1108511133 {
1108611134 if ( ! isSolidUnprocessed ( x , y ) ) continue ;
1108711135
@@ -11111,9 +11159,9 @@ class Box2dTileLayer extends TileCollisionLayer
1111111159 processed [ getIndex ( x + rectX , y + rectY ) ] = true ;
1111211160
1111311161 // create a single fixture for the entire rectangle
11114- const size = vec2 ( width , height ) ;
11162+ const shapeSize = vec2 ( width , height ) ;
1111511163 const offset = vec2 ( x + width / 2 , y + height / 2 ) ;
11116- this . box2dObject . addBox ( size , offset , 0 , 0 , friction , restitution ) ;
11164+ this . addBox ( shapeSize , offset , 0 , 0 , friction , restitution ) ;
1111711165 }
1111811166 }
1111911167}
@@ -11648,50 +11696,6 @@ class Box2dPrismaticJoint extends Box2dJoint
1164811696 getMotorForce ( time ) { return this . box2dJoint . GetMotorForce ( 1 / time ) ; }
1164911697}
1165011698
11651- ///////////////////////////////////////////////////////////////////////////////
11652- /**
11653- * Box2D Static Object - Box2d with a static physics body
11654- * @extends Box2dObject
11655- * @memberof Box2D
11656- */
11657- class Box2dStaticObject extends Box2dObject
11658- {
11659- /** Create a LittleJS object with Box2d physics
11660- * @param {Vector2 } [pos]
11661- * @param {Vector2 } [size]
11662- * @param {TileInfo } [tileInfo]
11663- * @param {number } [angle]
11664- * @param {Color } [color]
11665- * @param {number } [renderOrder] */
11666- constructor ( pos , size , tileInfo , angle = 0 , color , renderOrder = 0 )
11667- {
11668- const bodyType = box2d . bodyTypeStatic ;
11669- super ( pos , size , tileInfo , angle , color , bodyType , renderOrder ) ;
11670- }
11671- }
11672-
11673- ///////////////////////////////////////////////////////////////////////////////
11674- /**
11675- * Box2D Kiematic Object - Box2d with a kinematic physics body
11676- * @extends Box2dObject
11677- * @memberof Box2D
11678- */
11679- class Box2dKiematicObject extends Box2dObject
11680- {
11681- /** Create a LittleJS object with Box2d physics
11682- * @param {Vector2 } [pos]
11683- * @param {Vector2 } [size]
11684- * @param {TileInfo } [tileInfo]
11685- * @param {number } [angle]
11686- * @param {Color } [color]
11687- * @param {number } [renderOrder] */
11688- constructor ( pos , size , tileInfo , angle = 0 , color , renderOrder = 0 )
11689- {
11690- const bodyType = box2d . bodyTypeKinematic ;
11691- super ( pos , size , tileInfo , angle , color , bodyType , renderOrder ) ;
11692- }
11693- }
11694-
1169511699///////////////////////////////////////////////////////////////////////////////
1169611700/**
1169711701 * Box2D Wheel Joint
0 commit comments