Jump to content

P2 body problem with group rotation


Recommended Posts

Hi guys, working on a project for school I just came accross a problem I can't seem to find a solution about. Here it is :


So I have this enemy group rotating. The sprites are moving correcty but the bodies are staying in place because I set them to static = true. I have a gravity going on and I don't want them to be affected by it that's why they're set to static.

Here's my code for these : 

EnemyGroup = function(positionX, positionY, radius, angle, rotateSpeed, clockwise) {
    Phaser.Group.call(this, game);
    this.position = {x:positionX, y:positionY};
    this.radius   = radius;
    this.pivot.x  = positionX;
    this.pivot.y  = positionY;
    this.angle    = angle;
    this.rotateSpeed = rotateSpeed;
    this.clockwise   = clockwise;

    this.addChild(new Enemy(this.position.x - this.radius, this.position.y, 0.4));
    this.addChild(new Enemy(this.position.x + this.radius, this.position.y, 0.4));
    this.addChild(new Enemy(this.position.x, this.position.y - this.radius, 0.4));
    this.addChild(new Enemy(this.position.x, this.position.y + this.radius, 0.4));

    this.update = function(){
        if (this.clockwise) {
            this.angle += rotateSpeed;
        } else if (!this.clockwise) {
            this.angle -= rotateSpeed;

EnemyGroup.prototype = Object.create(Phaser.Group.prototype);
EnemyGroup.prototype.constructor = EnemyGroup;

Enemy = function(positionX, positionY, scale) {
    Phaser.Sprite.call(this, game, positionX, positionY, 'enemy');
    game.physics.p2.enable(this, true);
    this.body.static = true;


    this.update = function(){

Enemy.prototype = Object.create(Phaser.Sprite.prototype);
Enemy.prototype.constructor = Enemy;

The solution must be quite simple - I just don't see it at the moment. any clue ?

Maybe have the rotation happening on single enemies with a pivot based on their parents - instead of rotating the whole group ?
Thanks for reading !

Link to comment
Share on other sites

They're rotating, I want them to ! But the bodies are staying in place. I want the bodies to stick with the sprites while they're rotating ! 
I have gravity going on to simulate water flux, but I don't want the enemies to be affected by that gravity that's why I set static to true.

Feel free to ask if I'm still not clear enough hehe


EDIT : So I was thinking the problem came from each enemy having a static body - but even if they don't have a static body the body will stay in place while the sprite is actually moving.
The problem might be coming from how I handle rotation with the angle property on my group. Will try some other stuff. If you have any idea ? thanks

Apparently an option would be to use revolute constraint but I'm really confused about how it works and how to apply it to my situation.

Link to comment
Share on other sites

  • 8 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

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