instantia Posted June 5, 2014 Share Posted June 5, 2014 I turned to reflect sprite. this.obj.scale.setTo(1, -1); How to reflect the polygon body p2 physic? body.setTo(1, -1) not working Link to comment Share on other sites More sharing options...
instantia Posted June 6, 2014 Author Share Posted June 6, 2014 screen Link to comment Share on other sites More sharing options...
instantia Posted June 6, 2014 Author Share Posted June 6, 2014 Create a new polygon for left? Link to comment Share on other sites More sharing options...
PixelPicoSean Posted June 6, 2014 Share Posted June 6, 2014 I guess you need to create another body for left since body does not have any flip settings and sprite.scale only affects the graphic. Link to comment Share on other sites More sharing options...
ZoomBox Posted June 6, 2014 Share Posted June 6, 2014 Actually, in your case, it's the X axis that should be inverted.So this.scale.setTo(-1,1); Link to comment Share on other sites More sharing options...
ZoomBox Posted June 6, 2014 Share Posted June 6, 2014 But if it still doesn't work, I think you could reset each of its X coordinates.// Considering its anchor is [0.5,0.5]for(var i=0; i<this.body.shapes[0].vertices.length;i++){ this.body.shapes[0].vertices[i][0] = -this.body.shapes[0].vertices[i][0];}If it doesn't not work (it probably wouldn't since you would be changing the geometry of an existing shape), you could create your own "vertices" tab like so:var myVertices = [];for(var i=0; i<this.body.shapes[0].vertices.length;i++){ myVertices.push([-this.body.shapes[0].vertices[i][0], this.body.shapes[0].vertices[i][1]]);}// Now I have a vertices array, with coordinates (which looks like [[-200,200],[-150,100],[50, 150],...])// I erase the current bodythis.clearShapes();// I set the new body with my new coordinatesthis.loadPolygon(myVertices); Link to comment Share on other sites More sharing options...
instantia Posted June 6, 2014 Author Share Posted June 6, 2014 Created a new polygon.When the direction of changing.this.obj.body.clearShapes();this.obj.body.loadPolygon('physics', name+'_'+this.dir);Yet polygons same. But here is a bug: Why shifted polygin? Link to comment Share on other sites More sharing options...
instantia Posted June 6, 2014 Author Share Posted June 6, 2014 this is because this.obj.scale.setTo. I do not know how to fix ( Link to comment Share on other sites More sharing options...
ZoomBox Posted June 6, 2014 Share Posted June 6, 2014 What you coordinates looks like ?Show us you polygons values. Link to comment Share on other sites More sharing options...
instantia Posted June 6, 2014 Author Share Posted June 6, 2014 { "salmon_right": [ { "density": 2, "friction": 0, "bounce": 0, "filter": { "categoryBits": 1, "maskBits": 65535 }, "shape": [ 29, 84 , 28, 69 , 73, 65 , 74, 90 ] } , { "density": 2, "friction": 0, "bounce": 0, "filter": { "categoryBits": 1, "maskBits": 65535 }, "shape": [ 190, 95 , 122, 111 , 91, 104 , 101, 32 , 115, 23 , 155, 26 , 196, 53 ] } , { "density": 2, "friction": 0, "bounce": 0, "filter": { "categoryBits": 1, "maskBits": 65535 }, "shape": [ 73, 65 , 101, 32 , 91, 104 , 74, 90 ] } , { "density": 2, "friction": 0, "bounce": 0, "filter": { "categoryBits": 1, "maskBits": 65535 }, "shape": [ 233, 52 , 237, 65 , 212, 91 , 190, 95 , 196, 53 ] } ], "salmon_left": [ { "density": 2, "friction": 0, "bounce": 0, "filter": { "categoryBits": 1, "maskBits": 65535 }, "shape": [ 29, 84 , 28, 69 , 73, 65 , 74, 90 ] } , { "density": 2, "friction": 0, "bounce": 0, "filter": { "categoryBits": 1, "maskBits": 65535 }, "shape": [ 190, 95 , 122, 111 , 91, 104 , 101, 32 , 115, 23 , 155, 26 , 196, 53 ] } , { "density": 2, "friction": 0, "bounce": 0, "filter": { "categoryBits": 1, "maskBits": 65535 }, "shape": [ 73, 65 , 101, 32 , 91, 104 , 74, 90 ] } , { "density": 2, "friction": 0, "bounce": 0, "filter": { "categoryBits": 1, "maskBits": 65535 }, "shape": [ 233, 52 , 237, 65 , 212, 91 , 190, 95 , 196, 53 ] } ] } Link to comment Share on other sites More sharing options...
instantia Posted June 7, 2014 Author Share Posted June 7, 2014 The interesting thing is that if you put the shift on the same "right", everything works fine. if (this.obj.angle !== this.angleOld) { if (Math.abs(this.obj.angle) > 90) { this.obj.scale.setTo(this.scale, -this.scale); this.dir = 'left'; } else if (Math.abs(this.obj.angle) <= 90) { this.obj.scale.setTo(this.scale, this.scale); this.dir = 'right'; } if (this.dir !== this.dirOld) { /*console.log(this.obj.height);*/ this.obj.body.clearShapes(); this.obj.body.loadPolygon('physics', this.name+'_'+this.dir); } this.dirOld = this.dir; }- not working. if (this.obj.angle !== this.angleOld) { if (Math.abs(this.obj.angle) > 90) { this.obj.scale.setTo(this.scale, -this.scale); this.dir = 'right'; } else if (Math.abs(this.obj.angle) <= 90) { this.obj.scale.setTo(this.scale, this.scale); this.dir = 'right'; } if (this.dir !== this.dirOld) { /*console.log(this.obj.height);*/ this.obj.body.clearShapes(); this.obj.body.loadPolygon('physics', this.name+'_'+this.dir); } this.dirOld = this.dir; }- working.How so? left = right. Link to comment Share on other sites More sharing options...
instantia Posted June 8, 2014 Author Share Posted June 8, 2014 solved the problem so: if (Math.abs(this.obj.angle) > 90) this.dir = 'left';else if (Math.abs(this.obj.angle) <= 90) this.dir = 'right';if (this.dir !== this.dirOld) { this.obj.scale.setTo(this.scale, this.scale); this.obj.body.clearShapes(); this.obj.body.loadPolygon('physics', this.name+'_'+this.dir);}if (Math.abs(this.obj.angle) > 90) this.obj.scale.setTo(this.scale, -this.scale);else if (Math.abs(this.obj.angle) <= 90) this.obj.scale.setTo(this.scale, this.scale);this.dirOld = this.dir;a bit expensive( Link to comment Share on other sites More sharing options...
Recommended Posts