@@ -1097,41 +1097,79 @@ Phaser.Physics.P2.Body.prototype = {
10971097
10981098 loadPhaserPolygon : function ( key , object , options ) {
10991099 var data = this . game . cache . getPhysicsData ( key , object ) ;
1100-
1101- if ( data . length === 1 )
1100+ //cycle through the fixtures
1101+ for ( var i = 0 ; i < data . length ; i ++ )
11021102 {
1103- var temp = [ ] ;
1104-
1105- data = data . pop ( )
1106- // We've a list of numbers
1107- for ( var i = 0 , len = data . shape . length ; i < len ; i += 2 )
1108- {
1109- temp . push ( [ data . shape [ i ] , data . shape [ i + 1 ] ] ) ;
1110- }
1111-
1112- return this . addPolygon ( options , temp ) ;
1103+ var fixtureData = data [ i ]
1104+ this . addFixture ( fixtureData )
11131105 }
1114- else
1115- {
11161106
1117- // We've multiple Convex shapes, they should be CCW automatically
1118- var cm = p2 . vec2 . create ( ) ;
1119-
1120- //cycle through the fixtures
1121- for ( var i = 0 ; i < data . length ; i ++ )
1122- {
1123- var fixtureData = data [ i ]
1124- this . addPolygonShape ( fixtureData )
1125- }
1126-
1127- this . data . aabbNeedsUpdate = true ;
1128- this . shapeChanged ( ) ;
1107+ this . data . aabbNeedsUpdate = true ;
1108+ this . shapeChanged ( ) ;
11291109
1130- return true ;
1110+ return false ;
11311111
1112+ } ,
1113+
1114+ /**
1115+ * Add a polygon fixture. This is used during #loadPhaserPolygon.
1116+ *
1117+ * @method Phaser.Physics.P2.Body#addPolygonFixture
1118+ * @param {string } fixtureData - The data for the fixture.
1119+ * It contains: isSensor, filter (collision) and the actual polygon shapes
1120+ */
1121+
1122+ addFixture : function ( fixtureData ) {
1123+ console . log ( 'addPolygonFixture' , fixtureData )
1124+
1125+ if ( fixtureData . circle ) {
1126+ //a circle has unfortunately no position in p2
1127+ var shape = new p2 . Circle ( this . world . pxm ( fixtureData . circle . radius ) )
1128+ shape . collisionGroup = fixtureData . filter . categoryBits
1129+ shape . collisionMask = fixtureData . filter . maskBits
1130+ shape . sensor = fixtureData . isSensor
1131+
1132+ this . data . addShape ( shape ) ;
1133+ } else {
1134+ polygons = fixtureData . polygons
1135+ var cm = p2 . vec2 . create ( ) ;
1136+
1137+ for ( var i = 0 ; i < polygons . length ; i ++ ) {
1138+ shapes = polygons [ i ]
1139+
1140+ var vertices = [ ] ;
1141+ for ( var s = 0 ; s < shapes . length ; s += 2 )
1142+ {
1143+ vertices . push ( [ this . world . pxmi ( shapes [ s ] ) , this . world . pxmi ( shapes [ s + 1 ] ) ] ) ;
1144+ }
1145+
1146+ var shape = new p2 . Convex ( vertices ) ;
1147+
1148+ // Move all vertices so its center of mass is in the local center of the convex
1149+ for ( var j = 0 ; j !== shape . vertices . length ; j ++ )
1150+ {
1151+ var v = shape . vertices [ j ] ;
1152+ p2 . vec2 . sub ( v , v , shape . centerOfMass ) ;
1153+ }
1154+
1155+ p2 . vec2 . scale ( cm , shape . centerOfMass , 1 ) ;
1156+
1157+ cm [ 0 ] -= this . world . pxmi ( this . sprite . width / 2 ) ;
1158+ cm [ 1 ] -= this . world . pxmi ( this . sprite . height / 2 ) ;
1159+
1160+ shape . updateTriangles ( ) ;
1161+ shape . updateCenterOfMass ( ) ;
1162+ shape . updateBoundingRadius ( ) ;
1163+
1164+
1165+ shape . collisionGroup = fixtureData . filter . categoryBits
1166+ shape . collisionMask = fixtureData . filter . maskBits
1167+ shape . sensor = fixtureData . isSensor
1168+
1169+ this . data . addShape ( shape , cm ) ;
11321170 }
1171+ }
11331172
1134- return false ;
11351173
11361174 } ,
11371175
@@ -1148,82 +1186,6 @@ Phaser.Physics.P2.Body.prototype = {
11481186 * @param {boolean|number } [options.removeCollinearPoints=false] - Set to a number (angle threshold value) to remove collinear points, or false to keep all points.
11491187 * @return {boolean } True on success, else false.
11501188 */
1151-
1152- addPolygonShape : function ( fixtureData ) {
1153- fixtureData . density
1154- fixtureData . filter
1155- fixtureData . friction
1156- polygons = fixtureData . polygons
1157-
1158- var cm = p2 . vec2 . create ( ) ;
1159-
1160- for ( var i = 0 ; i < polygons . length ; i ++ ) {
1161- shapes = polygons [ i ]
1162-
1163- var vertices = [ ] ;
1164- for ( var s = 0 ; s < shapes . length ; s += 2 )
1165- {
1166- vertices . push ( [ this . world . pxmi ( shapes [ s ] ) , this . world . pxmi ( shapes [ s + 1 ] ) ] ) ;
1167- }
1168-
1169- var c = new p2 . Convex ( vertices ) ;
1170-
1171- // Move all vertices so its center of mass is in the local center of the convex
1172- for ( var j = 0 ; j !== c . vertices . length ; j ++ )
1173- {
1174- var v = c . vertices [ j ] ;
1175- p2 . vec2 . sub ( v , v , c . centerOfMass ) ;
1176- }
1177-
1178- p2 . vec2 . scale ( cm , c . centerOfMass , 1 ) ;
1179-
1180- cm [ 0 ] -= this . world . pxmi ( this . sprite . width / 2 ) ;
1181- cm [ 1 ] -= this . world . pxmi ( this . sprite . height / 2 ) ;
1182-
1183- c . updateTriangles ( ) ;
1184- c . updateCenterOfMass ( ) ;
1185- c . updateBoundingRadius ( ) ;
1186-
1187- this . data . addShape ( c , cm ) ;
1188- }
1189-
1190- /*for (var i = 0; i < data.length; i++)
1191- {
1192-
1193- polygons = fixtureData.polygons
1194-
1195- for (var k = 0; k < polygons.length; k++)
1196- {
1197- polygon = polygons[k] //array of numbers
1198-
1199- var vertices = [];
1200- for (var s = 0; s < polygon.length; s += 2)
1201- {
1202- vertices.push([ this.world.pxmi(polygon[s]), this.world.pxmi(polygon[s + 1]) ]);
1203- }
1204-
1205- var c = new p2.Convex(vertices);
1206-
1207- // Move all vertices so its center of mass is in the local center of the convex
1208- for (var j = 0; j !== c.vertices.length; j++)
1209- {
1210- var v = c.vertices[j];
1211- p2.vec2.sub(v, v, c.centerOfMass);
1212- }
1213-
1214- p2.vec2.scale(cm, c.centerOfMass, 1);
1215-
1216- cm[0] -= this.world.pxmi(this.sprite.width / 2);
1217- cm[1] -= this.world.pxmi(this.sprite.height / 2);
1218-
1219- c.updateTriangles();
1220- c.updateCenterOfMass();
1221- c.updateBoundingRadius();
1222-
1223- this.data.addShape(c, cm);
1224- }
1225- }*/
1226- } ,
12271189
12281190 loadPolygon : function ( key , object , options ) {
12291191 var data = this . game . cache . getPhysicsData ( key , object ) ;
0 commit comments