Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
kenray opened this issue Mar 24, 2014 · 4 comments

Comments

@kenray
Copy link

commented Mar 24, 2014

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.

@photonstorm

This comment has been minimized.

Copy link
Owner

commented Mar 24, 2014

If you set a customSeparateX/Y on the body then it will go into the separationX/Y methods, but not actually separate them, instead the overlapx/y values are populated and it's entirely up to your code to sort out how to move them apart again.

This will only work if you collide the objects, not overlap them.

separateX/Y are protected internal methods used by collide, and were never meant to be called directly. None of your process or collision callbacks can be fired if you hit them directly.

@kenray

This comment has been minimized.

Copy link
Author

commented Mar 25, 2014

I see... unfortunately that won't help me as I can't use collision tests while dragging a sprite (or so it appears).

I ended up 'rolling my own' overlapX/Y tests in update() so I could use it both with velocity and with dragging, but it would really be great to have an optional param to the Arcade.overlap() method that would allow for retrieving these values. I understand Arcade.overlap is suppose to be a fast method of just determining _if_ two sprites overlap, so that's why I'm suggesting the optional param so it can remain fast for those who don't need the overlapX/Y values, but still allow it for people who do.

Oh, also btw: separateX/Y are not marked as in the docs, which sounds like they should be.

Since this is an enhancement request now, you can close the report (unless it's OK for me to close it).

@photonstorm

This comment has been minimized.

Copy link
Owner

commented Mar 25, 2014

I've tagged this as enhancement, please leave it open for now. I'll close it once I've added it into dev.

photonstorm added a commit that referenced this issue Sep 1, 2014
ArcadePhysics.overlap has been updated so that the Body.overlapX/Y pr…
…operties are set to the amount the two bodies overlapped by. Previously they were zero and only populated during the separation phase, but now the data is available for just overlap checks as well. You can then use these values in your ovrelap callback as required - note that they are changed for every check, so a Sprite overlap tested against 10 other sprites will have the overlapX/Y values updated 10 times in a single collision pass, so you can only safely use the values in the callback (#641)
@photonstorm

This comment has been minimized.

Copy link
Owner

commented Sep 1, 2014

I know it's quite old, but I've just added support for this :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.