Skip to content

Arcade.body.overlapX/overlapY should be retrievable from Arcade.overlap() #641

@kenray

Description

@kenray

My scenario is that I have two sprites that should pass over each other (not collide), and those sprites can be either both me moved with velocity or one of them can be dragged manually by the user onto the other sprite. In both cases, I need to be able to determine the amount of overlap in both directions.

I am able to get the kind of overlapping behavior by using the Arcade.body.overlap() function, but you can't test for the overlapX/overlapY values when this method is triggered.

Here's an example of the code I'm currently using testing for dragging a sprite ("brick") over another sprite ("bags"):

create: function() {
    bags = game.add.sprite(300,300,'bags');
    game.physics.enable(bags, Phaser.Physics.ARCADE);

    brick = game.add.sprite(200,300,'brick');
    brick.inputEnabled = true;
    brick.input.enableDrag(false,true);
    game.physics.enable(brick, Phaser.Physics.ARCADE);
    brick.body.moves = false;
},
update: function() {
    if (game.physics.arcade.overlap(bags,brick)) {
        console.log('overlapping: ' + brick.body.overlapX + ',' + brick.body.overlapY);
    }
    if (game.physics.arcade.separateX(bags.body,brick.body,true)) {
        console.log('separateX: ' + brick.body.overlapX + ',' + brick.body.overlapY);
    }
    if (game.physics.arcade.separateY(bags.body,brick.body,true)) {
        console.log('separateY: ' + brick.body.overlapX + ',' + brick.body.overlapY);
    }

Using the code above, it triggers the overlap method, but not the separateX/separateY methods.

I looked up Arcade.body.overlapX/overlapY in the source and discovered that the only time these values are filled with something other than 0 is when you use the Arcade.separate(), Arcade.separateX, or Arcade.separateY() methods. Unfortunately, this also turns on collision tracking, which also prevents triggering these methods if you are dragging a sprite (btw, this is also true even if you pass true for the 'overlapOnly' parameter to Arcade.separateX/separateY).

So unless there's a way to drag and still get overlapX/overlapY to work, I would suggest adding this to the Arcade.overlap() method.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions