Skip to main content

Phaser3, Matter physics, Calculate distance between a point and a physics shape

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

Popular posts from this blog

Where and how is this Laravel kernel constructor called? [closed]

Where and how is this Laravel kernel constructor called? public fucntion __construct(Application $app, $Router $roouter) { } I have read the documentation and some online tutorial but I can find any clear explanation. I am learning Laravel and I am wondering where does this kernel constructor receives its arguments from. "POSTMOTERM" CLARIFICATION: Here is more clarity.I have checked the boostrap/app.php and it is only used for boostrapping the interfaces into the container class. What is not clear to me is where and how the Kernel class is instatiated and the arguments passed to the object calling the constructor.Something similar to; obj = new kernel(arg1,arg2) or, is the framework using some magic functions somewhere? Special gratitude to those who burn their eyeballs and brain cells on this trivia before it goes into a full blown menopause alias "MARKED AS DUPLICATE". To some of the itchy-finger keyboard warriors, a.k.a The mods,because I believe in th...

Why is my reports service not connecting?

I am trying to pull some data from a Postgres database using Node.js and node-postures but I can't figure out why my service isn't connecting. my routes/index.js file: const express = require('express'); const router = express.Router(); const ordersCountController = require('../controllers/ordersCountController'); const ordersController = require('../controllers/ordersController'); const weeklyReportsController = require('../controllers/weeklyReportsController'); router.get('/orders_count', ordersCountController); router.get('/orders', ordersController); router.get('/weekly_reports', weeklyReportsController); module.exports = router; My controllers/weeklyReportsController.js file: const weeklyReportsService = require('../services/weeklyReportsService'); const weeklyReportsController = async (req, res) => { try { const data = await weeklyReportsService; res.json({data}) console...

Confusion between commands.Bot and discord.Client | Which one should I use?

Whenever you look at YouTube tutorials or code from this website there is a real variation. Some developers use client = discord.Client(intents=intents) while the others use bot = commands.Bot(command_prefix="something", intents=intents) . Now I know slightly about the difference but I get errors from different places from my code when I use either of them and its confusing. Especially since there has a few changes over the years in discord.py it is hard to find the real difference. I tried sticking to discord.Client then I found that there are more features in commands.Bot . Then I found errors when using commands.Bot . An example of this is: When I try to use commands.Bot client = commands.Bot(command_prefix=">",intents=intents) async def load(): for filename in os.listdir("./Cogs"): if filename.endswith(".py"): client.load_extension(f"Cogs.{filename[:-3]}") The above doesnt giveany response from my Cogs ...