planck.js icon indicating copy to clipboard operation
planck.js copied to clipboard

Convex shape validator fails to validate bucket shape

Open CreativeTechGuy opened this issue 3 years ago • 2 comments
trafficstars

Example code:

const bucket = new planck.Polygon([
    { x: -30, y: -26 },
    { x: -26, y: -18 },
    { x: -20, y: 26 },
    { x: 22, y: 26 },
    { x: 28, y: -18 },
    { x: 32, y: -26 },
    { x: 32, y: 30 },
    { x: -30, y: 30 },
].map((v) => new planck.Vec2(v)));
console.log(bucket.validate()); // This shows true

Screenshot of the points on a coordinate plane:

image

This is not a convex shape, but for some reason the validator says it is. This might be related to #218 although I am not repeating my start/end points so there is probably a different issue.

CreativeTechGuy avatar Nov 20 '22 02:11 CreativeTechGuy

Okay I think I figured it out. When constructing a Polygon, it'll generate the convex hull and strip out the remaining points. So in my screenshot, just points 1, 6, 7, 8 will remain. This all happens before validate can be called. So by the time you try to validate it, the shape has already been changed to a convex hull and so it'll be true.

It seems like the validate method should be static on the Polygon class since it doesn't work after the polygon has already been constructed.

CreativeTechGuy avatar Nov 20 '22 04:11 CreativeTechGuy

I only find one testbed example where this is used, I guess this was made to test the conversion code (rather than validate user input). Anyway, if you would like please free to make a pull request and convert it to a static method.

shakiba avatar Jul 07 '23 02:07 shakiba