Jump to content

[BUG]? - P2 Physics - nested sprites - weird behaviour


Matthias Elephant
 Share

Recommended Posts

Hi guys,

i have a problem with nested sprites with enabled body.

I need to add a child with a body to a parent with a body. In the example you can see that the body of the children isn't at the same position as the sprite himself. In addition if i rotate the parent, only the childrens sprite, but not the body will rotate.

Just copy the following code into the example: http://phaser.io/examples/v2/p2-physics/basic-movement#gv

var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update });

function preload() {
    game.load.image('atari', 'assets/sprites/atari130xe.png');
}

var sprite;
var children;

function create() {
	game.physics.startSystem(Phaser.Physics.P2JS);

	children = game.add.sprite(100, 100, 'atari');
	children.scale.set(0.5);
	game.physics.p2.enable(children, true);
	children.body.setZeroDamping();
	children.body.moveRight(10);

    sprite = game.add.sprite(200, 200, 'atari');
	game.physics.p2.enable(sprite, true);
	sprite.body.setZeroDamping();
	sprite.body.moveRight(10);
	//sprite.body.rotateLeft(10);
	
	sprite.addChild(children);
}

function update() {
}

 

You can help me?
Thanks :)

Link to comment
Share on other sites

  • 1 month later...

I've been trying this too and same problem.

 

var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update });

function preload() {

    game.load.image('atari', 'assets/sprites/atari130xe.png');
	game.load.image('sky', 'assets/skies/sunset.png');

}

var sprite;
var cursors;

function create() {

    game.add.image(0, 0, 'sky');

	//	Enable p2 physics
	game.physics.startSystem(Phaser.Physics.P2JS);

    //  Make things a bit more bouncey
    game.physics.p2.defaultRestitution = 0.8;

    //  Add a sprite
	sprite = game.add.sprite(200, 200, 'atari');
	child = game.add.sprite(200, 200, 'atari');
	
	sprite.addChild(child);

    //  Enable if for physics. This creates a default rectangular body.
	game.physics.p2.enable(sprite, true, true);

    //  Modify a few body properties
    // child.body.kinematic = true;
	sprite.body.setZeroDamping();
	sprite.body.fixedRotation = true;

    text = game.add.text(20, 20, 'move with arrow keys', { fill: '#ffffff' });

    cursors = game.input.keyboard.createCursorKeys();

}

function update() {

	sprite.body.setZeroVelocity();

    if (cursors.left.isDown)
    {
    	sprite.body.moveLeft(400);
    }
    else if (cursors.right.isDown)
    {
    	sprite.body.moveRight(400);
    }

    if (cursors.up.isDown)
    {
    	sprite.body.moveUp(400);
    }
    else if (cursors.down.isDown)
    {
    	sprite.body.moveDown(400);
    }

}

How do you make the body follow the child?

Link to comment
Share on other sites

  • 2 months later...
  • 1 year later...

Has there been any solution for this? It seems the child collision body is placed in the world due to the its relative coordinates to the parent. So when I add a child at 0,0 relative to the parent it shows up at worldX=0, worldY=0 when P2 physics is active.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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