How do I know which tile is being hit on collision

Get help using Construct 2

» Sat Aug 01, 2015 4:02 pm

nimos100 wrote:

nimos100 wrote:

@nimos100
Great tutorial!

1) It is strange but when I shoot at the TilemapBoxEnemy, i got Tilemap1 cells generated around it instead of replacing a tile of TilemapBoxEnemy with a tile of Tilemap1. "Text" indicates if there are some log changes.

Before shooting with Bullet2Player

After shooting with Bullet2Player

Here is my code
Object coordinates defined and converted

Bullet coordinates defined and converted

2) I would want actually to complicate even more the code by adding another sub condition with comparing instance variables.
Visually, it would work in that way:
The bullet2Player overlaps TilemapBoxEnemy.

If there is a TurretBody around a tile of TilemapBoxEnemy, the bullet2Player is just destroyed.

If there is no any TurretBody around a tile of TilemapBoxEnemy, the bullet2Player is destroyed and we check if any Tilemap1 tiles surround a tile of TilemapBoxEnemy. Depending on the location of Tilemap1 tiles we set different tiles to tile of TilemapBoxEnemy. For instance, there is a tilemap1 (X, Y+1 ) in relation to a tile (X, Y) of a TilemapBoxEnemy, so a tilemap1 or tilemap4 replaces randomly an overlapped tile of TilemapBoxEnemy.
Tilemap1 replaces a tile.

or
Tilemap4 replaces a tile

or any other tilemap replaces a tile indicated in a random basket of tilemaps. Here I wonder how can we control a random frequency of a particular tilemap appearance.
So the code of the additional sub condition is the following

3)
To continue a topic about random tilemaps,
let's say we created the level which consists of arranged tiles of Tilemap1.

Can we on start of layout to set random tiles of other Tilemaps to Tilemap1?

For Instance, on start of layout some tiles were changed

Here, we should again to check for location of tiles around each tile of Tilemap1. If any of 5 conditions 1)X,Y+1 2)X+1, Y 3) X-1, Y 4) X, Y-1 5) else is satisfied, we would set particular tilemaps to a tile (X,Y) .
The problem is that there is no overlapping here, no interaction with a tilemap1. How can we randomly distribute tiles of many different tilemaps on a tilemap1 without knowing the coordinates of each tile?
B
8
S
2
Posts: 48
Reputation: 646

» Mon Aug 03, 2015 10:28 am

heliogame wrote:1) It is strange but when I shoot at the TilemapBoxEnemy, i got Tilemap1 cells generated around it instead of replacing a tile of TilemapBoxEnemy with a tile of Tilemap1. "Text" indicates if there are some log changes.

Not really sure what is wrong with that, as im not really sure what the different tilemaps are and what role they play.

heliogame wrote:If there is no any TurretBody around a tile of TilemapBoxEnemy, the bullet2Player is destroyed and we check if any Tilemap1 tiles surround a tile of TilemapBoxEnemy. Depending on the location of Tilemap1 tiles we set different tiles to tile of TilemapBoxEnemy. For instance, there is a tilemap1 (X, Y+1 ) in relation to a tile (X, Y) of a TilemapBoxEnemy, so a tilemap1 or tilemap4 replaces randomly an overlapped tile of TilemapBoxEnemy.
Tilemap1 replaces a tile....
...or any other tilemap replaces a tile indicated in a random basket of tilemaps. Here I wonder how can we control a random frequency of a particular tilemap appearance.
So the code of the additional sub condition is the following

Not 100% I understand what you mean, but if I do. The frequency of what tile you want to replace with, Tilemap1 or Tilemap 4, you can control using a condition and a random roll.

heliogame wrote:Here, we should again to check for location of tiles around each tile of Tilemap1. If any of 5 conditions 1)X,Y+1 2)X+1, Y 3) X-1, Y 4) X, Y-1 5) else is satisfied, we would set particular tilemaps to a tile (X,Y) .
The problem is that there is no overlapping here, no interaction with a tilemap1. How can we randomly distribute tiles of many different tilemaps on a tilemap1 without knowing the coordinates of each tile?

You need some sort of condition to know where to place the tiles. So placing them without knowing under what conditions you want to place them is not possible. So you have to have some rules.
B
45
S
12
G
3
Posts: 1,210
Reputation: 7,559

» Tue Aug 04, 2015 2:01 pm

nimos100 wrote:
heliogame wrote:
heliogame wrote:Here, we should again to check for location of tiles around each tile of Tilemap1. If any of 5 conditions 1)X,Y+1 2)X+1, Y 3) X-1, Y 4) X, Y-1 5) else is satisfied, we would set particular tilemaps to a tile (X,Y) .
The problem is that there is no overlapping here, no interaction with a tilemap1. How can we randomly distribute tiles of many different tilemaps on a tilemap1 without knowing the coordinates of each tile?

You need some sort of condition to know where to place the tiles. So placing them without knowing under what conditions you want to place them is not possible. So you have to have some rules.

Ok, but at least can I identify tiles around the hit without setting them at the particular coordinates as you showed in your sample with cactus? The problem is that if I set every tile at the start of layout to design the level I will spend days and nights.
So, let's say we have Cactuses ( tilemap) and green tiles (tilemap2). When bullet hits cactus , I need to know if there are other cactuses and green tiles around the hit.
I worked on it using your sample but it seems that C2 does not understand this part

As a result, it does not recognize tiles around the cactus.

CapX
https://www.dropbox.com/s/apt75015upl1s ... .capx?dl=0
B
8
S
2
Posts: 48
Reputation: 646

» Tue Aug 04, 2015 3:04 pm

heliogame wrote:I worked on it using your sample but it seems that C2 does not understand this part

As a result, it does not recognize tiles around the cactus.

I downloaded the Capx and it seems like you get confused about the tile coordinates. In the screenshot you are setting a lot of tile positions, which makes little sense. First of all, to do comparisons with a tilemap you don't need/benefit from storing any tile positions. As it contains so many tiles, in theory you don't have to store any tile positions at all, the only reason to do it is because its faster to just store the tile positions than having to convert them all the time. But it makes no different when working with them. When I store tile position for objects, its only for non-tilemap objects, like the bullet sprite and so forth.

Whenever you have to compare something with a tilemap you have to convert the X,Y position that you want to compare or you use the tile position that you generated before hand. Again it makes no difference in how C2 works, its purely for your own sake that you do it!!.

This is the only part that need a conversion, because that is what keep track of the bullet position. And instead of having to keep converting it, whenever i want to compare the bullets position with something on the tilemap. I just do it here and store it in the bullet.

This is where it goes wrong. Because the initial condition and the one we are interested in is the bullet position compared to the tilemap. In the first line i use a "Tilemap.TileAt(Bullet.TileX, Bullet.TileY) = 15" This is the part where i might as well have converted it on the fly, like this:

But as you can see it requires me to write a lot more code, so to save time I just convert it once and use Bullet.TileX and Y instead. But the code does exactly the same.

Moving to 9-11
You are comparing Tilemap.TileX = Bullet.TileX Which is fine.
But you are also comparing Tilemap.TileYmin1 = Bullet.TileY - 1, which doesn't work because that is set in the start of layout, when the bullet position can be anything. So this comparison will never be true, and its the same for all the checks that is done. The reason it write that no tile was found anywhere around the cactus, is because the Else is misplaced, and will always be true because (10) will never be.

The way i would do it, if i understand you correct, is to layout what I want to happen. Meaning write down the flow you need to make it work.

Code: Select all
`1. Bullet hit cactus2. Check if there are other cactuses around the bullet.         a. There is another cactus around the bullet -> Do something.         b. There is not another cactus around the bullet -> Go to 33. Check if there are any green tiles around the bullet.         a. There is another green tile around the bullet -> Do something.         b. There is no green tile around the bullet`

Then go through each step and add them to the code. Even though you might already know what you want to happen, outlining it can be very helpful I think, especially if you need to keep track of a lot of positions and so on. Because it seems you get confused with what you are trying to do, and what you need to do, to make it work. So you start adding a lot of variables that are not needed.
B
45
S
12
G
3
Posts: 1,210
Reputation: 7,559

Previous