How do I know which tile is being hit on collision

Get help using Construct 2

Post » Thu Feb 26, 2015 11:06 pm

I did some tests and it appears that with the physics behavior, a projectile will bounce without overlapping the tile so in this case your solution won't work.

While using the physics behavior, I managed to target the closest tile on my project -with the surrounding tiles thing- but I increased the projectile speed and now it's less accurate.
Most of the time it works :
hitTileRight.png

Some of the surrounding tiles (under the blue overlay square) aren't empty so I can target a closest non empty tile when the projectile collides with the wall. The red square is the returned tile from the projectile position with positionToTile.

But sometimes, quite often, it won't work with my fast projectile :
hitTile.png

The tile returned from the projectile coordinates is one tile farthest from the green wall than it should be.
No surrounding tiles are overlapping the green wall so my events won't work because it needs to target a close non empty tile.

I avoided this new issue by increasing the radius of the tested surrounding tiles from 1 square :
hitTile2.png

Now I can find the closest tile that is not empty.

That's an ugly solution thought :(
You do not have the required permissions to view the files attached to this post.
B
12
S
7
G
7
Posts: 450
Reputation: 4,276

Post » Fri Feb 27, 2015 12:53 am

I don't know the Physics behaviour that well, so if there is another way to do it, I really don't know. But if your works I guess its fine. But what if two tiles are equal range, won't it bug? for instant if you shoot in the corner below where your shot is on the pictures, then the bottom tile and the right tile will be of equal range, or don't you calculate it that precise, so its not likely to happen?
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,838

Post » Fri Feb 27, 2015 3:09 am

Seems like we ought to be able to get collision points using a trigger condition, instead of crazy workarounds. It also seems like we should have some kind of raycasting collision testing. It can't be that much harder to test a ray against a polygon than it is to test two polygons.

Maybe it's time for someone to read the source and add a line to the physics behavior so that it triggers a condition when it computes a collision, then stores coordinates of the collision.
B
14
S
5
G
1
Posts: 189
Reputation: 1,536

Post » Fri Feb 27, 2015 4:11 pm

oops, same post.
Last edited by Coin-coin le Canapin on Fri Feb 27, 2015 4:37 pm, edited 1 time in total.
B
12
S
7
G
7
Posts: 450
Reputation: 4,276

Post » Fri Feb 27, 2015 4:35 pm

nimos100 wrote:I don't know the Physics behaviour that well, so if there is another way to do it, I really don't know. But if your works I guess its fine. But what if two tiles are equal range, won't it bug? for instant if you shoot in the corner below where your shot is on the pictures, then the bottom tile and the right tile will be of equal range, or don't you calculate it that precise, so its not likely to happen?

I did some tests about it and you're right, it doesn't work the right way.
Here the projectile collides with the purple square but the detected closest square is a green one.
tilenotright.png


I guess I could instead check for the distance between each tile and the projectile instead of the tile behind the projectile. I should just be sure that I won't use the projectile center coordinates because the tiles origin is, if I'm not wrong, on their upper left corner. Or something like that. That's too bad there is no straightforward solution to this…
You do not have the required permissions to view the files attached to this post.
B
12
S
7
G
7
Posts: 450
Reputation: 4,276

Post » Sun Mar 01, 2015 9:21 pm

@bladedpenguin - Yeah. You are getting on to something. Physics needs raycasting, the base system class needs raycasting, and the overlap methods needs updating to be a little more situationally robust. (you should tell ashley :) as I am sure he has heard it enough from me, lol)

I have read the source code for both box2d (which is the code runs the physics behavior) and as much of the core classes that runs construct 2 as I can. Changing the physics behavior is perfectly possible. It would be quite easy to add raycasting (most box2d features are easy to add). But you can't mod the system files for construct, so you would have to create a different behavior to handle raycasting vs. solids. This can be done fairly easily as the algorithms for raycasting are easy to find on google.

But, the biggest problem with all of this is the fact that the tile map generates collision polys in a smart manner. For example: if you have a bunch of square collision tiles, the tilemap will group those up and make it so it uses as few collision shapes as possible. This is awsome for optimization, but this means when you hit a poly, and that poly is a large one that makes up an entire group of tiles, finding the tile still requires some mucking about.

This actually becomes a sort of dichotomy between performance and a particular function. Does this all make sense? I am saying that the solution to this problem is going to be, somewhat more nuanced than it appears at first look.
Image
B
33
S
11
G
2
Posts: 564
Reputation: 5,153

Post » Mon Mar 02, 2015 3:32 am

Hrm this is going to be tricky to work with the optimization that C2 already does, and especially to do it in a way that does not break with updates. My project mostly doesn't use physics, so I would be raycasting against the collision poly. The functions to get that poly aren't in the SDK, so even after finding them, they might change or put the engine in an odd state. I'm content to give it a rest for now. My knowledge of the engine isn't up to this.
B
14
S
5
G
1
Posts: 189
Reputation: 1,536

Post » Fri Jun 19, 2015 12:52 pm

@nimos100
I am wondering how do we erase/change a range of tiles on a collision with a bullet. For instance, a bullet hits a tile, and then it should trigger a change of 8 tiles around the one that was hit. Let's say we erase those 9 tiles of tilemap 1 and replace them with tiles of tilemap 2 (the red one).
Image

I only managed to erase and set a tile that was hit , but no luck with erasing and setting neighboring tiles.

Image


nimos100 wrote:Haven't read all the replies. So this might not be what you mean, but how I understood the original question. But in that case you can just ignore it :D

Here is a simple example of how to get the tile.

Image

Image

(Be advise that the tilemap at row 0 for both X and Y is empty so its not an error that it seems like it should be tile 3,7, its just me that didn't put any tiles in that row, so the white area around the tiles are actually row 0)

If you want the coordinate in X,Y you can just convert the TileX and TileY to that.
B
8
S
2
Posts: 48
Reputation: 646

Post » Sat Jun 20, 2015 7:53 am

heliogame wrote:@nimos100
I am wondering how do we erase/change a range of tiles on a collision with a bullet. For instance, a bullet hits a tile, and then it should trigger a change of 8 tiles around the one that was hit. Let's say we erase those 9 tiles of tilemap 1 and replace them with tiles of tilemap 2 (the red one).

I only managed to erase and set a tile that was hit , but no luck with erasing and setting neighboring tiles.


Since you know which tile the hit takes place you can use this to change the surrounding tiles, simply by passing these coordinates to a function that will replace the correct tiles with the new ones, like this.

Image
Image
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,838

Post » Sat Jun 20, 2015 1:29 pm

Thanks @nimos100,
Is tile 12 a red one that replaces 9 tiles?
Here is what i tried but it does not trigger the change
Image

here is how it looks as the base
Image

and this is what i want to achieve
Image
B
8
S
2
Posts: 48
Reputation: 646

PreviousNext

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 18 guests