playMaker

Author Topic: Most Efficient way to detect what GO is next to another GO?  (Read 3676 times)

jasperPT

  • Full Member
  • ***
  • Posts: 115
  • I am a VFX Animator teaching myself to make games
Most Efficient way to detect what GO is next to another GO?
« on: September 24, 2013, 05:14:52 PM »
Hi,

I am trying to set up a grid system, and have it so that each grid piece knows what other grid piece is on either side, above and below it.

This is so that they can share information and send each other events, or figure out what state they are in to do things like match 3 and so on.

Currently I am doing it by creating a small "link" collider on each side of the tiles, one for up, down, left and right.
If this Link collides with the Link of another tile, then it will get the information from that connected tile and send it to its owner.


It looks something like this
$ = Tile
* = Link

  *
*$*
  *
So my Left Link object can set the value of its parents GO variable "Left Link" as the parent of the Link that it collides with.

This seems to get expensive quickly, as it is creating 4 new objects for each tile, (with a minimum of 3 verts for a tri I assume?) so 12 new verts for all 4 sides, and then another 6 if i want to do Y axis (which i do as well).


I am wondering if this is the best and most logical/efficient way to find out what object is next to another object and get information from it like int values and such.

Also to keep in mind is that I need to move these Tiles around at times, or destroy and create new tiles, so they will not always be next to each other and need to know at any given time what it is next to.


Cheers,
Jasper

mweyna

  • Full Member
  • ***
  • Posts: 242
Re: Most Efficient way to detect what GO is next to another GO?
« Reply #1 on: September 25, 2013, 12:35:08 AM »
As a fellow animator learning to code, this is no small task. I've been playing around with a similar system and so far have found the best way to accomplish this is by using Arrays, rather then having each object have its own FSM. For performance reason, if every block has an FSM, and say you have 200 blocks, suddenly you have 200 constantly runnings FSMs and your logical frame rate will take a dive.

What I did to accomplish this was rather complex, but seems to be working well enough on a performance level.

- Using PoolManager (http://poolmanager.path-o-logical.com/) I spawn the block geo from a prefab.
- Using Array's, I first generate an Array of coordinates for my map size, 0,0,1, 0,0, 2 etc, to fit my world size that I define (15x15).  Then at those coordinates at the same Array ID's to the coordinates I spawn the Geo in another array.
- In general, I've been keeping all my FSMs on the top level as much as possible. So rather then an individual object looking for it's neighbor, you'd have a single FSM on the top of the scene level, running though the array of all blocks, then assigning variables.

As for selection, I've created an FSM that creates a RECT field, then using those Array's will go though and check every block "Am I in this RECT field" and if so, will add those to another temporary selection Array.

Depending upon what you're goal for your game is this could be overkill or just what you need. Feel free to PM me and I'll try to explain my setups in more detail. Either way, definitely check out PoolManager, and learn about Playmaker Arrays.

jasperPT

  • Full Member
  • ***
  • Posts: 115
  • I am a VFX Animator teaching myself to make games
Re: Most Efficient way to detect what GO is next to another GO?
« Reply #2 on: September 26, 2013, 09:07:53 AM »
Thanks for the tips!
Yes transfering my brain from animation mode over to design/programming can be somewhat problematic but very rewarding, im constantly amazed at how creative the problem solving feels.

I have been using poolmanager, only just recently found out about it and had to go through my game setting everything up in pools. It was such a nightmare and im still bug fixing from the process, but the performance boost is incredible.

Im also using array maker in a very similar way, I have a setup where in game I can set the width and height, then it will generate my grid and tiles apropriately, giving each one a name of A1 A2 B1 B2 etc.
I did this mainly because I knew at some stage im going to need to act on these specifically.


However, what I would ultimately like to do, is have a button that adds width and subtracts a row, as well as a button that does the same for height, so you can build levels visually, which I was intending on having as part of the gameplay. This is one of the areas where each tile needed to know if it had a tile activated on its right, left, up or down. (so that if it doesn't then it can activate or create a new tile there) but maybe I am approaching that the wrong way.


Each of my tiles has 1 or more FSMs on it (usually more) because they act in somewhat complex ways, although maybe there is a way to reduce their actions down to an overall single or few FSMs, I might pick your brain some more once I explain my game in a bit more depth.

I have never used RECT fields before, when i get home i can do some research on exactly how they work.
I may have to explain my game a bit more, I will try to do some screen grabs and all of that tonight.


mweyna

  • Full Member
  • ***
  • Posts: 242
Re: Most Efficient way to detect what GO is next to another GO?
« Reply #3 on: September 26, 2013, 11:14:52 AM »
Depending on the intended behaviors and gameplay implications, having any FSMs in my experience on the individual tiles is going to hurt framerate. On my tiles I wanted to be able to have a "selected" textures so when you click on them, it turns it to another color, the way I accomplished this was having an FSM on the tile, that would change the color, then deactivate itself (It was a child object of the tile gameobject). When I wanted to change the color, I'd find the object, turn on the color controller game object, send a command to change the color, and it would turn itself off again automatically.

As far as finding the neighbor state, another approach I took for mine was to at the same time as creating the geometry array, I also created an INT array of state ID's. So when it was spawning A1, B1, etc it would also add a 1 to that same ID number on a different array. So if you were trying to find if the tile next to object was activated or not, you'd check the state number (by finding closet game object to your box position plus 1 to the appropriate float value). Then getting that index id and checking it against the state). If that makes sense. So if I was writing an FSM I'd, get my position, save to floats, add 1 to appropriate direction, get nearest object from an array, get position of object, float compare to ensure it's in the exact coordinates, if so, check the state from my state array, something like that. If you'd just want to see if theres a block there, you could forgo states entirely and do just that part minus states.

jasperPT

  • Full Member
  • ***
  • Posts: 115
  • I am a VFX Animator teaching myself to make games
Re: Most Efficient way to detect what GO is next to another GO?
« Reply #4 on: September 26, 2013, 12:15:35 PM »
This sounds really good, Im going to give it a try.
Thanks!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Most Efficient way to detect what GO is next to another GO?
« Reply #5 on: September 26, 2013, 02:18:58 PM »
@jasperPT; Yes indeed, you said it very well, solving problems in creating the logic and features is indeed something that I also see as very creative. If you only look at the intended purpose of a tool, you are missing out on a lot. Extrapolation is key!

Bye,

 Jean

Remy

  • Playmaker Newbie
  • *
  • Posts: 1
Re: Most Efficient way to detect what GO is next to another GO?
« Reply #6 on: September 28, 2013, 05:29:11 PM »
Really funny, i'm a 3d artist converting to game development too, and i'm also looking for the same answer as you!

I was looking at a way to check neighbour cubes in a 8 x 8 grid (changing states of the 4 adjacent cubes, checking value...) and thought about many solutions.

I first thought about activating a sphere trigger on the pressed cube (it collides with the four needed cubes only) and detecting with a " OnTriggerEnter " for the actions. Then I thought about checking the coordinates (the cubes are those in x-1, x+1, y-1,y+1) based on pressed cube coordinate, but i really lack of playmaker (and programming) knowledge to be efficient.

Good luck for the work, thanks for asking the question first! (i'll give a look at that rect field thing)

jasperPT

  • Full Member
  • ***
  • Posts: 115
  • I am a VFX Animator teaching myself to make games
Re: Most Efficient way to detect what GO is next to another GO?
« Reply #7 on: October 02, 2013, 07:33:26 AM »
that also sounds like something that could work with a bit of tweaking.
I have just got myself 2D toolkit so got a bit distracted working away on that of recent and will come back to this problem soon.