0 Favourites

How do I get the Tile ID of a Collision

  • 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:

    TilemapObstacles.TileToPositionX(TilemapObstacles.PositionToTileX(Player.X))[/code:3e6dfuw1]
    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....
  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • 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:

    Tilemap.PositionToTileX(Player.X + Player.Width/2)[/code:3nwja19w]
    
    And this will give you the Y position of the [i]bottom edge[/i] of your sprite:
    
    [code:3nwja19w]Tilemap.PositionToTileY(Player.Y + Player.Height/2)[/code:3nwja19w]
    
    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:3nwja19w]Tilemap.PositionToTileX(Player.ImagePointX("BottomRight"))
    Tilemap.PositionToTileY(Player.ImagePointY("BottomRight"))[/code:3nwja19w]
    
    Not exactly perfect solutions but better than nothing; if I come across something bulletproof I'll post it. Good luck!
  • Thanks for replying.

    I'll have a play and see what I can come up with.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)