0 Favourites

How do I know which tile is being hit on collision

  • Hello everyone,

    I am curious, is there a good way to pick a tile in a tilemap from an on collision event?

    If you have a bullet and it hits a tile in the tile map, how do you calculate that? On collision event just returns the tilemap but not the x,y coordinate of the tile. As it turns out, usually an object colliding with a tile is not in the same tile space as the tile it is hitting (ie floor(sprite.x/tileSize != colliding tile.x)

    Does this make any sense?

    This is part of a discussion here:

    The question is very easy, but the answer is hard.

  • 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 don't know if this has been proposed yet, but this is what I would at least start with

    bullet collision at offset (2,0) /** collision with a tilemap 2 pixels to the right side **/

    -> set bulletcheck (boolean) to on

    if bulletcheck is on & tilemap, compare tile at bullet.x+2 (I know this isn't necesarily how overlaps work, but hopefully you get my point), bullet.y = whatever tile you're wanting to find

    -> tilemap settile (or) erasetile

    of course, you would have to have checks for every offset of your bullet... I'm assuming this is like an 8 direction bullet, but I'm sure you could do something with the calculated angle of x and y and make it a bit more elegant using functions.

    does that make sense though?

  • You would probably have to round the coordinates too, ie. (round(self.X/8)*8) if you have a 8 pixel based tile

  • as I'm thinking about this more, you'd need some way of just calculating where on the tilemap your bullet actually hit based on the tilemap index, but I don't think that would be too hard...

    sorry for the triple post...

  • mudmask I was hoping for a more elegant solution. I had originally tried to project the bullet along its current path and see what tile it was overlapping ((b.x + xprojection) / tilesize)... but this doesn't work at shallow angles.

    Your proposed method is almost like classic nes collision case scenarios. You look at the tile you are in and then look at the tiles surrounding the bullet. then bases on movement and penetration you figure out what the bullet has hit.

    This can be done, but it will basically involve rolling your own collision code. Worse yet, this doesn't account for non-square tiles. If you wanted slopes that would further involve more cases to test and resolve. If I had to do that, I might as well not be using construct... lol (if the goal is to save time, right)

  • I don't think there's going to be an easy solution. I suspect it will need to be hacked together, possibly using invisible sprites of various sizes and shapes containered with or pinned to the tilemap.

  • Here's a way to find all the tiles a box object is currently overlapping.

    If you want to check against a different collision shape and/or with a rotated shape then try the second one.

    It's also possible to use tiles with non-box collision polys but you just need to set the tile sprite accordingly.

  • Hi guys, thank you for your discussion, but it is just beyond my comprehension . I could only grab the whole section of the tilemap since my boxes are 50 X 50. So, I can not know where is my box is placed on the tilemap, since my box is the whole tilemap now.

    I attached here capx with 2 sprites and 2 tilemaps. 2 sprites demonstrates what I want to do. A -shoots and make the grey box the yellow solid one, D shoots and the yellow box becomes the grey unsolid one.

    I am wondering maybe I would stick to sprites as my boxes? I want to use boxes only ( no slopes or other shapes). If I use say 100 of them per one layout, will my game be lagging? Thanks.

  • heliogame - they do say optimizing too early is the worst sin you can commit in programing. I didn't think this would be that hard on first thought. R0J0hound provided good examples and can be learned from. It might not be a bad idea to just make the game with sprites and see how it works. C2 can handle thousands of sprites, but if you are making for mobile then that might not work.

  • heliogame - it looks like you got it working in the example, which is cool. Now make a level at the size you want and see if it works. You'll probably wan't to line up the boxes using snap to grid though.

  • ruskul, so, no chances to make the same thing with tiles? I am afraid my game will be too heavy... or limited. I want to spread those boxes everywhere.

  • Hey... pretty much when R0J0hound shows up, the magic happens.

    I'm stuck in an 8 bit world though, with some of the projects I'm working on.

  • R0J0hound, I replaced the sprite 2, which overlaps a tile, with a bullet. While bullet is colliding with a tile, the effect of overlapping does not appear. What was wrong?

    Also, I am thinking about an interaction of 2 tilemaps on one layout. One tilemap is solid, the other one is unsolid. When I shoot at the tile of solid tilemap, this tile would be replaced with a tile of unsolid tilemap.

    When I shoot at the tile of unsolid tilemap, this tile would be replaced with a tile of solid tilemap.

    Is this interaction of 2 tilemaps possible in C2?

    Thanks

  • heliogame the use of 2 tilemaps is a good idea and is common practice. Really, anytime you have tilemaps and need some to be solid and others not, this is fine.

  • R0J0hound, I replaced the sprite 2, which overlaps a tile, with a bullet. While bullet is colliding with a tile, the effect of overlapping does not appear. What was wrong?

    It looks fine here. Slow the bullet if you want to see it longer.

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