How do I know which tile is being hit on collision

Get help using Construct 2

Post » Sat Jan 24, 2015 12:05 am

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:

viewtopic.php?t=123208&p=877040#p877040

The question is very easy, but the answer is hard.
Image
B
33
S
11
G
2
Posts: 564
Reputation: 5,153

Post » Sat Jan 24, 2015 3:36 am

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?
B
72
S
18
G
9
Posts: 376
Reputation: 9,106

Post » Sat Jan 24, 2015 3:37 am

You would probably have to round the coordinates too, ie. (round(self.X/8)*8) if you have a 8 pixel based tile
B
72
S
18
G
9
Posts: 376
Reputation: 9,106

Post » Sat Jan 24, 2015 3:43 am

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...
B
72
S
18
G
9
Posts: 376
Reputation: 9,106

Post » Sat Jan 24, 2015 7:00 pm

@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)
Image
B
33
S
11
G
2
Posts: 564
Reputation: 5,153

Post » Sat Jan 24, 2015 7:52 pm

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.
B
11
S
4
Posts: 152
Reputation: 1,236

Post » Sat Jan 24, 2015 8:16 pm

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.
You do not have the required permissions to view the files attached to this post.
B
92
S
32
G
107
Posts: 5,273
Reputation: 69,957

Post » Sat Jan 24, 2015 11:05 pm

Hi guys, thank you for your discussion, but it is just beyond my comprehension :o . 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.
You do not have the required permissions to view the files attached to this post.
B
8
S
2
Posts: 48
Reputation: 646

Post » Sun Jan 25, 2015 12:50 am

@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.
Image
B
33
S
11
G
2
Posts: 564
Reputation: 5,153

Post » Sun Jan 25, 2015 12:53 am

@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.
Image
B
33
S
11
G
2
Posts: 564
Reputation: 5,153

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 15 guests