How To Detect Shape Collision

The purpose of this how is to detect shape collision when using SwinGame’s functionality.

Level

Intermediate

Details

In this how to, a shape is randomly move on the screen when it hit the rectangle surrounding it, it will stop using CircleLinesCollision.

On the right is a video demonstrate the program while down below is the code of the program.







Source Code Widget

  • program HowToDetectShapeCollision;
    uses SwinGame,sgTypes;
    
    procedure BouncingCircle(cPoint : Point2D ;c : Circle ;l : array of LineSegment);
    var
        random : Integer;
    begin
        random := Rnd(8 );
        if  CircleLinesCollision(c ,l )  = false   then
        begin
        case random  of
            0 : begin
                cPoint.x += 0;
                cPoint.y -= 1;
            end
            1 : begin
                cPoint.x += 0;
                cPoint.y += 1;
            end
            2 : begin
                cPoint.x -= 1;
                cPoint.y += 0;
            end
            3 : begin
                cPoint.x += 1;
                cPoint.y += 0;
            end
            4 : begin
                cPoint.x -= 1;
                cPoint.y -= 1;
            end
            5 : begin
                cPoint.x += 1;
                cPoint.y -= 1;
            end
            6 : begin
                cPoint.x -= 1;
                cPoint.y += 1;
            end
            7 : begin
                cPoint.x += 1;
                cPoint.y += 1;
            end
        
        end;
        end;
    end;procedure Main();
    var
        lineArray : array [0..3] of LineSegment;
        c : LongWord;
        c1 : Circle;
    begin
        OpenGraphicsWindow('Shape Collision Movement' ,800 ,600 );
        LoadDefaultColors();
        c := ColorGreen;
        c1 := CircleAt(400 ,300 ,15 );
        lineArray[0 ] := LineFrom(300 ,200 ,300 ,400 );
        lineArray[1 ] := LineFrom(500 ,200 ,500 ,400 );
        lineArray[2 ] := LineFrom(300 ,200 ,500 ,200 );
        lineArray[3 ] := LineFrom(300 ,400 ,500 ,400 );
        repeat
            ProcessEvents();
            ClearScreen(ColorWhite );
            FillCircle(c ,c1 );
            DrawLine(ColorGreen ,lineArray[0 ] );
            DrawLine(ColorGreen ,lineArray[1 ] );
            DrawLine(ColorGreen ,lineArray[2 ] );
            DrawLine(ColorGreen ,lineArray[3 ] );
            BouncingCircle(c1.center ,c1 ,lineArray );
            RefreshScreen();
        
        until WindowCloseRequested();
        ReleaseAllResources();
    end;
    begin
        Main();
    end.
  • #include 
    #include "SwinGame.h"
    void bouncing_circle(point2d * c_point,circle  c,line_segment  *l) 
    {
        int32_t  random;
        random = rnd(8 ) ;
        if ( circle_lines_collision(c ,l ) == false  )
        {
        switch (random )
        {
            case 0 : 
            {
                (c_point).x += 0 ;
                (c_point).y -= 1 ;
            }
            case 1 : 
            {
                (c_point).x += 0 ;
                (c_point).y += 1 ;
            }
            case 2 : 
            {
                (c_point).x -= 1 ;
                (c_point).y += 0 ;
            }
            case 3 : 
            {
                (c_point).x += 1 ;
                (c_point).y += 0 ;
            }
            case 4 : 
            {
                (c_point).x -= 1 ;
                (c_point).y -= 1 ;
            }
            case 5 : 
            {
                (c_point).x += 1 ;
                (c_point).y -= 1 ;
            }
            case 6 : 
            {
                (c_point).x -= 1 ;
                (c_point).y += 1 ;
            }
            case 7 : 
            {
                (c_point).x += 1 ;
                (c_point).y += 1 ;
            }
        
        }
        }
    }int main() 
    {
        line_segment line_array[3];
        uint32_t  c;
        circle  c1;
        open_graphics_window("Shape Collision Movement" ,800 ,600 );
        load_default_colors();
        c = color_green ;
        c1 = circle_at(400 ,300 ,15 ) ;
        line_array[0 ] = line_from(300 ,200 ,300 ,400 ) ;
        line_array[1 ] = line_from(500 ,200 ,500 ,400 ) ;
        line_array[2 ] = line_from(300 ,200 ,500 ,200 ) ;
        line_array[3 ] = line_from(300 ,400 ,500 ,400 ) ;
        do 
        {
            process_events();
            clear_screen(color_white );
            fill_circle(c ,c1 );
            draw_line(color_green ,line_array[0 ] );
            draw_line(color_green ,line_array[1 ] );
            draw_line(color_green ,line_array[2 ] );
            draw_line(color_green ,line_array[3 ] );
            bouncing_circle((c1).center ,c1 ,line_array );
            refresh_screen();
        }
        while ( ! (window_close_requested() ) );
        release_all_resources();
        return 0;
    }
    
  • Source Code Coming Soon

  • Source Code Coming Soon

  • Source Code Coming Soon


Possibilities