TurnDirection
Direction# = TurnDirection(CurrentAngle#, TargetAngle#, Tolerance)
 
Parameters:

    CurrentAngle# = The current angle of the viewer object
    TargetAngle# = The Target angle we wish to turn our object towards
    Tolerance = The minimum difference in degrees where the object should not be rotated
Returns:

    Direction# = The direction that the object should turn (0=no turn, 1 or -1)
 

      TurnDirection returns the direction that we need to change the current angle by in order for it be aligned to the target angle. The function will turn the shortest distance.



FACTS:


      * TurnDirection returns a zero when target point is within the required tolerance.




Mini Tutorial:


      In this example the user controls a test object. The object uses TurnDirection to rotate towards the target object.


  
; set the targets position and it's current angle
  targetx#=300
  targety#=300
  targetangle#=90
  
; angle
  Angle#=270
  
  Do
     Cls RGB(0,0,0)
     
     mX#=MouseX()
     mY#=MouseY()
     
   ; get the angle of the target
     AngleToTarget#=GetAngle2D(mx#,my#,targetx#,targety#)
     
   ; find the smallest direction we need to turn in order
   ; to face our target direction
     Direction#=TurnDirection(angle#,AngleToTarget#,2)
     
   ; if the direction is <> 0 then we turn
     If Direction#<>0
        Angle#=WrapAngle(angle#,Direction#)
     EndIf
     
   ; draw our object
     DrawObject(mx#,my#,angle#,$00ff00)
     
   ; draw the target object
     DrawObject(targetx#,targety#,targetangle#,$ff0000)
     
     Sync
  Loop
  
  
  
Function DrawObject(x#,y#,angle#,Col)
  x2#=CosNewValue(x#,angle#,100)
  y2#=SinNewValue(y#,angle#,100)
  CircleC x#,y#,10,false,col
  LineC x#,y#,x2#,y2#,col
  
  d=180-25
  x3#=CosNewValue(x2#,angle#-d,10)
  y3#=SinNewValue(y2#,angle#-d,10)
  LineC x2#,y2#,x3#,y3#,col
  
  x3#=CosNewValue(x2#,angle#+d,10)
  y3#=SinNewValue(y2#,angle#+d,10)
  LineC x2#,y2#,x3#,y3#,col
  
  CenterText x#,y#,Angle#
EndFunction
  




This example would output.

  
  no Text Output
  

 
Related Info: AngleDifference | Cos | CosH | CosNewValue | CosRadius | CurveAngle | GetAngle2D | Sin | SinH | SinNewValue | SinRadius | WrapAngle :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com