phaserjs / phaser

Phaser is a fun, free and fast 2D game framework for making HTML5 games for desktop and mobile web browsers, supporting Canvas and WebGL rendering.
https://phaser.io
MIT License
37.12k stars 7.1k forks source link

Scaled tilemap layer & scaled sprite will not collide properly #2305

Closed Cross22 closed 8 years ago

Cross22 commented 8 years ago

I am trying to recreate an old DOS game and doing scaling on tilemaps and sprites to create appropriate low-res visuals. Both the map layer and the sprite have their scale set to (4,2), and this causes the physics.arcade.collide(player,mapLayer) check to fail- collisions are not found. It boils down to an intersection test in Phaser.Tile where the unscaled tile rect is compared with scaled body rect (relevant section below). I have also tried just setting world scale and not the individual layer/sprite scales. This also fails, but in a different location.

Phaser.Tile:

 intersects: function (x, y, right, bottom) {

        if (right <= this.worldX)     // right is SCALED body position, worldX is UNSCALED tile position
        {
            return false;
        }

        if (bottom <= this.worldY)
        {
            return false;
        }

        if (x >= this.worldX + this.width)
        {
            return false;
        }

        if (y >= this.worldY + this.height)
        {
            return false;
        }

        return true;

    },
Cross22 commented 8 years ago

Found the culprit:

layer.setScale(2,2); works! layer.scale.set(2,2); should not be used.

photonstorm commented 8 years ago

Yes, I wish there was an easy way to make this more obvious - but at least you figured it out!