UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on July 30, 2005, 01:10:44 PM

Title: Circle To Sprite Intersection
Post by: kevin on July 30, 2005, 01:10:44 PM
And here's another new intersection feature.  this one lets you check if a sprite or sprites are within a circle. There's also a rect version.  So you can screen for objects without a certainly range/zone.


[pbcode]

 MakeBitmapFont 1,$ffffff



; Make a Green image
   Cls $00ff00
   GetImage 1,0,0,30,30
  preparefximage 1

; Create Two convex shapes,
   CreateConvexShape 2,50,15   
   CreateConvexShape 3,30,15

; Merge Them to create a 'ring'
   Mergeshape 3,2
   ShiftShape 2,50,50
   resizeshape 2,100,100
  CopyShape 2,1

; Create a second image
   CreateImage 2,100,100

; tell PB to redirect all drawing to this image
   RenderToImage 2
; clear it to the BLUE
   cls $ff
; draw the previously created Ring shape to the
   ink $223322
   DrawShape 2,0,0,2   
   imagemaskcolour 2,Rgb(0,0,255)

; Randomly Draw 10,000 pixels, but only inside the shape
#trace off
   lockbuffer  
 For lp =0 to 10000
    x#=rnd(GetImagewidth(2))
    y#=rnd(GetImagewidth(2))
    if pointhitshape(x#,y#,2,0,0)
   boxc x#,y#,x#+1,y#+1,1,rndrgb()
    endif  
 next   
   unlockbuffer
#trace on

;  prepare image #2 as FX iamge
   preparefximage 2

; Tell PB to direct all drawing back to the screen
   rendertoscreen

; Assign Shape #2, to Image #2   
   ImageShape 2,2


; Get the Width/height of screen   
   sw=getscreenwidth()
   sh=getscreenHeight()

 
    
; Create a bunch of randomly positioned sprites
   MaxSprites=25


   RayX1#=rnd(getScreenwidth())
   RayY1#=rnd(getScreenHeight())

   
; =====================================
; Start of the Main DO/LOOp
; =====================================

Do
 ; Clear the Screen to black
   Cls FlashColour
   ink $ffffff

 mx#=mousex()
 my#=mousey()


   If InitSprites=0   
 InitSprites=true   
 For lp=1 to MaxSprites
    ; CReate this sprite
    CreateSprite lp
    ; Assign Image #2 to this sprite
    SpriteIMage lp,2
    ; Set Sprites Draw Mode to Rotated
    SpriteDRawMode lp,2
   
     ; Enable Sprite Collision For this sprite
    SpriteCollision lp,true
     ; Enable this Sprites Collsion Class (the class/group it belong too)
    SpriteCollisionClass lp,3
 
    ; Set this sprites Collision mode to "SHAPE"
    SpriteCollisionMode lp,1
 
    SpriteFlashColour lp,rndrgb()
 ;Randomly Enable Sprite Collision Debug Mode      

;     SpriteCollisionDebug lp,true
    centerspritehandle lp

   ; Randomly Position this sprite on the screen
    PositionSpriteXYZ LP,Rnd(sw),rnd(sh),100   

    Scale#=0.50+(Rnd(150)/100)
    ScaleSprite lp,Scale#

    SpriteCollisionRadius lp,50*Scale#
 next
   endif
    


  ; Run through and Turn/Rotate all the sprites
 For lp=1 to MaxSprites
    turnsprite lp,1+(lp*0.12)
    spritedrawmode lp,2
 next


 Circle_Over_sprite mx#,my#,100


   ; Draw all the sprites
 DrawOrderedSprites   



; Display a fps
 text 0,0,fps()


 if timer()>InputDelay

 ; =================================
 ; When the F1 key is pressed, Turn all
 ; the sprites collisin Debug info on/off
 ; =================================
    If FunctionKeys(1)
   For lp=1 to MaxSprites
      spritecollisiondebug lp,1-getspritecollisiondebug(lp)
   next
   INputdelay=timer()+100
    endif   

 ; =================================
 ; When the F2 key is pressed, Toggle the
 ; sprites to Solid/Transparent mode
 ; =================================
    If FunctionKeys(2)    
   For lp=1 to MaxSprites
      spritetransparent lp,1-getspritetransparent(lp)
   next
   INputdelay=timer()+100
    endif   

    if Spacekey()
      InitSprites=false
      INputdelay=timer()+100
    endif
 endif

 
; Show the Screen
   Sync


; Loop back to the previous DO statement to keep the demo running
loop




Function Circle_Over_Sprite(x#,y#,radius#)
 Circle x#,y#,radius#,0

 ThisSprite=GetFirstSprite()
 repeat
    if CircleHitSprite(x#,y#,Radius#,ThisSprite)
   SpriteDrawmode ThisSprite,2+4096
    endif
    ThisSprite=GetNextSprite(ThisSprite)
 until ThisSprite=0
EndFunction



[/pbcode]


  Related Sprite Collision Links:


     * Manually Doing Pixel Perfect Sprite Collisions (Examples & Video) (https://www.underwaredesign.com/forums/index.php?topic=4432.0)