Jump to content

Rotate Physic object around a moving object


Recommended Posts



I am new to Phaser and I have some issues with P2 Physics


My problem


I have a moving object in the middle with a polygon physic shape (player).

I want to rotate something like a "middle age shield" around the flying object with a custom polygon physic shape.

The shield has an x offset of -30px;


My current solution (for a shield without an offset)


I use a  LockConstraint for that:

this.shield.body.fixedRotation = true;game.physics.p2.createLockConstraint(this.shield, this.player);

For rotation I change manual the angle (I want no smooth transition):

function rotateShield (position) {    if (position == 'top') {        this.rotateShield(0);        return;    }    if (position == 'right') {        this.rotateShield(90);        return;    }    if (position == 'bottom') {        this.rotateShield(180);        return;    }    if (position == 'left') {        this.rotateShield(-90);        return;    }    console.log("shield has unknown position: " + position);};function rotateShield(angle) {    // kill to avoid collision on rotation    this.shield.kill();    this.shield.angle = angle;    this.shield.body.angle = angle;    this.shield.revive();};

All is working as expected.

The shield is moving with the player.

Physics and sprite of the shield is rotated correctly. 


But my shield has an offset, so I tried:

game.physics.p2.createLockConstraint(this.shield, this.player, [-30, 0]);

That is working if the shield is rotated to the top, but not to the other sites.for other angles.

In the future the angles are not fixed to top, bottom, right, left ... so it is not solution to recreate the Constraint with an angle and x/y offset.


Is their another solution? 



I tried a lot until I found this constraint solution.

First I manually changes the x and y coordinates in the update loop (that was very buggy).

Then I tried to add the shield as a child, but I was not able to set a custom physic body.

Then I tried groups, with the same result as addChild. I was not able to set a custom separated physic body.

Link to comment
Share on other sites

I found one solution,

a hidden base(point):

    this.base = this.group.create(0,0);    game.physics.p2.enable(this.base);    this.base.body.clearShapes();    this.base.body.fixedRotation = true;    game.physics.p2.createLockConstraint(this.base, this.player, [-30, 0]);    game.physics.p2.createLockConstraint(this.shield, this.base);


Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...