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

This question already has an answer here:

Is there an algorithm that would calculate the location of the nearest MovieClip in an array? Perhaps a loop? If so, what?

share|improve this question
2  
Loop through the items measuring their distance. Keep track of the shortest distance and the object that has it. When you've looped through all the items, return the one with the shortest distance. – Byte56 Apr 21 at 20:41
I'll try it. I'll try it. – Abe Apr 21 at 20:47
@Byte56 No, this is flash and actionscript, I do not understand C++ code. – Abe Apr 21 at 21:38
1  
@Abe It's actually the key information that you needed. If you want to know if one distance is greater or smaller than two other points you could easily compare both values. You could do the same for a complete list/array. If you look at Bytes answer he does exactly that. I don't want to sound harsh, but is your brain turned on? – Sidar Apr 22 at 2:04
1  
@Abe Look at Bytes answer. Look at how he returns the shortest object. The code is trivial and not hard to understand. You have 4 down votes because 1: It's a bad question. 2: Clearly you didn't do enough research yourself. Why do you voted his answer as a solution if you still don't understand how this actually works? – Sidar Apr 24 at 17:22
show 6 more comments

marked as duplicate by Byte56, bummzack, Josh Petrie, Anko, msell Apr 25 at 8:53

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

up vote 2 down vote accepted

Something like the following algorithm:

getNearest(Vector3f position) {
    distance = MAX_VALUE
    tmpDistance = MAX_VALUE
    nearestObject = null
    foreach objectInArray //for each object in your array
        //get the distance from that object to the position you're interested in
        tmpDistance = getDistance(currentObject, position)
        if (tmpDistance < distance) { //if it's nearer, set it as nearest
            distance = tmpDistance
            nearestObject = currentObject
        }
    }
    return nearestObject
}

If you find that this is slow, and it will be if you have a lot of items, you'll want to look into spatial partitioning and other methods for optimizing finding objects that are close.

share|improve this answer
Well, if I could convert that to flash code............................................................................‌​.................................................................................‌​. this is not flash................................... I could convert it!..............................................But irrevelant answer to the following code format. – Abe Apr 21 at 21:39
2  
It's pseudo code. It's not irrelevant. I'm not here to write your code for you. If you're unsure how to write the above in flash, you need to go back to some basic tutorials in flash and learn how to use it. – Byte56 Apr 21 at 21:44
Ok nevermind I was able to convert it, thanks. At least I think, since I know english. – Abe Apr 21 at 21:48

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