findCloser

  how to decide which of several objects on the stage is nearest
animation distance




  findCloser
Peter  Lee wrote
>>I want to make a simulating bug which moves toward nearest food source 
>>among many food sources.
>
and then Mikael Wirén wrote:
>maths are good for this kind of thing, though i´m no good at it =(
>
>but check out the:
>abs and cos functions.
>
>

I think pythagoras is enough to decide this.


on findCloser bug,f1,f2
  set v1=the loc of sprite f1 - the loc of sprite bug  
  set v2=the loc of sprite f2 - the loc of sprite bug 
  if testLt(v1,v2) then 
    put "the food in sprite " & f1 & " is next"
  else
    put "the food in sprite " & f2 & " is next"
  end if
end

on testLt p1,p2
  set r1=pythar(p1)
  set r2 =pythar(p2)
  return r1<r2
end

on pythar p
  set p1 = getat(p,1)
  set p2 = getat(p,2)
  set erg = p1*p1 +p2*p2
  return erg
end


Best regards

Daniel Plaenitz
<http://www.lingo.de>

There is an example of this on my shock gallery, smarties.htm

Home shock + cgi Bits 'n pieces Director Lingo Shocklets Contact










pageID=l_findCloser