Aggregate Bodies in PhysicsJS

312 Views Asked by At

I would like to make a custom physicsjs body type called "player" which is a composition of 2 circles and one square. The figure would look like a square with a circle attached to the left and right sides of the square. I was thinking of just making a custom polygon into this shape, however the circles will not be true circles, but instead look more like a hexagon. Any ideas how to make physicsjs draw a curved line between verticies instead of a straight line or how to combine existing bodies into one new body?

1

There are 1 best solutions below

2
On

composite bodies still need to be built in. There's no easy way to do this, but you can create a custom body that creates other bodies (eg, custom body that extends a square, that creates two circles). Just add a "connect" and "disconnect" method to the custom body so you can add and remove the extra circle bodies when it's added to a world.

Then you can use verlet constraints to attach them together.

As for the appearance, you'd need to find a way to draw that yourself with canvas. If you wanted to have the physics of a curved polygon, you'd have to write that yourself. So it's probably easier to just skin it with an image. To do that just set "body.view = myImage"

This is a bit outdated, but has some examples: http://flippinawesome.org/2013/12/02/building-a-2d-browser-game-with-physicsjs/