BGE - How to: Snap spawning objects to grid.

Hi dear members of the Forum. I was working on a “Bomberman” like game. Due to other works couldn’t continue until now.

In Bomberman, when a player put a Bomb, it doesn’t spawn in the Origin point of the player mesh… they always spawn arrangled in the closer square unit of a kind of grid (like in the Image).

I done this by putting Empty objects every 1 unit in the floor of the game. So if player is touching the Empty and press “Bomb key”, the bomb will spawn in that Empty Origin point.

The problem with this, is that all the information about what items the player picks (number of bombs, lenght of fire explotion, type of bomb, etc) is not stored in the Player Mesh, but in each one of this Empty objects (cause Empties are the ones that spawn bombs). Cause various reassons this becomes a trouble for me.


So the question is, there is a way to make the bombs spawns into the closer unit of a kind of grid, keeping all information in the Player mesh? Like the Player mesh is the one that spawns bombs, but the Bomb object will SNAP to some sort of invisible grid…


If anyone have an idea, please tell me. And due to I’m still such a noob (but enthusiast noob) in BGE, I only use Logic Bricks…

Thanks!

Attachments


Here’s my idea:

Your question intrigued me so… I attach my solution in the blend file. It’s kind of long to explain so you’ll just have to download it and see.

Just a few notes:

  • the bombs (spheres) are spawned only on the Y axis for simplicity
  • move the player (or I should say the thing that is going to spawn the bombs - cube) with A, D on the x axis… this is just to make sure it actually works :slight_smile: and Left Mouse Button to add bomb
  • and the simple motion sensor has to be set a velocity that is equal to the spacing of your grid…

Let me know if it helps in any way.

example.blend (528 KB)

Hi razvanc87, thanks for your reply. The player need to be able to move freely (not grid restriction), only the bombs are restricted to grid. The idea you gave is not bad, but would be if player ONLY could move unit per unit into same grid as bombs, that is not the case :frowning:

If you have ever player bomberman, you know what I mean. Even in the image I attached, you can see all bombs and placed in middle of each unit, but FOR EXAMPLE, the red character player is in middle of two units.

Thanks, and any other idea will be welcome. :slight_smile:

Pfiu… yeah, I totally forgot that the player doesn’t snap to the grid. But anyway, what you’re saying isn’t all that doable only with logic bricks. This would be very easy with Python though… you just check for the nearest grid box (in the intended direction) and plant the bomb there. Are you willing to accept Python recipes? :slight_smile:

At this point, I guess I need to accept them, hehe. Would you share any recipe, easy to do (understand) for someone don’t know Python? (always said will learn but well… time has come). Thanks.

Ok, so for understand… if we have the characters named “PLAYER”, the object that will spawn named “BOMB” and our grid with each unit named (F1, F2, F3, F4, F5. …) how would be a simple “recipe”?. :stuck_out_tongue:

Attachments


OK, I’ve made a blend file for you to play with. Hope it helps.

snap.blend (587 KB)

Oh my god!, this is just exactly what I was looking for from almost the first day I started the project. Works just PERFECT! and you made it all well explained (even for a noob as me hehe). Bro, you’re my hero! Mega thanks a lot! Serious thanks, thanks :slight_smile:

Glad I could help :slight_smile: