How To Collide Sprite With Lines

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

Level

Advanced

Details

Swingame has enough physics knowledge to determine the escape angle of a sprite if it hits a line with a certain angle. To do this you only need to check if the line 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 line. To do this you need to follow these steps:

  1. Declare four lines that encompasses the whole screen so the ball will keep bouncing off of the lines it hits and will not go off the screen.
  2. Declare four lineSegment variables.
  3. Create four lines (Line From) from those four variables.
  4. Create the ball sprite(Create Sprite), and set its initial position(Sprite Set X, Sprite Set Y).
  5. 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.
  6. In the game loop draw all the lines
  7. Draw (Draw Sprite) and update (Update Sprite) the sprite
  8. Detect if there is a collision (Circle Line Collision) between the sprite and the line
  9. If there is a collision then call the funtion Collide Circle Line and pass the line and the sprite as parameters. This function will collide the line with the sprite
  10. Refresh (Refresh Screen) the screen so all the pictures will be drawn on the game screen
  11. 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 line.

Source Code Widget

  • program HowToCollideSpriteWithLines;
    uses SwinGame,sgTypes;
    
    procedure Main();
    var
        leftLine : LineSegment;
        ball : Sprite;
        topLine : LineSegment;
        bottomLine : LineSegment;
        rightLine : LineSegment;
    begin
        OpenGraphicsWindow('Bouncing Ball' ,800 ,600 );
        ClearScreen(ColorWhite );
        LoadBitmapNamed('ball' ,'ball_small.png' );
        leftLine := LineFrom(10 ,10 ,10 ,590 );
        rightLine := LineFrom(790 ,10 ,790 ,590 );
        topLine := LineFrom(10 ,10 ,790 ,10 );
        bottomLine := LineFrom(10 ,590 ,790 ,590 );
        ball := CreateSprite(BitmapNamed('ball' )  );
        SpriteSetX(ball ,100 );
        SpriteSetY(ball ,500 );
        SpriteSetVelocity(ball ,VectorTo(1 ,-0.6 )  );
        repeat
            ProcessEvents();
            ClearScreen(ColorWhite );
            DrawLine(ColorRed ,leftLine );
            DrawLine(ColorRed ,rightLine );
            DrawLine(ColorRed ,topLine );
            DrawLine(ColorRed ,bottomLine );
            DrawSprite(ball );
            UpdateSprite(ball );
            if  CircleLineCollision(ball ,leftLine )    then
                CollideCircleLine(ball ,leftLine );
            if  CircleLineCollision(ball ,rightLine )    then
                CollideCircleLine(ball ,rightLine );
            if  CircleLineCollision(ball ,topLine )    then
                CollideCircleLine(ball ,topLine );
            if  CircleLineCollision(ball ,bottomLine )    then
                CollideCircleLine(ball ,bottomLine );
            RefreshScreen();
        
        until WindowCloseRequested();
        FreeSprite(ball );
        ReleaseAllResources();
    end;
    begin
        Main();
    end.
    Download Now"
  • #include 
    #include "SwinGame.h"
    int main() 
    {
        line_segment  left_line;
        sprite  ball;
        line_segment  top_line;
        line_segment  bottom_line;
        line_segment  right_line;
        open_graphics_window("Bouncing Ball" ,800 ,600 );
        clear_screen(color_white );
        load_bitmap_named("ball" ,"ball_small.png" );
        left_line = line_from(10 ,10 ,10 ,590 ) ;
        right_line = line_from(790 ,10 ,790 ,590 ) ;
        top_line = line_from(10 ,10 ,790 ,10 ) ;
        bottom_line = line_from(10 ,590 ,790 ,590 ) ;
        ball = create_sprite(bitmap_named("ball" ) ) ;
        sprite_set_x(ball ,100 );
        sprite_set_y(ball ,500 );
        sprite_set_velocity(ball ,vector_to(1 ,-0.6 ) );
        do 
        {
            process_events();
            clear_screen(color_white );
            draw_line(color_red ,left_line );
            draw_line(color_red ,right_line );
            draw_line(color_red ,top_line );
            draw_line(color_red ,bottom_line );
            draw_sprite(ball );
            update_sprite(ball );
            if ( circle_line_collision(ball ,left_line )  )
                collide_circle_line(ball ,left_line );
            if ( circle_line_collision(ball ,right_line )  )
                collide_circle_line(ball ,right_line );
            if ( circle_line_collision(ball ,top_line )  )
                collide_circle_line(ball ,top_line );
            if ( circle_line_collision(ball ,bottom_line )  )
                collide_circle_line(ball ,bottom_line );
            refresh_screen();
        }
        while ( ! (window_close_requested() ) );
        free_sprite(ball );
        release_all_resources();
        return 0;
    }
    
    Download Now"
  • 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.