Matter-js: Matter.Bodies.fromVertices not working

Created on 2 Sep 2017  路  5Comments  路  Source: liabru/matter-js

Hi.. i've tried implementing the Matter.Bodies.fromVertices to make a convex shape ( included decomp.min.js ), but it's not working..... Please provide a pure javascript code example or a detailed documentation.

question

Most helpful comment

Never mind. Worked it out. I had to pass objects of vectors in the vertex data array like below

vertices = [
        {x : 0 , y : 0},
        {x : 0 , y : 50},
        {x : 25 , y : 25},
        {x : 50 , y : 50},
        {x : 50 , y : 0}
]

All 5 comments

Same here. I have included poly-decomp properly. It gives an error

var vert = [
        [0,0],
        [10,100],
        [20,120],
        [30,60],
        [40,100],
        [50,200],
        [60,100],
        [70,200],
        [80,180],
        [90,100],
        [100,180],
        [100,0],
        [0,0]
    ]
var terrain = Bodies.fromVertices(400,200, vert , {isStatic : true} );

Above code gives error

Uncaught TypeError: Cannot read property 'type' of undefined at Object.Composite.add (matter.js:1291) at index.html:47

Never mind. Worked it out. I had to pass objects of vectors in the vertex data array like below

vertices = [
        {x : 0 , y : 0},
        {x : 0 , y : 50},
        {x : 25 , y : 25},
        {x : 50 , y : 50},
        {x : 50 , y : 0}
]

Thanks for the solution... I've used another way though... Matter.Vertices.fromPath("x1,y1,x2,y2,.....") ... it returns the object array from the sequential vertices passed to it as a string...

Yes, you need to pass an array of vectors (so objects with x and y). Looks like you guys figured it out!

Running in to this issue using the most recent version of Matter from NPM.

setupPhysicsBodies()
{
        Matter.Bodies.fromVertices( 400, 200, Matter.Vertices.fromPath( this.getWorldMaskPath() ) )
}

//Last function i've used.
getWorldMaskPath()
{
        return '869 0 894 89 855.7 270.7 937.3 410.7 994 540 1099 564 1145.7 540 1207.3 505.7 1207.3 564 1189 600.7 1164 672.3 1192.3 782.3 1135.7 879 1185.7 1012.3 1200.7 1080 0 1080 0 0 869 0'.split( ' ' ).map( point => Math.round( point ) ).join( ',' )
}

The 'getWorldMaskPath' function returns a comma seperated string.

I also tried a function before this that returned an array with { x, y } objects and one that returned Matter.Vector.create( x, y ) objects.

All functions I've tried all returned the same error discussed above (@sayamqazi)

Probably doing something wrong here but if I console.log 'getWorldMaskPath' it looks like what you guys described?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mrspeaker picture mrspeaker  路  3Comments

christianmalek picture christianmalek  路  4Comments

probityrules picture probityrules  路  4Comments

cluber22 picture cluber22  路  3Comments

car1ot picture car1ot  路  3Comments