RotateToPoint
Degrees# = RotateToPoint(ViewerX#, ViewerY#, ViewerAngle#, TargetX#, TargetY#, Tolerance#)
 
Parameters:

    ViewerX# = The X coordinate of the viewer object
    ViewerY#= The Y coordinate of the viewer object
    ViewerAngle# = The current angle of the viewer object
    TargetX# = The X position of the target object
    TargetY# = The Y position of the target object
    Tolerance# = The minimum difference in degrees, where object should not be rotated
Returns:

    Degrees# = The number of degree these two angles differ
 

      RotateToPoint returns the direction that we need to rotate an object in order to face the target point. This is useful for making a game characters move/turn towards other characters/players.



FACTS:


      * RotateToPoint 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 RotateToPoint to turn towards the target object.


  
; set the targets position and it's current angle
  targetx#=300
  targety#=300
  targetangle#=90
  
; init my angle
  MyAngle#=270
  
; Start of Do/LOOP
  Do
     Cls RGB(0,0,0)
     
     MyX#=MouseX()
     MyY#=MouseY()
     
   ; Calc what direction we need to rotate
   ; in order to face the raget
     Direction#=RotateToPoint(MyX#,MyY#,MyAngle#,targetx#,targety#,2)
     
   ;
     If Direction#<>0
        MyAngle#=WrapAngle(MyAngle#,Direction#)
     EndIf
     
   ; Draw my position and angle
     DrawObject(MyX#,MyY#,MyAngle#,RGB(0,255,0))
     
   ; DRaw the target
     DrawObject(TargetX#,TargetX#,TargetAngle#,RGB(255,0,0))
     
     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 | CosNewValue | CosRadius | CurveAngle | GetAngle2D | Sin | SinNewValue | SinRadius | TurnDirection | WrapAngle :
 


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