Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by StuartTresadern

I have been playing around a little with handleMovementTrace to resolve the tile position (not the tile value). If I detect a collision on the y axis it’s easy enough to work out the y tile position but how do I calculate the correct x position?.

For example if I have a ball entity hitting some collision tiles (breakout style) and the collision.y is true I can subtract / add based on velocity to calculate the offset and resolve the tile y position that has been hit. Maybe I am just having a bad day but I cannot seem to work out how to calculate the x position.

1 decade ago by jswart

handleMovementTrace: function( res ) {
    if( res.collision.x ) {
        // do something
    }

    // Continue resolving the collision as normal
    this.parent(res); 
},

1 decade ago by StuartTresadern

I understand how to detect the collision. I needed to work out the point of collision position for the x axis when the collision was detected on the y axis and vice versa.

So far I have come up with the following. As my entity is a little smaller that the tile size it makes it a little easier. Currently I do three checks (I only want one tile)

1) check if res.pos.x + (entity.x / 2) has a tile.
2) check if res.pos.x has tile.
3) check if res.pos.x + (tilesize) has a tile.

Code is below but it seems a bit of a kludge ! I was looking for a better solution.

            handleMovementTrace:function (res) {

                if (res.collision.y) {

                    var offsety = this.vel.y < 0 ? -1 : ig.game.collisionMap.y +1;
                    var offsetx = ig.game.collisionMap.getTile(res.pos.x + (this.size.x / 2), res.pos.y + offsety) ? (this.size.x / 2) : offsetx = ig.game.collisionMap.getTile(res.pos.x, res.pos.y + offsety) ? 0 : 32;

                    var health = ig.game.hMap.decreaseHealth((res.pos.x + offsetx), res.pos.y + offsety, 1);
                    if (health > 0 )  ig.game.sfxBrickHit.play();
                    if (health == 1) {
                        ig.game.blocksMap.setTile((res.pos.x + offsetx), res.pos.y + offsety, 0);
                        ig.game.sfxBrickKill.play();
                        ig.game.playerScore+=1;
                    }
                }

               if (res.collision.x)
                {
                    var offsetx = this.vel.x < 0 ? -1 : ig.game.collisionMap.tilesize.x +1;
                    var offsety = ig.game.collisionMap.getTile(res.pos.y + (this.size.y / 2), res.pos.x + offsetx) ? (this.size.y / 2) : offsety = ig.game.collisionMap.getTile(res.pos.x +offsetx, res.pos.y) ? 0 : 32;

                    var health = ig.game.hMap.decreaseHealth((res.pos.x + offsetx), res.pos.y + offsety, 1);
                    if (health > 0 )  ig.game.sfxBrickHit.play();

                    if (health == 1) {
                        ig.game.blocksMap.setTile((res.pos.x + offsetx), res.pos.y + offsety, 0);
                        ig.game.sfxBrickKill.play();
                        ig.game.playerScore+=1;
                    }
                }

                this.parent(res);
Page 1 of 1
« first « previous next › last »