I'm currently working on a Phaser 3 game project, using Matter Physics. I created a matter.image object. In order to create a physics shapes, which I have to determine the coordinates of each vertex of the polygon outline like this, I used Physics Editor (https://www.codeandweb.com/physicseditor). It generates a JSON file include vertices, you can refer to Attachment #1.
Now I want to calculate the horizontal distance between the physics shape and a point. For example, in this figure, the blue one in the left is physics shape, and the blue one in the left is the point, and the green line is the horizontal distance I want to calculate.
Code used to create the object:
// in the preload() function
this.load.json('sprite', '../assets/chisato-sprite.json');
// in the create() function
sprites = this.cache.json.get('sprite');
chisatoLeft = this.matter.add.image(480, 425, 'chisatoLeft', null, { shape: sprites.left });
How can I do that?
Thanks in advance.
Attachment #1 Excerpt of the json file
{
"generator_info": "Shape definitions generated with PhysicsEditor. Visit https://www.codeandweb.com/physicseditor",
"left": {
"type": "fromPhysicsEditor",
"label": "left",
"isStatic": true,
"density": 0.10000000149011612,
"restitution": 0,
"friction": 0.10000000149011612,
"frictionAir": 0.009999999776482582,
"frictionStatic": 0.5,
"collisionFilter": {
"group": 0,
"category": 1,
"mask": 255
},
"fixtures": [
{
"label": "",
"isSensor": false,
"vertices": [
[ { "x":71, "y":226.5 }, { "x":76.5, "y":215 }, { "x":57.5, "y":216 } ],
[ { "x":166, "y":197.5 }, { "x":182.5, "y":169 }, { "x":164.5, "y":177 } ],
[ { "x":33.5, "y":214 }, { "x":29.5, "y":203 }, { "x":24.5, "y":206 } ],
[ { "x":198.5, "y":44 }, { "x":176, "y":38.5 }, { "x":162, "y":48.5 }, { "x":175.5, "y":50 } ],
[ { "x":175.5, "y":50 }, { "x":162, "y":48.5 }, { "x":168.5, "y":59 } ],
[ { "x":61, "y":223.5 }, { "x":57.5, "y":216 }, { "x":29.5, "y":203 }, { "x":47, "y":215.5 } ],
[ { "x":192.5, "y":61 }, { "x":168.5, "y":59 }, { "x":173, "y":68.5 } ],
[ { "x":159, "y":195.5 }, { "x":164.5, "y":177 }, { "x":173, "y":68.5 }, { "x":156.5, "y":180 } ],
[ { "x":129, "y":17.5 }, { "x":86, "y":10.5 }, { "x":85, "y":10.5 }, { "x":29.5, "y":203 }, { "x":156.5, "y":180 }, { "x":173, "y":68.5 }, { "x":168.5, "y":59 }, { "x":162, "y":48.5 } ],
[ { "x":20.5, "y":273 }, { "x":17.5, "y":341 }, { "x":26, "y":399 }, { "x":265.5, "y":381 }, { "x":226, "y":234.5 }, { "x":87.5, "y":231 }, { "x":47, "y":249.5 } ],
[ { "x":43, "y":219.5 }, { "x":47, "y":215.5 }, { "x":29.5, "y":203 } ],
[ { "x":208.5, "y":166 }, { "x":204.5, "y":130 }, { "x":200, "y":140.5 } ],
[ { "x":57.5, "y":216 }, { "x":76.5, "y":215 }, { "x":156.5, "y":180 }, { "x":29.5, "y":203 } ],
[ { "x":5.5, "y":152 }, { "x":24.5, "y":206 }, { "x":29.5, "y":203 }, { "x":85, "y":10.5 }, { "x":13.5, "y":64 }, { "x":5.5, "y":92 } ],
[ { "x":52.5, "y":20 }, { "x":32, "y":35.5 }, { "x":13.5, "y":64 }, { "x":85, "y":10.5 } ],
[ { "x":185.5, "y":201 }, { "x":189, "y":199.5 }, { "x":200.5, "y":161 }, { "x":200, "y":140.5 }, { "x":182.5, "y":169 } ],
[ { "x":182.5, "y":169 }, { "x":200, "y":140.5 }, { "x":204.5, "y":130 }, { "x":173, "y":68.5 }, { "x":164.5, "y":177 } ],
[ { "x":299, "y":400 }, { "x":265.5, "y":381 }, { "x":26, "y":399 } ],
[ { "x":87.5, "y":231 }, { "x":226, "y":234.5 }, { "x":217, "y":226.5 }, { "x":152.5, "y":212 }, { "x":92.5, "y":220 } ],
[ { "x":152.5, "y":212 }, { "x":156.5, "y":180 }, { "x":76.5, "y":215 }, { "x":92.5, "y":220 } ]
]
}
]
},
Via Active questions tagged javascript - Stack Overflow https://ift.tt/TF70WUq
Comments
Post a Comment