Matter-js: @question How to rotate element by point without Composite.

Created on 23 Apr 2017  路  2Comments  路  Source: liabru/matter-js

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:

  1. How to solve my problem to keep the pieces together.
  2. I could move them smoothly.
  3. And they do not fall apart in the event of a collision.
feature-request improve

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:

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?

All 2 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jack-guy picture jack-guy  路  3Comments

djipco picture djipco  路  4Comments

ShadewEnder picture ShadewEnder  路  3Comments

TimuJiang picture TimuJiang  路  4Comments

Zhaopengyang picture Zhaopengyang  路  3Comments