program HowToCollideASpriteWithRectangle; uses SwinGame,sgTypes; procedure Main(); var topRect : Rectangle; leftRect : Rectangle; ball : Sprite; bottomRect : Rectangle; rightRect : Rectangle; begin OpenGraphicsWindow('Bouncing Ball' ,800 ,600 ); ClearScreen(ColorWhite ); LoadBitmapNamed('ball' ,'ball_small.png' ); leftRect := RectangleFrom(50 ,80 ,30 ,440 ); rightRect := RectangleFrom(720 ,80 ,30 ,440 ); topRect := RectangleFrom(80 ,50 ,640 ,30 ); bottomRect := RectangleFrom(80 ,520 ,640 ,30 ); ball := CreateSprite(BitmapNamed('ball' ) ); SpriteSetX(ball ,200 ); SpriteSetY(ball ,400 ); SpriteSetVelocity(ball ,VectorTo(1 ,-0.6 ) ); repeat ProcessEvents(); ClearScreen(ColorWhite ); FillRectangle(ColorBlue ,leftRect ); FillRectangle(ColorRed ,rightRect ); FillRectangle(ColorGreen ,topRect ); FillRectangle(ColorYellow ,bottomRect ); DrawSprite(ball ); UpdateSprite(ball ); if SpriteRectCollision(ball ,leftRect ) then CollideCircleRectangle(ball ,leftRect ); if SpriteRectCollision(ball ,rightRect ) then CollideCircleRectangle(ball ,rightRect ); if SpriteRectCollision(ball ,topRect ) then CollideCircleRectangle(ball ,topRect ); if SpriteRectCollision(ball ,bottomRect ) then CollideCircleRectangle(ball ,bottomRect ); RefreshScreen(); until WindowCloseRequested(); FreeSprite(ball ); ReleaseAllResources(); end; begin Main(); end.
How To Collide A Sprite With Rectangle
This article will show you how to collide a sprite with a rectangle.
Swingame has enough physics knowledge to determine the escape angle of a sprite if it hits a rectangle with a certain angle. To do this you only need to check if the rectangle and the sprite was in collision and if they were then you just have to call a function to make the sprite collide with the rectangle. To do this you need to follow these steps:
- Declare four rectangles that encompasses the whole screen so the ball will keep bouncing off of the rectangles it hits that rectangle and will not go off the screen.
- Declare four rectangle variables.
- Create four rectangles (Rectangle From) from those four variables.
- Create the ball sprite(Create Sprite), and set its initial position(Sprite Set X, Sprite Set Y).
- Set the velocity of the sprite using vector by calling the function Sprite Set Velocity. The speed will increase with the increased numbers passed in as parameters in the Vector To function.
- In the game loop draw and fill all the rectangles (Fill Rectangle) with some color
- Draw (Draw Sprite) and update (Update Sprite) the sprite
- Detect if there is a collision (Sprite Rect Collision) between the sprite and any of the rectangles
- If there is a collision then call the funtion Collide Circle Rectangle and pass the a rectangle and the sprite as parameters. This function will collide the sprite with that particular rectangle. Do this for all four rectangles.
- Refresh (Refresh Screen) the screen so all the pictures will be drawn on the game screen
- Repeat the whole process until the user closes the window(Window Close Requested)
The code below shows a small program that demonstrates all these steps in making a sprite colliding with a rectangle.
If there is a useful hint you will find it in a box like this
Suggested projects will appear here.