Collision test "pushes" object - is this a bug or what?

Hi!

I have been around for a little while and I think I never mentioned that I am a game coder coming from ActionScript and Vanilla. After a long time rusting the joints I am back to the game (no pun intended). I am starting with Phaser 3 and am doing some tests myself of how to do basic things before to jump over a project that an old client ordered to me.

Anyway, this morning I was playing with collision tests when suddenly I bumped (no pun intended again!) to this funny behavior. I really would like to know if this a bug or purposeful. I realized that in a particular situation if you try to detect the collision between an object moving towards a stopped one, when they collide the moving object will push the stopped object. The funniest part is that it only will happen if both objects has their hit areas set to circle. Better explaining, if you leave it as default (square) it won’t happen. In fact if at least one of the two objects has its area as square it won’t happen. That’s why it has the appearance of a bug to me.

Also being experienced with several other languages it really sounds odd to me that an event function that supposedly should only make a test causes a behavior change. From my perspective a test should produce only a true/false result but in any circumstance change things (it should be left to the coder to decide what to do depending on the result).

Here it goes the code to reproduce the behavior:

var Moving;
var Stopped;

class Foo extends Phaser.Scene
{
    constructor ()
    {
        super();
    }

    create ()
    {
        Stopped = this.physics.add.sprite(800, 300,"");
        Stopped.setCircle(18);
        
        Moving = this.physics.add.sprite(500, 300, "");
        Moving.setCircle(18)
     
        this.physics.add.collider(Moving, Stopped, function (Moving, Stopped) {
        });
    }
    
    update () {
        Moving.x += 5;
    }
    
}

const config = {
    type: Phaser.AUTO,
    parent: "Foo",
    width: 1024,
    height: 768,
    pixelArt: true,
    backgroundColor: 0xffffff,
    physics: {
        default: "arcade",
        arcade: {
            debug: true
        }        
    },
    scene: [ Foo ]
};

const game = new Phaser.Game(config);

:laughing:

collide() etc. are not test functions. They resolve collisions by separating bodies, possibly accelerating them, and set collision flags on the bodies.

You should be setting velocity instead:

Moving = this.physics.add.sprite(500, 300, "");
Moving.setVelocityX(300);

Hey Samme this is a very valuable information! Thanks!

I have found this tutorial teaching how to detect collision that put it this way, but as per what you said this is NOT the correct way of doing it. Ugh!

Could you point me some example of code where I can learn how to check simple collision detection between two objects? I am so sorry for keep asking things that seems to be obvious, but I have experienced a lot of difficulty of learning directly from Phaser documentation because the lack of code sample…

Thanks!

PS: I mean, I checked this your suggested change and it worked PERFECTLY for the purposes, but I am puzzled that I had the impression that maybe you mentioned that this is not the right way?

overlap() is really intended for that case.

The first game tutorial (Part 6, Part 8) covers some of this.

1 Like

Thanks, Samme, overlap did the trick to me!