Jump to content

Arcade group collision


Tufan
 Share

Recommended Posts

I want to collide a group with itself but it seems broken.

sprite1 teleporting back to it's old position after colliding with sprite2. There's no problem with game.physics.arcade.collide(sprite, sprite2) but i need to collide more than 2 sprites.

I don't want any knockback. Couldn't find anything in the docs, google etc.

function create() {
    game.world.setBounds(0, 0, 1920, 1920);
    game.physics.startSystem(Phaser.Physics.ARCADE);
    sprites = game.add.group();

    sprite = game.add.sprite(game.world.centerX, game.world.centerY, 'skin1');
    sprites.add(sprite);
    game.physics.enable(sprite, Phaser.Physics.ARCADE);
    sprite.anchor.setTo(0.5, 0.5);
    sprite.scale.setTo(0.2, 0.2);
    radius = sprite.width / 2;

    sprite.body.setCircle(
        radius,
        (-radius + 0.5 * sprite.width  / sprite.scale.x),
        (-radius + 0.5 * sprite.height / sprite.scale.y)
    );
        
    sprite2 = game.add.sprite(game.world.centerX, game.world.centerY, 'skin1');
    sprites.add(sprite2);
    game.physics.enable(sprite2, Phaser.Physics.ARCADE);
    sprite2.anchor.setTo(0.5, 0.5);
    sprite2.scale.setTo(0.2, 0.2);
    radius = sprite2.width / 2;

    sprite2.body.setCircle(
        radius,
        (-radius + 0.5 * sprite2.width  / sprite2.scale.x),
        (-radius + 0.5 * sprite2.height / sprite2.scale.y)
    );
    
    game.camera.follow(sprite, Phaser.Camera.FOLLOW_LOCKON, 0.1, 0.1);
}

function update() {
    game.physics.arcade.collide(sprites, sprites);
    sprite.body.velocity.y = 0;
    sprite.body.velocity.x = 0;
    if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
    {
        sprite.body.velocity.x = -300;
    }
    else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
    {
        sprite.body.velocity.x = 300;
    }

    if (game.input.keyboard.isDown(Phaser.Keyboard.UP))
    {
        sprite.body.velocity.y = -300;
    }
    else if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN))
    {
        sprite.body.velocity.y = 300;
    }
}

function render() {
    game.debug.body(sprite);
    game.debug.body(sprite2);
}

 

Link to comment
Share on other sites

15 minutes ago, samid737 said:

Hi here is your code put into an example:

In the example the collision works fine? Are you sure you did not mis something out? In the example, the only difference is that the sprite1 and sprite2 starting positions in create().

Maybe you just can't notice difference with small sprites.

 

Link to comment
Share on other sites

1 hour ago, Tufan said:

sprite1 teleporting back to it's old position after colliding with sprite2

I think that's partly camera movement you're seeing. Nevertheless, the separation distance does look too large, and the circles seem to be penetrating each other well before the collision starts.

 

Link to comment
Share on other sites

44 minutes ago, samme said:

I think that's partly camera movement you're seeing. Nevertheless, the separation distance does look too large, and the circles seem to be penetrating each other well before the collision starts.

Is it possible to fix this with scaled and circle bodies? I'm pretty new to Phaser so i have no idea what to do with this situation.

Link to comment
Share on other sites

EDIT: Cancel that, the same issue exists (hickups) when making the bodies heavy.

Thats strange I did not notice that, but I understand your problem now. An alternative (more costly though) in case it is an issue that must be addressed: in P2 physics it sort of looks okay:

 

 

Link to comment
Share on other sites

19 minutes ago, samme said:

It does look like a bug. Can you avoid scaling the sprites? You could scale the images instead.

Yes, i can.

17 minutes ago, samid737 said:

Thats strange I did not notice that, but I understand your problem now. An alternative (more costly though) in case it is an issue that must be addressed: in P2 physics it sort of looks okay:

Well, the docs says there can be only one body with P2 physics so i don't think i can use P2.

"A game object can only have 1 physics body active at any one time, and it can't be changed until the object is destroyed." - Phaser.Physics.P2

Link to comment
Share on other sites

 

 

Yes in your case you have two different game objects in one group, each having a seperate body to replicate the example. However, using P2 would be futile since it does not fix your problem (see above). Scaling the images directly is the way out then.

Link to comment
Share on other sites

5 minutes ago, samme said:

Use 


game.physics.arcade.collide(sprites)

With collide(sprites, sprites), there seems to be 2 separations per collide call, which also seems wrong.

I didn't know collide function can work with only one object. Now everything is fine even if i scale sprites. Thank you.

Link to comment
Share on other sites

12 minutes ago, samme said:

Use 


game.physics.arcade.collide(sprites)

With collide(sprites, sprites), there seems to be 2 separations per collide call, which also seems wrong.

How can i define collideCallback function without object2? Doesn't work this way: collide(sprites, null, callback)

Link to comment
Share on other sites

  • 2 weeks later...
 Share

  • Recently Browsing   0 members

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