How To Detect Collision Between Sprites

This article will show you if there is a collision between two sprites.

Level

Advanced

Read these first

How To Move A Sprite

Details

SwinGame lets you detect if two sprites are in touch with each other. After detecting the collision, SwinGame can change the path of two sprites, it can vanish the sprites or do whatever you wish. The code below shows you how to detect the collision. The steps it performs are:

  1. Load sprites that are going to collide in game environment
  2. Create sprites from those images
  3. Place and give velocities to those images
  4. Place and give velocities to those images
  5. In game loop update the sprites, draw the sprites and check if sprite collision happens
  6. Do something if there is a collision

Source Code Widget

  • program HowToDetectCollisionBetweenSprites;
    uses SwinGame,sgTypes;
    
    procedure Main();
    var
        ball1 : Sprite;
        ball2 : Sprite;
    begin
        OpenGraphicsWindow('Detect Collision' ,800 ,600 );
        LoadBitmapNamed('ball' ,'ball_small.png' );
        ball1 := CreateSprite(BitmapNamed('ball' )  );
        ball2 := CreateSprite(BitmapNamed('ball' )  );
        SpriteSetX(ball1 ,400 );
        SpriteSetY(ball1 ,200 );
        SpriteSetDx(ball1 ,0 );
        SpriteSetDy(ball1 ,0.1 );
        SpriteSetX(ball2 ,400 );
        SpriteSetY(ball2 ,400 );
        SpriteSetDx(ball2 ,0 );
        SpriteSetDy(ball2 ,-0.1 );
        repeat
            ClearScreen(ColorWhite );
            if  SpriteCollision(ball1 ,ball2 )    then
            begin
                DrawText('Collision Happens!!!' ,ColorRed ,380 ,15 );
            end;
            DrawSprite(ball1 );
            UpdateSprite(ball1 );
            DrawSprite(ball2 );
            UpdateSprite(ball2 );
            RefreshScreen();
            ProcessEvents();
        
        until WindowCloseRequested();
        FreeSprite(ball1 );
        FreeSprite(ball2 );
        ReleaseAllResources();
    end;
    begin
        Main();
    end.
    Download Now"
  • #include 
    #include "SwinGame.h"
    int main() 
    {
        sprite  ball1;
        sprite  ball2;
        open_graphics_window("Detect Collision" ,800 ,600 );
        load_bitmap_named("ball" ,"ball_small.png" );
        ball1 = create_sprite(bitmap_named("ball" ) ) ;
        ball2 = create_sprite(bitmap_named("ball" ) ) ;
        sprite_set_x(ball1 ,400 );
        sprite_set_y(ball1 ,200 );
        sprite_set_dx(ball1 ,0 );
        sprite_set_dy(ball1 ,0.1 );
        sprite_set_x(ball2 ,400 );
        sprite_set_y(ball2 ,400 );
        sprite_set_dx(ball2 ,0 );
        sprite_set_dy(ball2 ,-0.1 );
        do 
        {
            clear_screen(color_white );
            if ( sprite_collision(ball1 ,ball2 )  )
            {
                draw_text("Collision Happens!!!" ,color_red ,380 ,15 );
            }
            draw_sprite(ball1 );
            update_sprite(ball1 );
            draw_sprite(ball2 );
            update_sprite(ball2 );
            refresh_screen();
            process_events();
        }
        while ( ! (window_close_requested() ) );
        free_sprite(ball1 );
        free_sprite(ball2 );
        release_all_resources();
        return 0;
    }
    
    Download Now"
  • Source Code Coming Soon

  • Source Code Coming Soon

  • Source Code Coming Soon


Possibilities

You can make a simple pong game with random velocity and direction of the ball