How do I detect and delete tilemap tiles that are overlapping a sprite?

0 favourites
  • 5 posts
From the Asset Store
Each tile 64 by 64 pixels - Ultimate Tile Set for your awesome games
  • Hello there! I teach Construct 3 to 4th-6th graders as an elective at my school. Recently, one of my students has been trying to make a little tank game where two tanks roll around and shoot explosive shells at each other. He wants the terrain to be destroyed in circular chunks, like explosions from the Worms series. I must admit, he is stretching the limits of my understanding of Construct 3, and I want to learn more about how C3 works in order to help him.

    I have dug around on the forums, but I keep finding older topics with broken links and conversations that I can only partially pull context from since all the images are gone.

    From what I can gather and what I have (poorly) attempted, I am using a tilemap with 2x2 tiles to represent my earth, and explosions create a sprite object with a circular-shaped collision box. At this point, I need to load all of the tiles that are overlapping the circle into an array and then delete them, correct? I am a bit fresh on arrays, I have only used them to contain level data for spawning a certain number of enemies on certain levels of a game, that kind of deal.

    So I suppose I am asking, how do I properly detect the overlapped tiles, and how do I load them into an array correctly so that I can use the array to destroy them? Or, am I going about this in a difficult way?

  • I vaguely recall this concept had been explored pretty extensively. It might be worthwhile necroing the thread you found or tagging the owner of the example. If it was a dropbox link the file is likely still there, just the link needs updating.

    If my memory didn't fail me, R0J0hound was involved in the particular thread I was thinking about, and he's definitely still lurking around on these forums.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • This topic has a working capx that should import into construct 3 fine.

    It utilizes some math to erase a circle. Same equation to graph a circle.

    construct.net/en/forum/construct-2/how-do-i-18/simple-scorched-earth-worms-ga-129595

  • It kind of blows my mind that you could remember not only the person involved in the topic from four years ago but the exact topic in question.

    Whenever I ask for help, I try to dig into the examples so I can understand how the end result was reached instead of just copying the code. I struggled with math in school, so I have some questions.

    So, when the game starts, we start at the left side at x = 1 since the loop index starts there (or at 0, we start on the left side, is what it is). We use... uuh some graphing equation (math that I didn't do well on) to create a sine wave that doesn't go below a certian y value. This gives us our "surface" pixel. Then our width is 1 since we are building 1 line at a time from the left, and we do the entire height possible of the screen downwards. If we went down below the earth, we'd see the exact sine wave as the top of the earth on the bottom side 640 pixels down, right?

    I'm a bit behind on the erasing code... I can tell both radius and dx are important here. In this instance, we use radius to set DX in order to get the erase code... so each shot that my students program would edit radius to determine the digging power of the shot.

    So... we take both a negative value of whatever the radius is and a positive value of the radius to build essentially a "diameter" of how big the hole is going to be with a range of values. So with -50 and 50, we have 100 loops of this code. Every time we repeat the code, we shave off a ring of the attacked area from... the outside in? I'm basically guessing. This is why I went to summer school for math, haha.

  • The first part was just to fill the tilemap with something. Ideally I’d just draw an image and load that but that’s another can of worms.

    So anyways I just use the equation for a sine wave to define the terrain:

    y >= y0 + amplitude*sin(frequency*x +x0)

    x0 and y0 just adjust where the sine wave is.

    I use >= because I want everything below the sine wave to be filled.

    So you could fill the tilemap by looking at all the tile locations, plugging x and y of the tile into that formula and if it’s true drawing that tile.

    It could look like this:

    For “x” from 0 to tilemap.width-1

    For “y” from 0 to tilemap.height-1

    Compare: loopindex(“y”)>=y0+amplitude*sin(x0+loopindex(“x”)*frequency)

    — set tile at (loopindex(“x”), loopindex(“y”)) to 1

    Erasing is basically the same. Look at all the tiles and measure their distance to the center of the circle. If the distance is less than the radius, erase that tile.

    Now it’s slow to loop over all the tiles. But we don’t have to. With a circle we can look at the box of them a radius away from the circle.

    We can do it faster by utilizing the “erase range” action instead of erasing each individually.

    So we can loop x from -radius to positive radius.

    And we can calculate the y position of the top half of the circle at that x. The bottom half y is basically flipped.

    Sorry. I’m probably not helping at all. I’ve run out of time.

    Hopefully it got the gist of it out there. The rest was just trickery to do less so it’s faster.

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