RayHitSprite
CollisionFlag = RayHitSprite(RayX1, RayY1, RayX2, RayY2, SpriteIndex)
 
Parameters:

    RayX1 = The Rays Starting X coordinate
    RayY1 = The Rays Starting Y coordinate
    RayX2 = The Rays Ending X coordinate
    RayY2 = The Rays Ending Y coordinate
    SpriteIndex = The Sprite to check for intersection against
Returns:

    CollisionFlag = The result of the collision query (0= No collision ,1 = Collision)
 

      The RayHitSprite function returns if a collision occurs between a ray and a sprite. When testing the sprite the RayHitSprite command uses the sprites current collision mode as the hard region of the sprite.

      If a collision occurs, the closest impact point on the sprite (along the ray) is returned as well as the normal at the point of intersection. These values can be access via GetIntersectX# / GetIntersectY# & GetNormalX# / GetNormalY# functions.




FACTS:


      * RayHitSprite uses the sprites collision mode as the hard region of the sprite.

      * Point of intersection is returned in GetIntersectX# / GetIntersectY#

      * The Normal at the Point of Intersection is returned in GetNormalX# / GetNormalY#

      * See SpriteCollisionMode about collision modes.




Mini Tutorial:


      This example creates a random image, creates 10 randomly positioned sprites on the screen using this image. The main loop rotates the sprites while scans them for collisions using RayHitSprite



  
  
; Make a Blue image, with circle on it
  Cls RGB(0,0,255)
  For lp =0 To 100
     CircleC Rnd(100),Rnd(100),Rnd(5),1,RndRGB()
  Next
  GetImage 1,0,0,100,100
  PrepareFXImage 1
  
; Make 10 sprites
  For spr=1 To 10
     
   ; Create the sprite
     CreateSprite Spr
     
   ; psition the spriotye randomly on the screen
     x=Rnd(GetScreenWidth())
     y=Rnd(GetScreenHeight())
     PositionSprite Spr,x,y
     
   ; assign this sprite Image #1
     SpriteImage Spr,1
     
     CenterSpriteHandle spr
     SpriteCollision spr,on
     
   ; Set sprite collision to Rotated rectangles
     SpriteCollisionMode Spr,1
  Next
  
  
  
; Init the Rays Starting Point
  RayX1=Rnd(GetScreenWidth())
  RayY1=Rnd(GetScreenHeight())
  
  
  
  
  Do
     Cls RGB(0,0,0)
     
   ; Set the Rays End point to the mouses current position
     RayX2=MouseX()
     RayY2=MouseY()
     
     
   ; Create  Variable to hold the closest Sprite hit
   ;index (if nay)
     
     ClosestSprite=0
     
   ; start for/next loop from 1 to 10
     For spr=1 To 10
        
      ; Turn this sprite
        TurnSprite spr,spr*0.33
        
      ; Set it's draw mode to just rotated
        SpriteDrawMode Spr,2
        
      ; Check if a point(the mouse) is over this sprite
        If RayHitSprite(RayX1,RayY1,rayX2,rayY2,Spr)=true
           
         ; Since our ray hit this sprite, we'll trim it's END
         ; point back to this intersection point.
           
         ; So logically after testing our newly resized
         ; against all the sprites, we'll end up with the
         ; nearest point of impact along this ray.
           
           RayX2=GetIntersectX#(0)
           RayY2=GetIntersectY#(0)
           
           ClosestSprite=Spr
           
        EndIf
        
     Next
     
     
     If CLosestSprite>0
      ; change the draw mode of the nearest sprite only
        SpriteDrawMode ClosestSprite,2+4096
     EndIf
     
     
   ; Draw the ray to the screen
     Line RayX1,RayY1,RayX2,RayY2
     
     
     DrawAllSprites
     
   ; If the mouse Button is pressed, then we'll
   ; randomly reposition our rays origin point
     If MouseButton()
      ; Init the Rays Starting Point
        RayX1=Rnd(GetScreenWidth())
        RayY1=Rnd(GetScreenHeight())
     EndIf
     
     
     Sync
  Loop
  
  
  





 
Related Info: CircleHitSprite | CompareSpritePixels | EllipseHitSpritePixels | QuadHitSprite | QuadHitSpritePixels | RectHitSprite | RectHitSpritePixels | ShapeHitSprite | ShapeHitSpritePixels | SpriteCollisionAccuracy | SpriteCollisionMode | SpriteHit | SpritesOverlap | TriangleHitSpritePixels :
 


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