The GetImageShape function returns what shape (if any) has been attached to this image for sprite collision tests.
FACTS:
* The assigned shape is only considered when this image is used as a sprite. If the sprite has it's collision mode set to 'shape' (See SpriteCollisionMode), then the sprite collision engine will use this images attached shape, rather than the images size or pixel data directly.
Mini Tutorial:
This example builds a vector shape, an image and a collection of sprites. Then tests for collisions between them.
; --------------------------------------------------------------------- ; Getting Pixel Perfect Sprite Collision with Vector Shapes ; --------------------------------------------------------------------- SetFPS 60 MakeBitmapFont 1,$ffffff ; Make a Green image Cls RGB(0,255,0) 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 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 Sprite #1 (the sprite the user controls in this demo) lp=1 CreateSprite lp SpriteImage lp,2 SpriteDrawMode lp,2 SpriteCollision lp,true SpriteCollisionClass lp,3 SpriteCollisionMode lp,3 SpriteCollisionRadius lp,70 PositionSprite LP,Rnd(sw),Rnd(sh) SpriteCollisionDebug lp,on RotateSprite lp,45 CenterSpriteHandle lp PositionSpriteZ lp,10 ; Create a bunch of randomly positioned sprites MaxSprites=25 ; ===================================== ; Start of the Main DO/LOOp ; ===================================== Do ; Clear the Screen to black Cls FlashColour Ink $ffffff If InitSprites=0 InitSprites=true 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 Collision 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 SpriteCollisionRadius lp,25 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 Next EndIf ; Display a message Print "Sprite Collision - Shape Mode" Print "Touch the WHITE region in the sprite" TurnSprite 1,0.5 ; Run through and Turn/Rotate all the sprites For lp=2 To MaxSprites TurnSprite lp,1+(lp*0.12) SpriteDrawMode lp,2 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 T1=Timer() NextSprite=GetFirstSprite() HitCount=0 Repeat ThisSprite=SpriteHit(1,NextSprite,%0010) If ThisSprite>0 Inc HitCount SpriteDrawMode ThisSprite,2+1024 NextSprite=GetNextSprite(ThisSprite) FlashColour=$375070 EndIf Until ThisSprite=0 ; If there was an impact, Flash the Screen RED If flashcolour Print "Impacts:"+Str$(hitcount) Else Print "No Impacts" EndIf T2=Timer() t=t2-t1 Inc frames at#=at#+t Print "Time:"+Str$(at#/frames) If Timer()>InputDelay If FunctionKeys(1) For lp=2 To MaxSprites SpriteCollisionDebug lp,1-GetSpriteCollisionDebug(lp) Next INputdelay=Timer()+100 EndIf If FunctionKeys(2) For lp=2 To MaxSprites SpriteTransparent lp,1-GetSpriteTransparent(lp) Next INputdelay=Timer()+100 EndIf If SpaceKey() InitSprites=false INputdelay=Timer()+100 EndIf EndIf If MouseButton() S#=GetSpriteScaleX(1)+0.2 If S#>10 Then S#=0.1 ScaleSprite 1,s# EndIf ; Draw all the sprites DrawOrderedSprites ; Show the Screen Sync ; Loop back to the previous DO statement to keep the demo running Loop |
|