I need to find the object with the smallest distance out of 14 others to one object, but they constantly move, and the script must recalculate the minimum distance constantly. I cannot figure out how.
Offline
http://scratch.mit.edu/projects/Magnie/718306, it detects which one is closest to the center.
You can probably put a forever loop around it so it'll continue to detect the closest one.
Offline
Thanks. Is there a way to use that information to make it point to the closest one?
Offline
RedAlertFan wrote:
Thanks. Is there a way to use that information to make it point to the closest one?
Once you know which sprite is closest, you can use the following simple block:
point towards (closest sprite name)or
if <(name) = [a]> point towards[a v] else point towards [b v] enddepending on your needs...
Last edited by LS97 (2012-02-24 06:49:43)
Offline
Pythagoras theorem
c^2 = a^2 + b^2
Put the names of the sprites in a list called sprites (or anything, really) with the exception of the sprite you are comparing to.
Forever set (ShortestDistance) to [1000] set (Counter) to [0] Repeat [13] change (counter) by [1] set (DistanceX) to <[abs v] of <([x position v] of (item (counter) of [Sprites v])) - ([x position v] of [sprite v])>> set (DistanceY) to <[abs v] of <([y position v] of (item (counter) of [Sprites v])) - ([y position v] of [sprite v])>> set (DistanceXSq) to <(DistanceX) * (DistanceX)> set (DistanceYSq) to <(DistanceY) * (DistanceY)> if <[<[sqrt v] of <(DistanceXSq) + (DistanceYSq)>>] < [(ShortestDistance)]> set (ShortestDistance) to <[sqrt v] of <(DistanceXSq) + (DistanceYSq)>> set (ClosestSprite) to (Counter) end endThe text bits are temporary until I can fix em.
Last edited by Smozzick (2012-02-24 13:14:24)
Offline