- Home /
Determine neighbours of grid cell :2D
I have generated grid with specific pattern(controlled grid)..And now i want to make list of all neighbours of perticular cell.
I have created some of the cell with transform and some of them filled with sphere prefab.
When i shoot sphere , i need to check the sphere with same color. And if they are more than 3 then i want to destroy them. But i do not understand the way how to manage neighbours for each cell.
How will i get neighbours of the collided object in shooting script associated with each sphere ?? Pleaze somebody guide me.
Code to generate Grid:
public var sphere : Transform[];
private var B : int = 0;
private var G : int = 1;
private var P : int = 2;
private var R : int = 3;
private var Y : int = 4;
private var E : int = 5;
public var m : int= 12;
public var n : int = 12;
public var radius : float = 0.5f;
public var useAsInnerCircleRadius : boolean = true;
private var offsetX : float;
private var offsetY : float;
private var formation = [ [E ,E ,E ,E, E, E, B, Y, Y ],
[E ,E ,E ,E, E, E, Y, Y, Y ],
[E ,E ,E ,E, E, E, Y, G, G ],
[E ,E ,E ,E, E, E, P, R, G ],
[E ,E ,E ,E, E, E, R, R, R ],
[E ,E ,E ,E, E, E, R, R, R ],
[E ,E ,E ,E, E, E, P, B, B ],
[E ,E ,E ,E, E, E, B, Y, B ],
[E ,E ,E ,E, E, E, G, E, Y ] ];
function Start() {
var go : GameObject = GameObject.Find("Grid");
var unitLengthv : float = ( useAsInnerCircleRadius )? (radius / (Mathf.Sqrt(3)/2)) : radius;
offsetX = Mathf.Sqrt(3);
offsetY = 1.5f;
for(var i : int = 0; i < formation.length; i++ ) {
for(var j : int = 0; j < formation[i].length; j++ ) {
var hexpos : Vector2 = HexOffset( i, j );
var pos : Vector3 = new Vector3( hexpos.x, hexpos.y, 0 ) + transform.position;
var tempsphere : Transform = Instantiate(sphere[formation[i][j]], pos, Quaternion.identity );
tempsphere.transform.parent = go.gameObject.transform;
if(formation[i][j] != E)
{
tempsphere.transform.rigidbody.constraints = RigidbodyConstraints.FreezeAll;
}
}
}
}
function HexOffset( x : int, y : int) : Vector2
{
var position : Vector2 = Vector2.zero;
if( y % 2 == 0 ) {
position.x = x ;//* offsetX;
position.y = y ;//* offsetY;
}
else {
position.x = ( x + 0.5f );// * offsetX;
position.y = y;// * offsetY;
}
return position;
}
Here E stands for empty space. If the bullet sphere is not collided with same color, then it wil be placed at nearest empty position..
Thanks.
Answer by Graham-Dunnett · Mar 22, 2013 at 11:44 AM
formation[i][j] has neighbours:
formation[i-1][j ]
formation[i ][j-1]
formation[i+1][j ]
formation[i+1][j+1]
However you need to check that i-1 is not negative, that j-1 is not negative, and that i+1 and j+1 are not larger than the array.
Thanks sir.. But in my shooting script, How can i deter$$anonymous$$e the neighbours of the collided object ? I am very confused. $$anonymous$$y shooting script is associated with sphere prefab.
Answer by robertbu · Mar 22, 2013 at 04:46 PM
When you posted your question, you did not include a reference to a picture of the layout. The code you are using above is represented as a rectangular grid (2D array), but you are actually using a hexagonal grid. @Graham Dunnet's solution will work just fine for a rectangular grid, but it won't work for your code.
There are several different ways you can approach the problem. One way would be to compare the position of the object you collided with to the other game objects. When I say 'position', I mean the actual position of the game object, not the contact point. In a hexagonal grid, all the neighbors are equal distance from each other, so just pick a distance slightly larger than the distance you used in the layout of your grid.
Answer by Maulik2208 · Mar 25, 2013 at 07:25 AM
Assuming that you are developing something like Bejeweled......And if it is the case then Using a 2d array is a Good idea and for perfectly checking for the neighbors then you can opt from several ways.....you can use Tagging as well as check the texture of the neighbor as well i was confused at the same point and got some help from here...... Bejeweled Neighbour
<-------- this link will lead you to my question and also there is a solution posted by @CodeMasterMike hope that solution will help you also.........Have a Happy coding.....
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Glitching between box colliders. One is kinematic... Help! 1 Answer
A Question about sprites 3 Answers
How to make 2d distortion? 2 Answers
Get position from Isometric TileMap 1 Answer