How To Collide A Sprite With Rectangle

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

Level

Advanced

Details

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:

  1. 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.
  2. Declare four rectangle variables.
  3. Create four rectangles (Rectangle 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 and fill all the rectangles (Fill Rectangle) with some color
  7. Draw (Draw Sprite) and update (Update Sprite) the sprite
  8. Detect if there is a collision (Sprite Rect Collision) between the sprite and any of the rectangles
  9. 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.
  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 rectangle.

Source Code Widget

  • 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.
    Download Now"
  • #include 
    #include "SwinGame.h"
    int main() 
    {
        rectangle  top_rect;
        rectangle  left_rect;
        sprite  ball;
        rectangle  bottom_rect;
        rectangle  right_rect;
        open_graphics_window("Bouncing Ball" ,800 ,600 );
        clear_screen(color_white );
        load_bitmap_named("ball" ,"ball_small.png" );
        left_rect = rectangle_from(50 ,80 ,30 ,440 ) ;
        right_rect = rectangle_from(720 ,80 ,30 ,440 ) ;
        top_rect = rectangle_from(80 ,50 ,640 ,30 ) ;
        bottom_rect = rectangle_from(80 ,520 ,640 ,30 ) ;
        ball = create_sprite(bitmap_named("ball" ) ) ;
        sprite_set_x(ball ,200 );
        sprite_set_y(ball ,400 );
        sprite_set_velocity(ball ,vector_to(1 ,-0.6 ) );
        do 
        {
            process_events();
            clear_screen(color_white );
            fill_rectangle(color_blue ,left_rect );
            fill_rectangle(color_red ,right_rect );
            fill_rectangle(color_green ,top_rect );
            fill_rectangle(color_yellow ,bottom_rect );
            draw_sprite(ball );
            update_sprite(ball );
            if ( sprite_rect_collision(ball ,left_rect )  )
                collide_circle_rectangle(ball ,left_rect );
            if ( sprite_rect_collision(ball ,right_rect )  )
                collide_circle_rectangle(ball ,right_rect );
            if ( sprite_rect_collision(ball ,top_rect )  )
                collide_circle_rectangle(ball ,top_rect );
            if ( sprite_rect_collision(ball ,bottom_rect )  )
                collide_circle_rectangle(ball ,bottom_rect );
            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.