The SpritesInShape function will calculate what sprites fall inside a vector shape. This shape can be rotated/scaled etc, Any sprite that either impacts the shapes edges, or is inside the shape will be added at a list. Which is stored in passed integer array. The function returns the number of sprites (if any) that are inside this shape.
FACTS:
* SpritesInShape Expects the passed array to be big enough to hold the sprite list returned (if any)
* See CreateShape, SpriteCollision, QuadHitSprite
Mini Tutorial:
This example, creates some sprites from shapes. If then tests what sprites are within a custom vector shape.
; Dim an Array to hold the Sprites found in our shape later on. Dim SpritesFound(1000) ; ------------------------------------- ; Detecting Sprites inside a shape ; -------------------------------------- ; SetFPS 60 MakeBitmapFont 1,$ffffff ; 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 ; 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 image 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()) ; ======================================== ; Create the users COllision shape ; ======================================== CollisionShape=20 CreateConvexShape CollisionShape,80,4 CreateConvexShape CollisionShape+1,60,4 MergeShape Collisionshape+1,Collisionshape ScaleShape 3,0.5 MergeShape 3,Collisionshape CollisionShapeScale#=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,2 ; Set this sprites Collision mode ; Mode 0 = None Rotated Rectangles ; Mode 1 = Rotated Rectangles ; Mode 2 = Circle ; Mode 3 = Shape ; Mode 4 = Sliding (circle) 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 ; Rotate the collision shape CollisionShapeAngle#=WrapAngle(CollisionShapeAngle#,-1) RotateShape CollisionShape,CollisionShapeAngle#,CollisionSHapeScale# ; draw the collision shape around the mouses position DrawShape CollisionShape,mx#,my#,2 Numb=SpritesInShape(CollisionShape,mx#,my#,SpritesFound(),2) For lp=0 To Numb-1 SpriteDrawMode SpritesFound(lp),2+1024 Next ; Check if the left mouse button is pressed If LeftMouseButton() CollisionShapeScale#=ClipRange#(CollisionShapeScale#-0.25,0.5,10) EndIf ; Check if the Right mouse button is pressed If RightMouseButton() CollisionShapeScale#=ClipRange#(CollisionShapeScale#+0.25,0.5,10) EndIf ; 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 |
|