How do I know which tile is being hit on collision

Get help using Construct 2

Post » Sat Jun 20, 2015 2:09 pm

heliogame wrote:Thanks @nimos100,
Is tile 12 a red one that replaces 9 tiles?

Yes this example only use one tilemap.

Without the Capx it im not 100% certain.
But think the wait 3 seconds might be causing the problem as you destroy the bullet, but want to pass it to the function 3 seconds later, im not certain if that is working well or how it works, as i tend to avoid waits. But you have to remember that the rest of your program doesn't pause while this wait is going on. If i remember correct objects that are destroyed will be removed from the game at the next tick. Meaning that the bullet you are trying to pass to the function doesn't exist anymore when you actually need the tile position, and since you are waiting 3 seconds which is 180 ticks a lot of things can go on in the meantime. But anyway im not 100% certain, but regardless of that, i think such situations are best to be avoided because you lack overview of whats going on with waits in your programs.

So i would use the bomb you create to spawn at the correct Tile (Where the bullet hit) and then add a timer behaviour to that and set it to 3 second, store the Tile position in the bomb or you can generate it later when the timer is up, then use the bomb tile position with the function. Meaning you pass the bomb coordinates to the function rather than the bullet. Think that will solve the problem.

Otherwise try to add the capx.
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,848

Post » Sat Jun 20, 2015 4:21 pm

nimos100 wrote:
heliogame wrote:Thanks @nimos100,
Is tile 12 a red one that replaces 9 tiles?

Yes this example only use one tilemap.

Without the Capx it im not 100% certain.
But think the wait 3 seconds might be causing the problem as you destroy the bullet, but want to pass it to the function 3 seconds later, im not certain if that is working well or how it works, as i tend to avoid waits. But you have to remember that the rest of your program doesn't pause while this wait is going on. If i remember correct objects that are destroyed will be removed from the game at the next tick. Meaning that the bullet you are trying to pass to the function doesn't exist anymore when you actually need the tile position, and since you are waiting 3 seconds which is 180 ticks a lot of things can go on in the meantime. But anyway im not 100% certain, but regardless of that, i think such situations are best to be avoided because you lack overview of whats going on with waits in your programs.

So i would use the bomb you create to spawn at the correct Tile (Where the bullet hit) and then add a timer behaviour to that and set it to 3 second, store the Tile position in the bomb or you can generate it later when the timer is up, then use the bomb tile position with the function. Meaning you pass the bomb coordinates to the function rather than the bullet. Think that will solve the problem.

Otherwise try to add the capx.



I passed the bomb coordinates to the function and it worked! Thank you.
B
8
S
2
Posts: 48
Reputation: 646

Post » Sat Jun 20, 2015 5:19 pm

@nimos100,

There is one problem with setting a new tile to a range of tiles around the hit. As long as it is always a square that fills the whole new area, it adds unnecessary tiles like I highlighted (crossed) in your picture.
Image

So, imagine that we hit at what an arrow points out
Image
How should we deal with changing tiles without adding unnecessary ones?
I guess we should add some kind of loop that goes through all the tilemap making a check against a tile that is not empty and then changing only those tiles, but it seems wacky combining it with functions. Uhh..
B
8
S
2
Posts: 48
Reputation: 646

Post » Sat Jun 20, 2015 6:59 pm

Yeah you can do it with a loop. And just comparing what type of tile is influence and then based on that change them. There are no difference really whether its in a function or not. Except functions makes it easier to work with your project in general so a function whenever its possible :D

Since you already know the starting point of which tiles to check meaning the impact tile X,Y and if you want the 8 tiles surrounding that you just have to start at that position (X,Y) - 1 so (X-1, Y-1). So you can do it like this:

Image

Since a tilemap will return -1 if there are no tile you can use that.
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,848

Post » Sat Jun 27, 2015 8:27 am

@nimos100,

Strange, but it did not worked for me. Nothing happens except that Bomb object is erased.
Image
Image
B
8
S
2
Posts: 48
Reputation: 646

Post » Sat Jun 27, 2015 12:08 pm

Strange, but it did not worked for me. Nothing happens except that Bomb object is erased.


Does it work if you remove the "Wait 3 seconds" ?
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,848

Post » Sat Jun 27, 2015 1:26 pm

nimos100 wrote:
Strange, but it did not worked for me. Nothing happens except that Bomb object is erased.


Does it work if you remove the "Wait 3 seconds" ?

@nimos100

Ah, I missed "1" in the code (functionBomb.Param(1)-1) . Now , it works great. Wow. Cool.


By the way, I am thinking of improving the code of locating an object around the tile we hit. For instance, we have a tile and know its coordinates like Tilemap. PositiontoToTileX(bullet.X) , Tilemap.PositionToTileY(bullet.Y). How do we coordinate tiles Up, Right, Left, Down around this tile?
Image
I just want to replace the location of objects by position Image as it seems to be not too rational.
B
8
S
2
Posts: 48
Reputation: 646

Post » Sat Jun 27, 2015 5:33 pm

heliogame wrote:By the way, I am thinking of improving the code of locating an object around the tile we hit. For instance, we have a tile and know its coordinates like Tilemap. PositiontoToTileX(bullet.X) , Tilemap.PositionToTileY(bullet.Y). How do we coordinate tiles Up, Right, Left, Down around this tile?


Since the bullet impact is always at (0,0) in tile value or what to call it, meaning this will always be by default the original tile coordinate of (TileX, TileY) regardless of where on the tilemap the bullet hit. Knowing this and looking at the image below you can see which value to add to this coordinate to get one of the surrounding tiles.

The reason you can do it this easy, is because you convert the bullets coordinate X,Y to a tile coordinate and therefore its easier to work with.

So in your case to get the different tiles you just add the correct amount to the TileX, TileY after the conversion:
Top:
Tilemap. PositiontoToTileX(bullet.X) , Tilemap.PositionToTileY(bullet.Y) - 1

Left:
Tilemap. PositiontoToTileX(bullet.X) - 1, Tilemap.PositionToTileY(bullet.Y)

Right:
Tilemap. PositiontoToTileX(bullet.X) + 1, Tilemap.PositionToTileY(bullet.Y)

Down:
Tilemap. PositiontoToTileX(bullet.X), Tilemap.PositionToTileY(bullet.Y) + 1

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

Post » Sat Jun 27, 2015 10:26 pm

nimos100 wrote:
heliogame wrote:By the way, I am thinking of improving the code of locating an object around the tile we hit. For instance, we have a tile and know its coordinates like Tilemap. PositiontoToTileX(bullet.X) , Tilemap.PositionToTileY(bullet.Y). How do we coordinate tiles Up, Right, Left, Down around this tile?


Since the bullet impact is always at (0,0) in tile value or what to call it, meaning this will always be by default the original tile coordinate of (TileX, TileY) regardless of where on the tilemap the bullet hit. Knowing this and looking at the image below you can see which value to add to this coordinate to get one of the surrounding tiles.

The reason you can do it this easy, is because you convert the bullets coordinate X,Y to a tile coordinate and therefore its easier to work with.

So in your case to get the different tiles you just add the correct amount to the TileX, TileY after the conversion:
Top:
Tilemap. PositiontoToTileX(bullet.X) , Tilemap.PositionToTileY(bullet.Y) - 1

Left:
Tilemap. PositiontoToTileX(bullet.X) - 1, Tilemap.PositionToTileY(bullet.Y)

Right:
Tilemap. PositiontoToTileX(bullet.X) + 1, Tilemap.PositionToTileY(bullet.Y)

Down:
Tilemap. PositiontoToTileX(bullet.X), Tilemap.PositionToTileY(bullet.Y) + 1

Image

@nimos100,
can you show a part of the code using these coordinates? I used them previously but none did not work for me. An object just does not appear. Maybe I am missing something.

Image
B
8
S
2
Posts: 48
Reputation: 646

Post » Sun Jun 28, 2015 9:49 am

heliogame wrote:@nimos100,
can you show a part of the code using these coordinates? I used them previously but none did not work for me. An object just does not appear. Maybe I am missing something.

Image


The reason it doesn't work is because you are using a tile coordinate in your creation but it requires a value in (x , y). so to solve that you have to first convert the bullet (x , y) value to a tile value (TileX, TileY) and then you use that value to convert to a (x, y) value so you can place it on the tilemap.

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

PreviousNext

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 22 guests