SpriteCollisionClass
SpriteCollisionClass SpriteNumber, CollisionClass
 
Parameters:

    SpriteNumber = The Index of the sprite you wish to set the collision class in
    CollisionClass = The Collison class value
Returns: NONE
 

      SpriteCollisionClass sets the collision class of a sprite. Collision Classes are used to classify sprites as belonging to a particular group. This allows us to not only check for sprite collisions between all of the sprites if we want, but we can also selectively define the group of sprites we're interesting in. This ensures collision checking functions like HitSprite are as fast an accurate as possible.

      PlayBasic supports 32 different Collision Classes, each collision class is represented by a set bit in the collision class value. You can combine classes by combining the bit combinations. This will you to be somewhat familiar with binary and bit manipulations.

      For example, lets say we were creating a scrolling shoot-em-up game. In our game we're going to have a collection of different sprites. Including the player sprite, the player bullets, the Aliens and the Alien bullet sprites.

      In other languages, if we checked the player for a sprite collision, it will not only check the player against the Alien sprites (like we want), but it will also check the player against every other created sprite also, including the players sprite, the players bullets, as well as the alien bullets.


      This creates handful of problems for us to deal with manually. Most notable is that if the player sprite can return a positive collision when contact is made between player and their own bullets, then we'll be getting lots of annoying false positive results. Thus we can't rely upon the collision to only return collisions between the player and the Aliens.


Enter collision classes.

      In PlayBasic we can avoid this situation completely, by giving give each group of sprites a different collision class. So when we need to check for collision(s), we can scan for impacts between the sprite were interested in, and any sprite of a given class. So if the Player Sprite was Class 1 (bit 0 - %0001 in binary), the player bullets were class 2 (bit 1 - %0010 in binary), the Aliens class 4 (bit 2 - %0100 in binary) and the Alien bullets were class 8 (bit 3 - %1000 in binary). We could check for sprite collisions on the Aliens by check for collision class 4, or we could even check for collisions between a combination of the Aliens and Alien Bullets by combining the collision class bits of 4 (the Aliens) and 8 (the alien bullets). Which would be 12 (or %1100 in binary)




FACTS:


      * Sprite Collision Class defaults to 1, (Bit 1 set %00000000000000000000000000000001 in binary).

      * Sprite collision classes are limited to 32. One bit per each different collision class




Mini Tutorial:


      This example is in 3 parts, part 1 creates two coloured images. Part 2 then creates both out test sprite (sprite #1) and a group of randomly position sprites to check collisions again. Part 3 is main loop, where it check for any collisions with sprite #1.


  
  
; ========================
; Part 1 - Create an image
; ========================
  
  Cls RGB(0,0,255)
  GetImage 1,0,0,32,32
  
  Cls RGB(0,255,00)
  GetImage 2,0,0,32,32
  
; =============================
; Part 2- Create some sprites
; =============================
  
; Create Sprite 1, and assign it image 1
  CreateSprite 1
  SpriteImage 1,1
  CenterSpriteHandle 1
; Enable Collision for Sprite #1
  SpriteCollision 1,on
; Set Sprite #1 to collision Class %0001
  SpriteCollisionClass 1,%0001
  
; Create a bunch fo sprites to check collision against
  For Sprites=2 To 50
     CreateSprite Sprites
     SpriteImage Sprites,2
     x=Rnd(GetScreenWidth()-32)
     y=Rnd(GetScreenHeight()-32)
     PositionSprite sprites,x,y
     
   ; Enable Collision for this sprite
     SpriteCollision Sprites,on
     
   ; Set sprite to Collision Class %0010
     SpriteCollisionClass Sprites,%0010
  Next
  
  
; =============================
; Part 3- The Main Loop
; =============================
  
; Start a DO/Loop
  Do
   ; Clear the screen
     Cls RGB(0,0,0)
   ; Display a message
     Print "Checking for Collisions"
     
   ; Position the Sprite 1 at the mouses position
     PositionSprite 1,MouseX(),MouseY()
     
   ; Check if sprite #1 hit another sprite
   ; of this sprite class
     ThisSprite=SpriteHit(1,GetFirstSprite(),%0010)
     
   ; If there was impact, then we loop through and
   ; find any other sprites we might have hit also
     While ThisSprite>0
        Print "Hit Sprite:"+Str$(ThisSprite)
        
      ; Check if this sprite hit another sprite ?
     NextSprite=GetNextSprite(ThisSprite)
     ThisSprite=SpriteHit(1,NextSprite,%0010)
  EndWhile
  
; Draw All of the sprites
  DrawAllSprites
  
; Draw the screen
  Sync
  
; Loop back to the DO statement
  Loop
  
  




This example would output.

  
  no Text Ouput
  

 
Related Info: ClosestSpriteToPoint | GetSpriteCollisionClass | SpriteCollision | SpriteHit | SpritesOverlap :
 


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