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

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...

How to show number of registered users in Laravel based on usertype?

i'm trying to display data from the database in the admin dashboard i used this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count(); echo $users; ?> and i have successfully get the correct data from the database but what if i want to display a specific data for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype i tried this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count()->WHERE usertype =admin; echo $users; ?> but it didn't work, what am i doing wrong? source https://stackoverflow.com/questions/68199726/how-to-show-number-of-registered-users-in-laravel-based-on-usertype

The number of nodes and edges I calculated manually and by a method provided by networkx library dont match

I am trying to analyse a graph using networkx library in Python. I have implemented a function that returns the nodes and edges of that graph. After that I counted the number of nodes and edges by implementing the len() function for nodes and edges, which gave me 17 for both nodes and edges. However, when I used the method graph.number_of_nodes() of networkx library it gave me 53. My code: import pandas as pd import networkx as nx import matplotlib.pyplot as plt header_list = ["a","b","w"] E =pd.read_csv("aves-barn-swallow-contact-network.edges",sep=" ",header= None,names=header_list) # print(E.head()) # prints first few rows of the csv file G = nx.from_pandas_edgelist(E,"a","b",["w"]) nx.draw(G) plt.show() class GraphAnalyse: #finding nodes and edges def nodeEdge(self,Graph): nodes = Graph.nodes() edges = Graph.edges() ...