How To Collide A Sprite With A Triangle

This article will show you how to collide a sprite with a triangle.

Level

Advanced

Details

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:

  1. 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.
  2. 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.
  3. Create four triangles (triangle From) from those four variables.
  4. 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.
  5. Create the ball sprite(Create Sprite), and set its initial position(Sprite Set X, Sprite Set Y).
  6. 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.
  7. In the game loop draw and fill all the triangles (Fill triangle) with some color
  8. Draw (Draw Sprite) and update (Update Sprite) the sprite
  9. 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.
  10. 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.
  11. Refresh (Refresh Screen) the screen so all the pictures will be drawn on the game screen
  12. 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.

Source Code Widget

  • 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.
  • #include 
    #include "SwinGame.h"
    int main() 
    {
        triangle  right_tri;
        sprite  ball;
        triangle  bottom_tri;
        line_segment bottom_lines[n - 1];
        line_segment top_lines[n - 1];
        line_segment left_lines[n - 1];
        triangle  top_tri;
        triangle  left_tri;
        line_segment right_lines[n - 1];
        open_graphics_window("Bouncing Ball" ,800 ,600 );
        clear_screen(color_white );
        load_bitmap_named("ball" ,"ball_small.png" );
        left_tri = triangle_from(10 ,10 ,10 ,580 ,200 ,285 ) ;
        right_tri = triangle_from(790 ,10 ,790 ,580 ,600 ,285 ) ;
        top_tri = triangle_from(10 ,10 ,780 ,10 ,390 ,170 ) ;
        bottom_tri = triangle_from(10 ,590 ,780 ,590 ,390 ,430 ) ;
        left_lines = lines_from(left_tri ) ;
        right_lines = lines_from(right_tri ) ;
        top_lines = lines_from(top_tri ) ;
        bottom_lines = lines_from(bottom_tri ) ;
        ball = create_sprite(bitmap_named("ball" ) ) ;
        sprite_set_x(ball ,300 );
        sprite_set_y(ball ,300 );
        sprite_set_velocity(ball ,vector_to(1 ,-0.6 ) );
        do 
        {
            process_events();
            clear_screen(color_white );
            fill_triangle(color_red ,left_tri );
            fill_triangle(color_green ,right_tri );
            fill_triangle(color_yellow ,top_tri );
            fill_triangle(color_blue ,bottom_tri );
            draw_sprite(ball );
            update_sprite(ball );
            if ( circle_triangle_collision(sprite_circle(ball ) ,left_tri )  )
                collide_circle_lines(ball ,left_lines );
            if ( circle_triangle_collision(sprite_circle(ball ) ,right_tri )  )
                collide_circle_lines(ball ,right_lines );
            if ( circle_triangle_collision(sprite_circle(ball ) ,top_tri )  )
                collide_circle_lines(ball ,top_lines );
            if ( circle_triangle_collision(sprite_circle(ball ) ,bottom_tri )  )
                collide_circle_lines(ball ,bottom_lines );
            refresh_screen();
        }
        while ( ! (window_close_requested() ) );
        free_sprite(ball );
        release_all_resources();
        return 0;
    }
    
  • Source Code Coming Soon

  • Source Code Coming Soon

  • Source Code Coming Soon

If there is a useful hint you will find it in a box like this

Possibilities

Suggested projects will appear here.