CompareSpritePixels
CollisionState = CompareSpritePixels(SpriteIndex1, SpriteIndex2, Accuracy#)
 
Parameters:

    SpriteIndex1 = The Source Sprite to check for collision against
    SpriteIndex2 = The Dest Sprite to check for collision against
    Accuracy# = The level of accuracy that should be used.
Returns:

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

      The CompareSpritePixels function detects if a pixel level collision occurs between a pair of sprites.

      The Accuracy parameter allows the user to fine tune the quality of the pixel level collisions. Generally speaking, the lower the quality the faster the comparison will be. On the flip side, the higher the quality the more accurate, but slower the comparison. It's up to you to pick the accuracy level that is appropriate for your images and the performance requirements of your game.


Example Accuracy Values

      0.25 = Reduce collision accuracy to 25%
      0.50 = Reduce collision accuracy to 50%
      0.75 = Reduce collision accuracy to 75%
      1.00 = Set collision accuracy to 100%
      2.00 = Set collision accuracy to 200%



FACTS:


      * Sprite Images should be prepared as FX images for best speed! See SpriteImage, PrepareFXImage, LoadFXImage etc etc

      * Pixels level collisions ignore the sprites transparent colour.




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 CompareSpritePixels



  
; Make a pair of images with circles on them
  For img=1 To 2
     CreateFXImage Img,100,100
     RenderToImage Img
     Cls RGB(0,0,0)
     For lp =0 To 25
        If img=1
           CircleC Rnd(100),Rnd(100),RndRange(2,10),1,RndRGB()
        Else
           LineC Rnd(100),Rnd(100),Rnd(100),Rnd(100),RndRGB()
        EndIf
     Next
  Next
  
; Restore Render to the Screen as the default.
  RenderToScreen
  
  
  
  
; Make 10 sprites
  For spr=1 To 10
     
   ; position the sprite randomly on the screen
     x=Rnd(GetScreenWidth())
     y=Rnd(GetScreenHeight())
     
   ; Create the sprite using image #1
     Spr=NewSprite(x,y,1)
     
   ; center the sprite
     CenterSpriteHandle spr
     
  Next
  
  
  
  TestSprite=NewSprite(0,0,2)
  
  
  
  Do
     Cls RGB(0,0,0)
     
   ; Check if the mouse  is over a sprite
     mx#=MouseX()
     my#=MouseY()
     
     PositionSprite TestSprite,mx#,my#
     
     
   ; start for/next loop to count through sprite indexes 1 to 10
     For spr=1 To 10
        
      ; Turn this sprite
        TurnSprite spr,0.25
        
      ; Check if our test triangle hits any pixels in this sprite
        If CompareSpritePixels(TestSprite,spr,1)=true
         ; Flash the sprite if there's a collison
           SpriteDrawMode Spr,2+4096
        Else
         ; Set it's draw mode to just rotated
           SpriteDrawMode Spr,2
        EndIf
     Next
     
     DrawAllSprites
     
     Sync
  Loop
  
  





 
Related Info: CircleHitSprite | EllipseHitSpritePixels | GetSpriteCollision | GetSpriteCollisionAccuracy | GetSpriteCollisionClass | GetSpriteCollisionMode | LineHitSpritePixels | PointHitSprite | PointHitSpritePixels | QuadHitSprite | QuadHitSpritePixels | RayHitSprite | RectHitSprite | RectHitSpritePixels | ShapeHitSprite | SpriteCollision | SpriteCollisionAccuracy | SpriteCollisionClass | SpriteCollisionMode | SpriteCollisionRadius | SpriteHit | SpritesOverlap | TriangleHitSpritePixels :
 


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