@@ -1095,6 +1095,47 @@ Phaser.Physics.P2.Body.prototype = {
10951095
10961096 } ,
10971097
1098+ loadPhaserPolygon : function ( key , object , options ) {
1099+ var data = this . game . cache . getPhysicsData ( key , object ) ;
1100+
1101+ if ( data . length === 1 )
1102+ {
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 ) ;
1113+ }
1114+ else
1115+ {
1116+
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 ( ) ;
1129+
1130+ return true ;
1131+
1132+ }
1133+
1134+ return false ;
1135+
1136+ } ,
1137+
1138+
10981139 /**
10991140 * Reads the shape data from a physics data file stored in the Game.Cache and adds it as a polygon to this Body.
11001141 *
@@ -1107,8 +1148,84 @@ Phaser.Physics.P2.Body.prototype = {
11071148 * @param {boolean|number } [options.removeCollinearPoints=false] - Set to a number (angle threshold value) to remove collinear points, or false to keep all points.
11081149 * @return {boolean } True on success, else false.
11091150 */
1110- loadPolygon : function ( key , object , options ) {
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 ) ;
11111182
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+ } ,
1227+
1228+ loadPolygon : function ( key , object , options ) {
11121229 var data = this . game . cache . getPhysicsData ( key , object ) ;
11131230
11141231 if ( data . length === 1 )
@@ -1132,7 +1249,7 @@ Phaser.Physics.P2.Body.prototype = {
11321249 for ( var i = 0 ; i < data . length ; i ++ )
11331250 {
11341251 var vertices = [ ] ;
1135-
1252+ console . log ( data [ i ] . filter . categoryBits )
11361253 for ( var s = 0 ; s < data [ i ] . shape . length ; s += 2 )
11371254 {
11381255 vertices . push ( [ this . world . pxmi ( data [ i ] . shape [ s ] ) , this . world . pxmi ( data [ i ] . shape [ s + 1 ] ) ] ) ;
0 commit comments