program HowToCollideASpriteWithATriangle; uses SwinGame,sgTypes; procedure Main(); var rightTri : Triangle; ball : Sprite; bottomTri : Triangle; bottomLines : array [0..n - 1] of LineSegment; topLines : array [0..n - 1] of LineSegment; leftLines : array [0..n - 1] of LineSegment; topTri : Triangle; leftTri : Triangle; rightLines : array [0..n - 1] of LineSegment; begin OpenGraphicsWindow('Bouncing Ball' ,800 ,600 ); ClearScreen(ColorWhite ); LoadBitmapNamed('ball' ,'ball_small.png' ); leftTri := TriangleFrom(10 ,10 ,10 ,580 ,200 ,285 ); rightTri := TriangleFrom(790 ,10 ,790 ,580 ,600 ,285 ); topTri := TriangleFrom(10 ,10 ,780 ,10 ,390 ,170 ); bottomTri := TriangleFrom(10 ,590 ,780 ,590 ,390 ,430 ); leftLines := LinesFrom(leftTri ); rightLines := LinesFrom(rightTri ); topLines := LinesFrom(topTri ); bottomLines := LinesFrom(bottomTri ); ball := CreateSprite(BitmapNamed('ball' ) ); SpriteSetX(ball ,300 ); SpriteSetY(ball ,300 ); SpriteSetVelocity(ball ,VectorTo(1 ,-0.6 ) ); repeat ProcessEvents(); ClearScreen(ColorWhite ); FillTriangle(ColorRed ,leftTri ); FillTriangle(ColorGreen ,rightTri ); FillTriangle(ColorYellow ,topTri ); FillTriangle(ColorBlue ,bottomTri ); DrawSprite(ball ); UpdateSprite(ball ); if CircleTriangleCollision(SpriteCircle(ball ) ,leftTri ) then CollideCircleLines(ball ,leftLines ); if CircleTriangleCollision(SpriteCircle(ball ) ,rightTri ) then CollideCircleLines(ball ,rightLines ); if CircleTriangleCollision(SpriteCircle(ball ) ,topTri ) then CollideCircleLines(ball ,topLines ); if CircleTriangleCollision(SpriteCircle(ball ) ,bottomTri ) then CollideCircleLines(ball ,bottomLines ); RefreshScreen(); until WindowCloseRequested(); FreeSprite(ball ); ReleaseAllResources(); end; begin Main(); end.
How To Collide A Sprite With A Triangle
This article will show you how to collide a sprite with a triangle.
SwinGame has enough physics knowledge to determine the escape angle of a sprite if it hits any side of a triangle with a certain angle. To do this you only need to check if the triangle and the sprite was in collision, and if they were then you have to call functions to make the sprite collide with the lines of the triangles. To do this you need to follow these steps:
- Declare four triangles that encompasses the whole screen so the ball will keep bouncing off of the triangles if it hits the triangles and will not go off the screen. Therefore, declare four triangle variables.
- We want to make the ball bouncing off any side of the triangle, therefore we need to make all the edges of the triangle a line. To do this first declare four LinesArray variables.
- Create four triangles (triangle From) from those four variables.
- Now create all the lines of the triangles a LinesArray. To do this just pass the triangles as parameters inside the function Lines From. Assign these lines into the previously created LinesArray 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 triangles (Fill triangle) with some color
- Draw (Draw Sprite) and update (Update Sprite) the sprite
- Detect if there is a collision (Circle Triangle Collision) between the sprite and any of the triangles. Make the sprite a circle by calling the function Sprite Circle and pass the sprite as parameter.
- If there is a collision then call the funtion Collide Circle Lines and pass the LinesArray and the sprite as parameters. This function will collide the sprite with all the lines of that particular triangle. Do this for all four triangles.
- 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 triangle.
If there is a useful hint you will find it in a box like this
Suggested projects will appear here.