1 decade ago
by Aitor
Hello everyone.
I know I can know if there has been a collision in the x axis with res.collision.x, but is there a way to know if this collision has been with a tile on the left or on the right of the entity?
Thank you in advance
If the entity's vel.x > 0, it's colliding on the right; if vel.x < 0, it's collding from the left.
1 decade ago
by Aitor
Thank you! I was thinking on that, but I was afraid of situations were that wouldn't work, but after thinking for a while I haven't found any, so I'll use that.
Collision tiles should never move. If they do, it's better to use an Entity.
1 decade ago
by Aitor
No, they don't move. Another question, how can I know wich is the pos of the tile the entity is collding with? I've tried using this function to get the tile of the top-left corner of an entity:
getTileOfEntity:function(entity)
{
var x=entity.pos.x+entity.size.x/2;
var y=entity.pos.y+entity.size.y/2;
//this.tilesize is a variable holding the size of a collision tile
var xTile=Math.floor(x/this.tileSize);
var yTile=Math.floor(y/this.tileSize);
return {x:xTile, y:yTile};
}
and adding one to tile.x it if the collision is to the right and substracting one to it if the collision is to the left. But that doesn't work correctly if the entity is bigger on the x axis than the tile, because for example if the tilesize was of 5x5 and i had a entity of 12x5 colliding wiht a tile on its right, I would need to do tile.x+=2, and not tile.x++. So, is there a way to effectively know which tile is the entity colliding with in the x or y axis?
handleMovementTrace: function( res ){
if( res.collision.x ){
var tile = {
x: ig.game.collisionMap.tilesize * res.tile.x,
y: ig.game.collisionMap.tileSize * res.tile.y
};
//do something
}
}
1 decade ago
by Aitor
thank you! but res.tile.x and res.tile.y always hold 1 or 0 for me. I thought this was the normal behaviour (I asked it in other thread). If it isn't, any idea why this happens?
Oops.
res.pos.x
and
res.pos.y
.
res.tile
holds the collision tile type. See the
documentation.
1 decade ago
by Aitor
But i think the problem remains. res.pos.x and res.pos.y seem to point to the entity's top-left corner position, so I have the same problem I described to get which tile the object is colliding with. For example If i have a entity which has it's bottom part colliding with a tile and it's top part in the air, res.pos.y would still return the top parts pos, but because I need to know the colliding tile's pos, and not the entity's pos, that wouldn't work. I guess I'll have to solve that myself.
Try this:
handleMovementTrace: function( res ){
if( res.collision.x ){
var tilesize = ig.game.collisionMap.tilesize, tile = {
x: tilesize * res.pos.x + this.vel.x > 0 ? 0 : tilesize,
y: tileSize * res.pos.y + this.vel.y > 0 ? 0 : tilesize
};
//do something
}
}
Page 1 of 1
« first
« previous
next ›
last »