Main Menu

Quad (poly) hit sprite

Started by kevin, August 02, 2005, 11:49:23 PM

Previous topic - Next topic

kevin

This picture shows the newest sprite collision function.  It lets you check if a sprite is inside/ or touching a quad polygon.  


 

PlayBASIC Code: [Select]
; ---------------------------------------------------------------------
; Detecting Quad (poly) Hits a Sprite
; ---------------------------------------------------------------------
; SetFPS 60
MakeBitmapFont 1,$ffffff

; Arrays are Rotating Verts
Dim VertexX#(4)
Dim VertexY#(4)


; 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 current image
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())


MyScale#=1


; =====================================
; 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,3

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


;Rect_Over_Sprite(mx#,my#,64,30)
Quad_Over_Sprite(mx#,my#,64,30,Myangle#,MyScale#)
Myangle#=wrapangle(Myangle#,1)

ScaleSpeed#=0
if leftmousebutton() then ScaleSpeed#=-0.25
if Rightmousebutton() then ScaleSpeed#=0.25
MyScale#=ClipRange#(MyScale#+ScaleSpeed#,0.25,50)


; Draw all the sprites
DrawOrderedSprites


Login required to view complete source code




 Related Sprite Collision Links:


    * Manually Doing Pixel Perfect Sprite Collisions (Examples & Video)