Sign up ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Using Unity3d, I have a array which is having 8 GameObjects in grid and one object (which is already known) is in center like this where K is already known object. All objects are equidistant from their adjacent objects (even with the diagonal objects) which means

(distance between 4 & K) == (distance between K & 3) = (distance between 2 & K)

1 2 3
4 K 5
6 7 8

I want to remove 1,3,6,8 from array (the diagonal objects).
How can I check that at runtime?

my problem is the order of objects {1-8} is not known so I need to check each object's position with K to see if it is a diagonal object or not. so what check should I put with the GameObjects (K and others) to verify if this object is in diagonal position

Regards,
Sam

share|improve this question
    
It is really not clear what you are asking. It sounds like you have all the information you need, what is the problem? – zehelvion Mar 13 '14 at 10:12
    
my problem is the order of objects {1-8} is not known so I need to check each object's position with K to see if it is a diagonal object or not. so what check should I put with the GameObjects (K and others) to verify if this object is in diagonal position – samfisher Mar 13 '14 at 10:30

2 Answers 2

One solution would be to get the normal vector between the objects and look at its x and z components. Diagonal objects will have an identical x and z length (assuming the objects are arranged on the X/Z plane, otherwise you'd have to swap components accordingly).

In code:

// MyObjects is an array or list of your corner GameObjects (1-8)
// K is your center GameObject
foreach(GameObject obj in MyObjects){
    Vector3 normal = (obj.transform.position - K.transform.position).normalized;
    // compare against a small float because of floating-point inaccuracies
    if(Mathf.Abs(Mathf.Abs(normal.x) - Mathf.Abs(normal.z)) < 0.001){
        // diagonal object detected!
    }
}
share|improve this answer

If (distance between 4 & K) == (distance between K & 3) = (distance between 2 & K) than it's a circle, therefore there's no diagonal :) If there was a diagonal the distance between the diagonal would be sqrt(2) times bigger than the distance between the adjacent.

Pick one of the objects that is not K, and order the others by distance (ignore K). Exclude the object you picked, 4th, 5th and the last one.

Example, if you picked 1, ordering the others by distance from one would result:

2, 4

3, 6

5, 7

8

You'd remove 1 (the one you picked), 3 and 6 (4th, 5th), 8 (the last).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.