Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@question How to rotate element by point without Composite. #410

Closed
Prendki opened this issue Apr 23, 2017 · 2 comments
Closed

@question How to rotate element by point without Composite. #410

Prendki opened this issue Apr 23, 2017 · 2 comments

Comments

@Prendki
Copy link

Prendki commented Apr 23, 2017

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.
@liabru
Copy link
Owner

liabru commented Apr 25, 2017

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?

@Prendki
Copy link
Author

Prendki commented May 14, 2017

This is exactly what i need thanks.

@liabru liabru closed this as completed in 749ed50 Jul 6, 2017
liabru added a commit that referenced this issue Jul 6, 2017
* constraint-fixes: (31 commits)
  tweaked Example.doublePendulum
  added Composite.bounds
  upgraded matter-wrap
  added Examples.ragdoll
  upgraded matter-tools
  fixed various constraints in examples
  improved spring and pin rendering
  update alpha build
  added Example.constraints
  added constraint.render.type and constraint.render.anchor
  update alpha build
  added constraint damping
  update alpha build
  changed lint rules
  added Example.doublePendulum
  added rotation point parameter to Body.rotate, closes #410
  fix constraint.angularStiffness
  fix soft body stiffness
  fixed Examples.compound constraint offset
  improved Example.chains using pin constraint
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants