ImageHitSpritePixels
CollisionState = ImageHitSpritePixels(ImageIndex, ImageXpos, ImageYpos, SpriteIndex, Accuracy#)
 
Parameters:

    ImageIndex = The Image index you wish to compare against the sprite
    ImageXpos = The X coordinate of the image
    ImageYpos = The Y coordinate of the image
    SpriteIndex = The Sprite Index 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 ImageHitSpritePixels function performs a pixel level comparison between an image and a specific sprite. Since images have no position, we need to supply the function with the coordinate of the image. This will be the top left hand corner of the image. The function uses the images width and height to create a region to check if the two zones overlap. If the sprite is sharing the same space then it'll do a pixel level comparison. If there's an impact it'll return true(1), otherwise it'll return a false.


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:



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

      * Pixels level collisions ignore the sprites transparent colour, and perform best with FX formatted images.

      * Also see SpriteCollisionMode





Example:



 
Example Source: Download This Example
  sw=GetScreenWidth()
  sh=GetScreenHeight()
  
  Global CollisionMask=NewImage(sw,sh,2)
  
  // Draw some circle for some collision hot spots
  RenderToImage CollisionMask
  For lp=0 To 50
     Circle Rnd(sw),Rnd(sh),50,true
  Next
  
  // clear our a chunk where the player is going to be dropped
  CircleC sw/2,sh/2,100,true,RGB(0,0,0)
  
  
; Create player image.. In this demo the player is a blue circle
  PlayerImage=NewFXImage(64,64)
  RenderToImage PlayerImage
  CircleC 32,32,32,true,255
  RenderToScreen
  
; Create player sprite
  PlayerSpeed#=5
  PLayer=NewSprite(sw/2,sh/2,PLayerImage)
  SpriteDrawMode PLayer,2
  SpriteCollisionMode Player,6
  SpriteCollisionDebug Player,off
  
  
; Main loop
  SetFPS 60
  Do
   ; set the cursor
     SetCursor 0,0
     
   ; Draw the backdrop
     c1=RGB(20,230,140)
     c2=RGB(120,30,40)
     ShadeBox 0,0,sw,sh,c1,c2,c1,c2
     
   ; Draw the collision Mask image so we can see what we're hitting
     DrawImage CollisionMask,0,0,true
     
   ; handle moving the player
     MoveX#=(LeftKey()*-PLayerSpeed#) + (RightKey()*PlayerSpeed#)
     MoveY#=(UpKey()*-PLayerSpeed#) + (DownKey()*PlayerSpeed#)
     MOveMe(Player,MoveX#,MoveY#)
     
   ; draw the sprites
     DrawAllSprites
     
   ; refresh the screen and loop back to the DO statement.
     Sync
  Loop
  
  
  
  
  
  
Function MoveMe(ThisSprite,MoveX#,MoveY#)
  SpriteTint ThisSprite,$ffffff
  
  If Movex#=0 And Movey#=0 Then Exitfunction
  
  // Get it's current position
  BaseX#=GetSpriteX(ThisSprite)
  BaseY#=GetSpriteY(ThisSprite)
  x#=BaseX#+MoveX#
  y#=BaseY#+MoveY#
  PositionSprite ThisSprite,x#,y#
  
  
; Check if this position hits the collision mask
  If ImageHitSpritePixels(CollisionMask,0,0,ThisSprite,1)
     SpriteTint ThisSprite,RndRGB()
     
     // iof it did, try and resolve
     dist=GetDistance2D(0,0,movex#,movey#)
     If Dist>0
        nx#=moveX#/dist
        ny#=movey#/dist
        
        X#=BaseX#
        Y#=BaseY#
        For lp=1 To Dist
           PositionSprite ThisSprite,Basex#+(lp*nx#),Basey#+(lp*ny#)
           If ImageHitSpritePixels(CollisionMask,0,0,ThisSprite,1)=true
              x#=Basex#+((lp-1)*nx#)
              y#=Basey#+((lp-1)*ny#)
              ExitFor
           EndIf
        Next
     EndIf
     PositionSprite ThisSprite,x#,y#
  EndIf
  
EndFunction
  
  
  
  
  
 
Related Info: CircleHitSprite | EllipseHitSpritePixels | PointHitSprite | PointHitSpritePixels | QuadHitSprite | QuadHitSpritePixels | RayHitSprite | RectHitSprite | RectHitSpritePixels | ShapeHitSprite | SpriteCollisionMode | SpriteHit | SpritesOverlap | TriangleHitSpritePixels :
 


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