I already know how to get the distances to objects but I need the one with the least distance, so is there any way to get a lowest number?
hah- the funny thing is, i’m pretty sure I knew at one point, but it ecapes me now. I have a feeling that it’s really, REALLY obvious.
put all of the distances in a list, then sort the list with:
listname.sort()
that sorts the numbers from highest to lowest or lowest to highest.
If you put every object into a list, you can determine the one with the shortest distance while parsing through the objects:
- Store the name and distance of the first object you get.
- If there are no more objects you have the name of the closest object stored and you are finished. Otherwise continue with 3.
- Get the next object.
- If the distance of the object from 3. is shorter than the stored distance, store the distance and the name of this object.
- Continue with 2.More objects to parse = slower (but it is linear expenditure O(n))
There is no need to sort the list as the distances will change when moving. Sorting needs more effort than linear looking throught the list.
Avoid doing this at every frame.
If you have only some objects to check (not all) you can build an object list at startup. Use this list to find the closest object with the above method. This helps to decrease the amount of objects to parse. You can also add or remove objects from the list later.
I hope this helps
thanks I’ll give it a shot