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 Bradley

I'm trying to detect if my player entity is colliding near the top of a tile. I don't see a way to know what part of a tile an entity is colliding with. Also don't see a way to know anything about a tile except what type it is. Are there tile properties available so I can compare it's position with the position of my player?

I've created an entity who can climb and/or slide down walls. Works great, but when he's high enough on a wall sometimes appears to be clung slightly above it. I just need to know how to make him cling to a wall only when my entity is below the top edge.

I appreciate your responses.

1 decade ago by Seeders

check the y coordinate of the tile which should be the top left corner. Then compare that to the player's y coordinate and check if its within the range you want. (50% tile height?)

1 decade ago by Bradley

How do I get a reference to the tile I'm colliding with?

1 decade ago by Arantor

If you know the pos.x and pos.y of the entity, you can divide that by the tilesize to convert it to tile positions, then go through the relevant map to pull out what the tile is.

Or if the ceiling is the same position the whole time (relative to the level), you could just use the entity's pos.y as-is to compare it against.

1 decade ago by Bradley

I think I would also need to check to make sure there wasn't a another tile immediately above the one I'm checking against, because I need to know if I'm at the top of a wall... possible, sure. So I would have to do two checks, per collision.

So, sounds like there's no way to get reference to the tile itself, to read properties from it?

1 decade ago by paulh

Not sure if this still works:

handleMovementTrace: function( res ) {
    if( res.tile.x == 12 ) {
        // Always ignore collisions with the PLATFORM_TILE
        // on the x axis
        res.collision.x = false;
        res.pos.x = this.pos.x + this.vel.x * ig.system.tick;
    }
        
    if( res.tile.y == 12 && this.vel.y < 0 ) {
        // Ignore collisions with the PLATFORM_TILE
        // on the y axis, if we're moving upwards
        res.collision.y = false;
        res.pos.y = this.pos.y + this.vel.y * ig.system.tick;
    }

1 decade ago by Bradley

@Arantor, "go through the relevant map to pull out what the tile is." How should I access a specific map, and find out what tile exists at a particular coordinate?

1 decade ago by Arantor

Get the entity's position, divide by the tile size, round it down then access the array. Any of the maps are simply arrays of arrays - once you have the x and y, you can just reference the map by looking into ig.game.backgroundMaps[n].tiles[y][x] where n is the number of the layer you're interested in (they're loaded sequentially into the ig.game.backgroundMaps array when you call loadLevel) and y and x are the y and x positions, in tiles, that you want to access.

1 decade ago by Bradley

ig.game.backgroundMaps... yes, that's what I was looking for. Thx.

1 decade ago by Bradley

Hmmm. ig.game.backgroundMaps[0].tiles[0] is returning undefined. ig.game.backgroundMaps[0].tiles[0][0] throws an error.

1 decade ago by Bradley

Oic. I think you meant ig.game.backgroundMaps[0].data.

1 decade ago by Arantor

Yes, it seems that I did, I was pulling it only very quickly from the source.

1 decade ago by Bradley

It works. I'm not doing two checks, just one, per collision.

handleMovementTrace: function( res ) {

if (res.collision.x) {

if (this.vel.y > 10)
{

var offsetY = -15;
var offsetTileX = (this.flip) ? -1 : 1;

var yTilePos = Math.round((this.pos.y + offsetY) / this.TILE_SIZE);
var xTilePos = Math.round(this.pos.x / this.TILE_SIZE) + offsetTileX;

tileAt = (yTilePos >= 0) ? ig.game.collisionMap.data[yTilePos][xTilePos] : null;

if (tileAt == 1)
{
if (!this.isClung)
{
this.cling();
}

}

}

}
else
{
if (this.isClung)
{
this.uncling();
}

}

this.parent(res);

},

cling: function ()
{
this.currentAnim = this.anims.cling;
this.currentAnim.flip.x = this.flip;
this.vel.y = 10;
this.gravityFactor = .08;
this.isClung = true;
},

uncling: function()
{
this.isClung = false;
this.gravityFactor = 1;
}

1 decade ago by Arantor

(You might want to edit your post to put ## on the lines before and after your code to format it, makes it a bit easier to read...)

Looks good :)

1 decade ago by Bradley

Ah ha! I was wondering about that. Thx.

1 decade ago by Arantor

All the formatting options are available from the "Show Formatting Help" just above the textbox ;)

Also note that you can also edit your own posts by hovering by your name above the post.
Page 1 of 1
« first « previous next › last »