LineHitShape
State = LineHitShape(X1#, Y1#, X1#, Y2#, ShapeIndex, ShapeX#, ShapeY#, [IntersectFlag=0])
 
Parameters:

    X1# = The starting X coordinate of the test line
    Y1# = The starting Y coordinate of the test line
    X1# = The ending X coordinate of the test line
    Y2# = The ending Y coordinate of the test line
    ShapeIndex = The index of the shape you wish to test
    ShapeX# = The base X position of the test shape
    ShapeY# = The base Y position of the test shape
    [IntersectFlag=0] = Flag to select if should calc the intersection point ( 0=No, 1= Yes)
Returns:

    State = The hit state of the line (0 = outside of shape, 1 = intersects shape)
 

      The LineHitShape function checks if a line (ray) is inside, or intersects a vector shape. It will return a true (1) when the line intersects, or a false(0) when it does not.

      The IntersectFlag parameter is optional, it defaults to zero. This parameters selects if the LineHitShape function will calculate the point of impact or not. When it's zero it doesn't, when it's one it'll calculate it. The impact point is reutrned via GetIntersectX# and GetIntersectY# respectively.



FACTS:


      * When detecting the impact point of along the line/ray LineHitShape will find the nearest impact to the x1/y1 coordinate.



Mini Tutorial:


      This example checks if the mouse pointer ever hits a moving shape.

  
  
; limit the speed to 100 frames per second or less
  SetFPS 100
  
  
; Create a convert shape to check collisions against
  MyShape=NewConvexShape(50,7)
  
; Init some variables to hold our shapes position
  ShapeX#=100
  ShapeY#=100
  
; Randomly init the test lines end point
  
  x1#=Rnd(GetScreenWidth())
  y1#=Rnd(GetScreenHeight())
  
  
; Start a So/Loop
  Do
   ; Clear the sreen
     Cls RGB(0,0,0)
     
   ; Display a message
     Print "Testing Line To Shape Collision"
     
   ; Rotate our test shape
     RotateShape MyShape,Angle#,1
     
   ; Bump the rotation angle
     Angle#=WrapAngle(angle#,1)
     
   ; Move and clip the shapes position
     Shapex#=Mod(Shapex#+1,GetScreenWidth())
     
   ; Read the mouse pointers position
     x2#=MouseX()
     y2#=MouseY()
     
   ; If the right mouse button is press
   ; Then change the test lines END position
     If RightMouseButton()
        x1#=x2#
        y1#=y2#
     EndIf
     
   ; Draw the Test Line
     LineC x1#,y1#,x2#,y2#,RGB(100,100,100)
     
     
   ; Check if a line hits the shape
     If LineHitShape(x1#,y1#,x2#,y2#,MyShape,Shapex#,Shapey#,true)
        
        
      ; Set the pen's ink colour to yellow
        Ink RGB(255,255,0)
        
      ; DRaw the impact point
        Circle GetIntersectX#(0),GetIntersectY#(0),5,true
        
        
      ; display a message
        Print "Line Hit Shape"
        
      ; set the ink colour to RED
        Ink RGB(255,0,0)
     Else
        
      ; If the line didn't hit the shape, then
      ; Set then ink colour to White
        Ink RGB(255,255,255)
     EndIf
     
   ; Draw the filled shape
     DrawShape MyShape,ShapeX#,ShapeY#,2
     
     
   ; Display the screen
     Sync
   ; loop back to the do statement
  Loop
  
  
  
  



      This example outputs,






 
Related Info: BoxHitShape | CircleHitShape | PointHitShape | ShapeHitShape | SpriteCollisionMode :
 


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