Main Menu

Hi, Field of view test

Started by rtw1701, June 04, 2007, 07:47:32 AM

Previous topic - Next topic

rtw1701

 ???  Hello,

Sorry for my first post to be a question, but I can't get something to work right in some test code I am trying to write.   I want to make it so that an object only "sees" within a certain field of view.   Here is code of what I attempted but it doesn't work for when the object is facing around 0 degress.    Is there just some other way to do this then the mess I came up with?   ::) Thanks.

; PROJECT : angles2
; AUTHOR  : Richard Waterman
; CREATED : 5/30/2007
; EDITED  : 6/3/2007
; ---------------------------------------------------------------------
; this code copied from turntomouse example in help
; I am trying to make a test program and determine if I can code an object that sees within a certain field of view
; Create a triangle shape
  CreateConvexShape 1,50,3
 
; Define some variable to hold the lookers position
  shapePosX#=400
  shapePosY#=400
         x1#=0
         y1#=0
         x2#=0
         y2#=0
        fov#=0
      angle#=0
     inview#=0

; start fo the program main loop
  Do
     
   ; clear the screen to black
     Cls 0
     
   ; get the X/Y coords of the mouse
     mx#=MouseX()
     my#=MouseY()
     
   ; draw a circle at the mouse positon
     CircleC mx#,my#,10,1,RGB(255,0,0)
     
   ; get the angle/direction of mouse from the shape.
    ; angle#=GetAngle2D(shapePosX#,shapePosY#,mx#,my#)
     angle2#=WrapAngle(GetAngle2D(shapePosX#,shapePosY#,mx#,my#),10)
   
   ; rotate the shape to face this direction
     RotateShape 1,angle#,1
   ; draw the shape
     DrawShape 1,shapePosX#,shapePosY#,2
     
     x0#=CosNewValue(shapePosX#,angle#,100)
     y0#=SinNewValue(shapePosy#,angle#,100)
     
     x1#=CosNewValue(shapePosX#,angle#+35,100)
     y1#=SinNewValue(shapePosy#,angle#+35,100)
     
     x2#=CosNewValue(shapePosX#,angle#-35,100)
     y2#=SinNewValue(shapePosy#,angle#-35,100)
     
     a1#=WrapAngle(GetAngle2D(shapePosX#,shapePosY#,x1#,y1#),1)
     a2#=GetAngle2D(shapePosX#,shapePosY#,x2#,y2#)
     
    ; a0#=GetAngle2d(ShapePosX#,ShapePosY#,m0#,y0#)
     
    ; fov#=AngleDifference(a1#,a2#)
      ; a test to see if this works but I don't think it respones the way i had hoped
      inview#=TurnDirection(a0#,angle2#,70)  ; wanted to see if this would "see" within a 70 degree view
     
      Text 10,135,"Facing "+str$(a0#)
     
      ; this is another test of vision,  almost works except when object is facing to the right at 0 degrees
      Text 10,115,"In view? :"+Str$(inview#)
      if ((angle2#=<a1#) and (angle2#>=a2#))
        text 10,45,"in fov, true"
      else
        text 10,45,"in fov, false"
      endif
   
     Text 10,25,"To Mouse Angle "+str$(angle2#)
     Text 10,10,"Ship Facing Angle "+str$(angle#)
   
     Text 10,75,"Line 2 "+str$(a1#)
     Text 10,85,"Line 3 "+str$(a2#)
     
      ; draw a line as pointer
      ;line 1
      Line shapePosX#,shapePosY#,x0#,y0#
      ;line 2
      Line shapePosX#,shapePosY#,x1#,y1#
      ;line 3
      Line shapePosX#,shapePosY#,x2#,y2#
     
   ; refresh the screen
     Sync
     
     If LeftKey() Then Angle# = WrapAngle(Angle#,-0.5)
     If RightKey() Then Angle# = WrapAngle(Angle#,0.5)
   ; loop back to the do statement to keep the program running
  Loop
 





kevin

Try this



; this code copied from turntomouse example in help
; I am trying to make a test program and determine if I can code an object that sees within a certain field of view
; Create a triangle shape
  CreateConvexShape 1,50,3
 
; Define some variable to hold the lookers position
  shapePosX#=400
  shapePosY#=400
         x1#=0
         y1#=0
         x2#=0
         y2#=0
        fov#=0
      angle#=0
     inview#=0

; start fo the program main loop
  Do
     
   ; clear the screen to black
     Cls 0
     
   ; get the X/Y coords of the mouse
     mx#=MouseX()
     my#=MouseY()
     
   ; draw a circle at the mouse positon
     CircleC mx#,my#,10,1,RGB(255,0,0)
     
   ; get the angle/direction of mouse from the shape.
    AngleToMouse#=GetAngle2D(shapePosX#,shapePosY#,mx#,my#)
;     angle2#=GetAngle2D(shapePosX#,shapePosY#,mx#,my#)


   
   ; rotate the shape to face this direction
     RotateShape 1,angle#,1
   ; draw the shape
     DrawShape 1,shapePosX#,shapePosY#,2
     
     x0#=CosNewValue(shapePosX#,angle#,100)
     y0#=SinNewValue(shapePosy#,angle#,100)
     
     x1#=CosNewValue(shapePosX#,angle#+35,200)
     y1#=SinNewValue(shapePosy#,angle#+35,200)
     
     x2#=CosNewValue(shapePosX#,angle#-35,200)
     y2#=SinNewValue(shapePosy#,angle#-35,200)
     

      AngleDiff#=AngleDifference(AngleToMouse#,Angle#)
     
      ; this is another test of vision,  almost works except when object is facing to the right at 0 degrees
    if AngleDiff#<35
        text 10,45,"in fov, true"
      else
         text 10,45,"in fov, false"
      endif
   
     Text 10,25,"To Mouse Angle "+str$(AngleToMouse#)
     Text 10,10,"Ship Facing Angle "+str$(angle#)
   
     Text 10,75,"Line 2 "+str$(a1#)
     Text 10,85,"Line 3 "+str$(a2#)
     
      ; draw a line as pointer
      ;line 1
      Line shapePosX#,shapePosY#,x0#,y0#
      ;line 2
      Line shapePosX#,shapePosY#,x1#,y1#
      ;line 3
      Line shapePosX#,shapePosY#,x2#,y2#
     
   ; refresh the screen
     Sync
     
     If LeftKey() Then Angle# = WrapAngle(Angle#,-0.5)
     If RightKey() Then Angle# = WrapAngle(Angle#,0.5)
   ; loop back to the do statement to keep the program running
  Loop
 




rtw1701

Doh,  that worked great.  Thanks again.