Hello :).
I need to rotate the element shown below at the circle point. This is a character consisting of a weapon (a rectangle) and a character (a circle). I want to rotate the character for an attack only with respect to the circle. The rotate function for the body does not support changing the pivot point. The rotate function for Composite supports. When I created a character as a Composite it exploded at a collision. And I can not use the Composite function setVelocity.
var circleChamp = Bodies.circle(400, 200, 10,{
render: {
fillStyle: 'transparent',
strokeStyle: '#006516',
lineWidth: 2
}
});
var weapon = Bodies.rectangle(410,185,3,30,{
render: {
fillStyle: 'transparent',
strokeStyle: '#000',
lineWidth: 1
}
});
var champ = Body.create({
parts: [circleChamp,weapon],
frictionAir:0.3
});
My question is:
Not sure exactly why composites are breaking here, but you can look at the source code for Composite.rotate and see how it's possible to rotate a single body about a point:
var cos = Math.cos(rotation),
sin = Math.sin(rotation);
var dx = body.position.x - point.x,
dy = body.position.y - point.y;
Body.setPosition(body, {
x: point.x + (dx * cos - dy * sin),
y: point.y + (dx * sin + dy * cos)
});
Body.rotate(body, rotation);
I guess it might be nice to build this into Body.rotate?
This is exactly what i need thanks.
Most helpful comment
Not sure exactly why composites are breaking here, but you can look at the source code for Composite.rotate and see how it's possible to rotate a single body about a point:
I guess it might be nice to build this into
Body.rotate?