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

Overlap doesn't appear to account for empty space and overlaps them with ground #7

Closed
iCodeForBananas opened this issue Jun 27, 2016 · 13 comments
Assignees

Comments

@iCodeForBananas
Copy link

iCodeForBananas commented Jun 27, 2016

UPDATE

See down the conversation, the collide behavior looks like it's correct but you can't use overlap yet.
#7 (comment)

OLD
The bullets hit the 50% slope and slide up.
bullets5

The bullets go straight through and if you're shooting through a 10x10 the bullet will enter one side and collide with the opposite side.
bullets5

This is how I create 50 reusable bullets.

    this.bullets = this.game.add.group()
    this.bullets.createMultiple(50, 'bullet')
    this.bullets.setAll('checkWorldBounds', true)
    this.bullets.setAll('outOfBoundsKill', true)
    this.physics.arcade.enable(this.bullets)
    this.game.slopes.enable(this.bullets)

This is how I fire a bullet.

    let bullet = this.rootScope.bullets.getFirstDead()
    bullet.bulletId = Guid()
    bullet.damage = this.damage
    bullet.weaponId = this.meta.id
    bullet.height = this.bulletHeight
    bullet.width = this.bulletWidth
    bullet.alpha = 0
    bullet.body.gravity.y = -1800

    // Add a touch of tile padding for the collision detection
    bullet.body.tilePadding.x = 50
    bullet.body.tilePadding.y = 1

    bullet.reset(x, y)
    let pointerAngle = this.rootScope.game.physics.arcade.moveToPointer(bullet, this.bulletSpeed)
    bullet.rotation = pointerAngle

    setTimeout(() => {
        bullet.alpha = this.bulletAlpha !== undefined ? this.bulletAlpha : 1
    }, 40)

This is how I collide bullets with the slope ground.

    this.physics.arcade.collide(this.bullets, this.ground,  function(bullet) {
        bullet.kill()
    }, null, this)

Normally I use overlap though.

    this.physics.arcade.overlap(this.bullets, this.ground,  function(bullet) {
        bullet.kill()
    }, null, this)

but this has been happening.

bullets5

@hexus
Copy link
Owner

hexus commented Jun 27, 2016

Interesting! Thanks for such a clear explanation.

If you have a way of setting up something like a codepen that I could use to diagnose this I'd be happy to look into it.

Something that jumps out at me immediately:

bullet.height = this.bulletHeight
bullet.width = this.bulletWidth

This is being set after enabling physics bodies for the particles of the emitter. Could it be that the body of each particle is larger than you expect? Perhaps you could find away to draw debug output for the bodies of currently alive particles, just to get a better visual of what's going on. Might be helpful to do the same for the tilemap at the same time.

Unfortunately the plugin itself doesn't yet have its own debug output for all the polygons and vectors involved in the collision handling, but that is on the roadmap for 0.2.0 (#4).

@iCodeForBananas
Copy link
Author

bullets6

I was messing with the body width and height instead like you said. I think the main issue is that I'm unable to use overlap instead of collide with the bullets. I'll see if I can get a codepen together.

@iCodeForBananas
Copy link
Author

iCodeForBananas commented Jun 28, 2016

Some of the particles in the example have a similar issue.
Change the collision on https://github.com/hexus/phaser-arcade-slopes/blob/gh-pages/src/ArcadeSlopesDemo.js#L300 to the below:

            // Collide the particles against the collision layer
            this.physics.arcade.overlap(this.emitter, this.ground, function(particle, ground) {
                particle.kill()
            }, null, this);

This should allow for the particles to come out of the player and when they overlap the ground be killed.
Instead they get killed instantly inside of the player object.

bullets7

@iCodeForBananas iCodeForBananas changed the title Bullets don't always collide correctly with slopes or they slide up the slope Overlap doesn't appear to account for empty space and overlaps them with ground Jun 28, 2016
@hexus
Copy link
Owner

hexus commented Jun 29, 2016

Very interesting! I will absolutely investigate this at some point, thank you for highlighting it so well.

The plugin is probably not handling the overlapOnly flag correctly. :)

@iCodeForBananas
Copy link
Author

iCodeForBananas commented Jun 29, 2016

Ok so I don't really understand the consequences of my actions but I got it to kind of work now.

Go to this line:

Replace the if statement with the following:

        if (! overlapOnly && collideCallback) {
            collideCallback.call(callbackContext, sprite, tile);
        }

overlap test

@hexus
Copy link
Owner

hexus commented Jun 29, 2016

Hmm, okay. That's interesting, because that branch passes handling to Arcade Physics to handle normally, if the tile doesn't have a tile.slope property (i.e. it hasn't been converted by the plugin).

Except the callback should still be called in the case of an overlap. The code you've linked to is meant to mirror this, to an extent:

https://github.com/photonstorm/phaser/blob/master/src/physics/arcade/TilemapCollision.js#L63

Except I decided to break it down a little more to avoid code repetition. But perhaps the return statements I've used are causing issues down the line, or separateTile() is doing something that I'm not catering for properly.

Also, love that the Lenny face is in the example GIF, haha. What do you use for screen capture?

@iCodeForBananas
Copy link
Author

LICEcap
http://www.cockos.com/licecap/

One of the most useful tools ever for showing people stuff!

@hexus hexus self-assigned this Jun 30, 2016
@hexus hexus added the bug label Jun 30, 2016
@hexus
Copy link
Owner

hexus commented Jun 30, 2016

I totally managed to reproduce the issue above, but even after completely disabling the plugin. This might be an issue with Phaser itself, and not the plugin.

@hexus
Copy link
Owner

hexus commented Jun 30, 2016

Okay, so this technically isn't a bug (but I think it is).

The callback is working as expected because the sprites are indeed overlapping a tile.

phaser-overlap-bug-1

The problem, as highlighted in the GIF, is that these tiles shouldn't collide and have an index of -1, but Phaser doesn't say anything about overlap() only checking tiles that collide. Whether this is correct is up for debate, but I feel like it isn't.

To work around this, you can add a process callback.

this.physics.arcade.overlap(this.emitter, this.ground, function (particle, tile) {
    particle.kill();
}, function (particle, tile) {
    return tile.collides;
}, this);

This will still check the overlap, but only for tiles that can collide. It should give the result you're after.

phaser-overlap-bug-2-2

Collision process callbacks in Phaser are a check that let you decide whether to actually continue with the overlap/collision based on the result of your own callback function. In this case, we simply check whether the current tile is meant to collide with anything.

Feel free to let me know if this resolves all of your issues. 😃

Also, technically, this means my plugin is incorrect, because it will bail out from overlapOnly collision checks if the tile doesn't collide, which is adverse to Phaser's above mentioned behaviour.

@hexus
Copy link
Owner

hexus commented Jun 30, 2016

Oh, and thanks for showing me LICEcap, it's fabulous! ✨

@iCodeForBananas
Copy link
Author

It totally worked! I didn't get a chance to really test it a lot but immediately I was able to shoot again! :)

@hexus hexus closed this as completed Jul 1, 2016
@iCodeForBananas
Copy link
Author

Just a heads up for closure on this, it's works awesome!
I updated the sprite bodies and added the processCallback to other things needing overlap and it all works! :)

@hexus
Copy link
Owner

hexus commented Jul 1, 2016

Awesome, thanks for letting me know.

I did close this pre-emptively but I had a gut feeling that this was the cause of all your issues. I thought maybe something else would be causing the sliding/missing tiles, but assumed these were different attempts to get it working and ultimately you just wanted the correct overlap responses. :)

Glad it works so well! I look forward to seeing your project progress.

@hexus hexus removed the bug label Jul 8, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants