The SpriteHitWorld checks the sprites collision zone against the it's associated collision world.
FACTS:
* SpriteHitWorld uses the sprites associated collision world. See SpriteCollisionWorld to assign a collision world to a sprite.
* Also see SpriteCollision, SpriteHit
Mini Tutorial:
This tutorial is built of 4 parts.
Part 1 - Builds a collision world.
Part 2 - Create a simple blue image to use for our test sprite
part 3 - Creates the test sprite and sets it's collision mode ups
The Main loop then positions and rotates the test sprite at the mouse pointers poisiton. It then check if it has collided with the world. if an impact occurs, the spriets draw mode is change to flash the sprite to bright blue colour.
; ============== ; Part #1 - create a world to collide with ; ============== ; Get the Screen size and use it as the world size WorldWidth=GetScreenWidth() WorldHeight=GetScreenHeight() ; create a Camera CreateCamera 1 ; Create world CreateWorld 2 CaptureToWorld 2 ; draw a series of boarder line for this world Line 0,0,worldwidth,0 Line worldwidth,0,worldwidth,worldheight Line worldwidth,worldheight,0,worldheight Line 0,worldheight,0,0 ; draw a series of polygon shaped obejct into the world For lp=1 To 10 xpos#=50+Rnd(worldwidth-100) zpos#=50+Rnd(worldheight-100) size=RndRange(30,100) angle=Rnd(359) Make_Convex(RndRange(3,20),xpos#,zpos#,Size,angle) Next lp ; Partition The world up into 32 by 32 cells PartitionWorld 2,32 ; Tell PB to return to Immediate drawing mode DrawGFXImmediate ; ============== ; Part #2 - Create a Blue Image ; ============== ; Make a blue image Cls RGB(0,0,255) GetImage 1,0,0,64,64 PrepareFXImage 1 ; ============== ; Part #3 - Create our test sprite ; ============== ; Create a sprite for to use for Collision against the world CreateSprite 1 SpriteImage 1,1 SpriteDrawMode 1,2 ; Set Sprite Collision mode to rotated SpriteCollisionMode 1,1 ; Set the Sprite to use World #2 for it's collision SpriteCollisionWorld 1,2 ; start of programs main DO/Loop Do ; capture to scene and grab the world info CaptureToScene ClsScene CaptureDepth 100 CameraGrabWorld 1,2 ; Get the mouse position mx#=MouseX() my#=MouseY() ; position sprite at the mouse position PositionSprite 1,mx#,my# ; Turn the sprite TurnSprite 1,1 ; check if the sprite hits the world If SpriteHitWorld(1) ; If the sprite hits the world, ; then we'll change ti's drawmode ; to flash it SpriteDrawMode 1,2+4096 Else SpriteDrawMode 1,2 EndIf ; DRaw the sprite DrawSprite 1 ; draw the camera DrawCamera 1 ; show the fps rate and continue this loop Text 0,0,FPS() Sync Loop ; This function creates a convex polygon shape Function Make_Convex(edges,xpos#,ypos#,Size,angle) sa#=360.0/edges c=RndRGB() For lp=0 To edges-1 a#=angle+(lp*sa#) x1#=xpos#+CosRadius(a#,size) y1#=ypos#+SinRadius(a#,size) If lp<(edges-1) a#=angle+((lp+1)*sa#) Else a#=angle EndIf x2#=xpos#+CosRadius(a#,size) y2#=ypos#+SinRadius(a#,size) Line x2#,y2#,x1#,y1# Next lp EndFunction i |
|