News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

Sprite Collision (Pixel Perfect) using Shapes

Started by kevin, June 08, 2005, 09:43:24 AM

Previous topic - Next topic

kevin

This example shows how to get pixel perfect collisions using attached vector shapes

requires PlayBASIC V1.075 or above


PlayBASIC Code: [Select]
; ---------------------------------------------------------------------
; Getting Pixel Perfect Sprite Collision with Vector Shapes
; ---------------------------------------------------------------------

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

; Create Two convex shapes,
CreateConvexShape 2,50,5
CreateConvexShape 3,40,5

; Merge Them to create a 'ring'
Mergeshape 3,2
ShiftShape 2,50,50

; 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
DrawShape 2,0,0,2

; Randomly Draw 10,000 pixels, but only inside the shape
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

; 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 Sprite #1 (the sprite the user controls in this demo)
lp=1
CreateSprite lp
SpriteIMage lp,1
SpriteCollision lp,true
SpriteCollisionClass lp,3
SpriteCollisionMode lp,0
PositionSprite LP,Rnd(sw),rnd(sh)
SpriteCollisionDebug lp,true


; Create a bunch of randomly positioned sprites
MaxSprites=10
For lp=2 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"
; When in Shpe mode, PB uses the current assigned shape of it's image
SpriteCollisionMode lp,3

; Randomly Enable Sprite Collision Debug Mode
SpriteCollisionDebug lp,rnd(true)

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


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

Do
; Clear the Screen to black
Cls 0

; Display a message
Print "Sprite Collision - Shape Mode"
Print "Touch the WHITE region in the sprite"

; Run through and Turn/Rotate all the sprites
For lp=2 to MaxSprites
turnsprite lp,1
next

; Position The users Test Sprite #1, at the mouses position
PositionSprite 1,mousex(),mousey()

flashcolour=0

; Check If the Users Sprite Has Hit any of the rotating sprites
NextSprite=GetFirstSprite()
repeat
ThisSprite=SpriteHit(1,NextSprite,%0010)
if ThisSprite>0
NextSprite=GetNextSprite(ThisSprite)
FlashColour=$770000
endif
until ThisSprite=0

; If there was an impact, Flash the Screen RED
if flashcolour
cls Flashcolour
else
print "No Impacts"
endif

; Draw all the sprites
DrawAllSprites

; Show the Screen
Sync

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






 Related Sprite Collision Articles:

 * Walking on Sprite Collision Map
 * Sliding Collision Around A Track
 * Sprite Collision With An Image Mask
 * Circle To Sprite Intersection  
 * Platformer Pixel Collisions Via Ray Casting
  * Manually Doing Pixel Perfect Sprite Collisions (Examples & Video)