hexus / phaser-arcade-slopes

:triangular_ruler: A Phaser CE plugin that brings sloped tile collision handling to the Arcade Physics engine
MIT License
126 stars 16 forks source link

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

Closed iCodeForBananas closed 8 years ago

iCodeForBananas commented 8 years ago

UPDATE

See down the conversation, the collide behavior looks like it's correct but you can't use overlap yet. https://github.com/hexus/phaser-arcade-slopes/issues/7#issuecomment-229096109

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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

hexus commented 8 years ago

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 commented 8 years ago

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: https://github.com/hexus/phaser-arcade-slopes/blob/86c68867ca6ffce1c3a2cbe638571b199cc7c977/src/ArcadeSlopes/Overrides.js#L48

Replace the if statement with the following:

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

overlap test

hexus commented 8 years ago

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 commented 8 years ago

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

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

hexus commented 8 years ago

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 commented 8 years ago

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. :smiley:

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 commented 8 years ago

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

iCodeForBananas commented 8 years ago

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

iCodeForBananas commented 8 years ago

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 commented 8 years ago

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.