The ImageShape command assigns a shape that will be used to represent this image during 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 dimensions.
Mini Tutorial:
This example builds a vector shape, an image and a collection of sprites. It then tests for collisions between the usings using sprites shape collision mode. Sprites set to shape collision used the shape attached to the image that the sprite is currently using.
; --------------------------------------------------------------------- ; Getting Pixel Perfect Sprite Collision with Vector Shapes ; --------------------------------------------------------------------- ; ; This example is pretty complex since in order to be self contained ; it has to manually create the shapes & images it requires. In the ; real world example you'd make you shapes with PlayShape editor ; ; --------------------------------------------------------------------- ; Set PB to run the program at or bellow 75 frames per second SetFPS 75 MakeBitmapFont 1,$ffffff ; Call a function to create a donut styled shape RingShape=CreateRingShape(50) ; Create image MyIMage=NewFXImage(100,100) ; Assign Ring Shape to MyImage ImageShape MyImage,RingShape ; tell PB to redirect all drawing to this image RenderToImage MyImage ; clear it to BLUE Cls $ff ImageMaskColour MyImage,RGB(0,0,255) ; draw the previously created Ring shape to the Ink $223322 DrawShape RingShape,0,0,2 ; Randomly Draw 10,000 pixels, but only inside the shape #Trace off LockBuffer For lp =0 To 10000 x#=Rnd(GetImageWidth(MyImage)) y#=Rnd(GetImageWidth(MyImage)) If PointHitShape(x#,y#,RingShape,0,0) BoxC x#,y#,x#+1,y#+1,1,RndRGB() EndIf Next UnLockBuffer #Trace on ; Tell PB to direct all drawing back to the screen RenderToScreen ; Get the Width/height of screen sw=GetScreenWidth() sh=GetScreenHeight() ; Create a bunch of randomly positioned sprites MaxSprites=25 Gosub Create_All_Sprites ; ===================================== ; Start of the Main DO/LOOp ; ===================================== Do ; Clear the Screen to black Cls RGB(0,0,0) Ink $ffffff ; Update Sprites via moving through the sprite list Sprite=GetFirstSprite() While Sprite>0 ; Use the Collision classes to work out which sprites are ;which Select GetSpriteCollisionClass(Sprite) ; This sprite is the users collision class Case 1 ; users control sprite TurnSprite Sprite,0.5 PositionSprite Sprite,MouseX(),MouseY() ; some other sprite Default ; any other sprite TurnSprite Sprite,1+(Count*0.12) SpriteDrawMode Sprite,2 EndSelect Sprite=GetNextSprite(Sprite) EndWhile HitCount=0 ; Check If the Users Sprite Hit any of the other sprites Sprite=GetFirstSprite() While Sprite>0 ; chekc if usersprite hit any sprite ofr collision class #2 (%0010) Sprite=SpriteHit(UserSprite,Sprite,%0010) If Sprite>0 Inc HitCount SpriteDrawMode Sprite,2+1024 Sprite=GetNextSprite(Sprite) EndIf EndWhile ; Scale the users sprite hit they press the mouse button If MouseButton() S#=GetSpriteScaleX(UserSprite)+0.2 If S#>10 Then S#=0.1 ScaleSprite UserSprite,s# EndIf ; Draw all the sprites DrawOrderedSprites ; Display info about this demo Print "Sprite Collision - Shape Mode" Print "Touch the WHITE region in the sprite" Print "Impacts:"+Str$(hitcount) Print "Click mouse button to scale sprite." ; Show the Screen Sync ; Loop back to the previous DO statement to keep the demo running Loop Create_All_Sprites: ; CReate the Users Test control sprite UserSprite=NewSprite(Rnd(sw),Rnd(sh),MyIMage) SpriteDrawMode UserSprite,2 SpriteCollision UserSprite,true SpriteCollisionClass UserSprite,1 SpriteCollisionMode UserSprite,3 ; SHape Collision SpriteCollisionDebug UserSprite,on RotateSprite UserSprite,45 CenterSpriteHandle UserSprite PositionSpriteZ UserSprite,10 ; Create a bunch of sprite for us to test collisions against For lp=2 To MaxSprites Sprite=NewSprite(Rnd(sw),Rnd(sh),MyIMage) ; Set Sprites Draw Mode to Rotated SpriteDrawMode Sprite,2 ; Enable Sprite Collision For this sprite SpriteCollision Sprite,true ; Enable this Sprites Collision Class (the class/group it belong too) SpriteCollisionClass Sprite,2 ; Set this sprites Collision mode to "SHAPE" ; When in Shpe mode, PB uses the current assigned shape of it's image SpriteCollisionMode Sprite,3 ; SpriteCollisionRadius Sprite,25 SpriteFlashColour Sprite,RndRGB() ;Randomly Enable Sprite Collision Debug Mode ; SpriteCollisionDebug lp,true CenterSpriteHandle Sprite ; Randomly Position this sprite on the screen PositionSpriteXYZ Sprite,Rnd(sw),Rnd(sh),100 Next Return Function CreateRingShape(Size) ; Create two convex shapes, RingShape=NewConvexShape(Size,15) TempShape=NewConvexShape(Size*0.60,15) ; Merge The two outlines to range donut (ring) shape. MergeShape TempShape,RingShape ; Delete the temp shape it's no longer needed DeleteShape TempShape ; Shift the shapes center over 50 ShiftShape RingShape,50,50 ; ResizeShape RingShape,100,100 RefreshShape RingShape EndFunction RingShape |
|