Jump to content

P2 body problem with group rotation


Faktori
 Share

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 :

6ea8ac939072d974b295a3549b46ab53.gif

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.setCircle(7);
    this.scale.setTo(scale);
    this.anchor.setTo(0.5);
    this.body.setCollisionGroup(enemyCG);
    this.body.static = true;
    this.body.collides(playerCG);

    game.add.existing(this);

    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

I'm not sure what your question is. They're rotating but you don't want them to? Or the bodies aren't moving... but you did that on purpose with static = true? They're not being affected by gravity and you know why but you can't fix it?

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...
 Share

  • Recently Browsing   0 members

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