How do I get the Tile ID of a Collision

Get help using Construct 2

Post » Sun Jan 18, 2015 10:21 am

This seems like such a simple thing to want, but for the life of me I can't figure it out.

When my character's sprite collides with a tilemap tile, how do I get the Tile ID of the tile in the collision?
If I do the whole:
Code: Select all
TilemapObstacles.TileToPositionX(TilemapObstacles.PositionToTileX(Player.X))

I get the tile position of the Player, not the tile involved in the collision. Ie the tile returned is one position above, below or to the left or right of the tile I'm interested in.

Any help gratefully received....
B
6
G
1
Posts: 3
Reputation: 657

Post » Mon Jan 19, 2015 12:44 am

I'll be needing to do exactly the same thing - get all the tiles that are overlapping a sprite. There doesn't appear to be a built-in function, and I agree this seems like something that would be very useful for many, many people. I'd also like to be able to query tile size for different tilemaps at runtime, but that's another thread. ;)

Anyway, since we don't seem to have what you're looking for I have some suggestions you can try, with the caveat that I haven't tried them myself yet. ;)

You can test points along the edges of your character sprite (not just the default image point) using Tilemap.PositionToTileX() and Tilemap.PositionToTileY(). Assuming your default image point is in the center of your character sprite, this will give you the X position of the right edge of the sprite:

Code: Select all
Tilemap.PositionToTileX(Player.X + Player.Width/2)


And this will give you the Y position of the bottom edge of your sprite:

Code: Select all
Tilemap.PositionToTileY(Player.Y + Player.Height/2)


Together that gets you the bottom-right corner of the sprite. Do that for all the corners and perhaps for the midpoints of all the sides and you have a pretty good idea whether your character is overlapping specific tiles.

You could also manually define some image points around the edges of your sprite. I.e., add an image point the the bottom-right corner of your sprite and name it "BottomRight", then query it like this:

Code: Select all
Tilemap.PositionToTileX(Player.ImagePointX("BottomRight"))
Tilemap.PositionToTileY(Player.ImagePointY("BottomRight"))


Not exactly perfect solutions but better than nothing; if I come across something bulletproof I'll post it. Good luck!
B
41
S
13
G
2
Posts: 49
Reputation: 4,215

Post » Tue Jan 20, 2015 12:04 am

Thanks for replying.
I'll have a play and see what I can come up with.
B
6
G
1
Posts: 3
Reputation: 657


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 8 guests