How To Change The Mass Of Sprites

This article will show you how to give different mass for different sprites and also the change in their behaviour accordingly.

Level

Advanced

Details

Higher number for mass means the object has higher inertia. If two objects collide with one is higher in mass, the direction and speed for the lower mass object will change significantly where the higher mass would continue its path with less difference.

To set the mass for an object you need to do the following steps:

  1. Get two images and make them sprites with initial position (Sprite Set X and Sprite Set Y)
  2. Set a velocity (Sprite Set Velocity) for those sprites
  3. Set the mass (Sprite Set Mass) for the sprites
  4. In the game loop collide both sprites with each other to observe the change in their behavior.

The code below shows how the mass will effect on speed and direction of an asteroid if it collides with the earth which is ten times heavier than the asteroid. You can try with the earth being only twice as heavy as the asteroid and see the difference.

Source Code Widget

  • program HowToChangeTheMassOfSprites;
    uses SwinGame,sgTypes;
    
    procedure Main();
    var
        earth : Sprite;
        asteroid : Sprite;
    begin
        OpenGraphicsWindow('Sprites With Different Mass' ,800 ,600 );
        ClearScreen(ColorWhite );
        LoadBitmapNamed('earth' ,'earth.png' );
        LoadBitmapNamed('asteroid' ,'asteroid.png' );
        earth := CreateSprite(BitmapNamed('earth' )  );
        SpriteSetX(earth ,700 );
        SpriteSetY(earth ,100 );
        SpriteSetVelocity(earth ,VectorTo(-0.8 ,0.6 )  );
        SpriteSetMass(earth ,10 );
        asteroid := CreateSprite(BitmapNamed('asteroid' )  );
        SpriteSetX(asteroid ,100 );
        SpriteSetY(asteroid ,500 );
        SpriteSetVelocity(asteroid ,VectorTo(1 ,-0.6 )  );
        SpriteSetMass(asteroid ,1 );
        repeat
            ProcessEvents();
            ClearScreen(ColorWhite );
            DrawSprite(earth );
            UpdateSprite(earth );
            DrawSprite(asteroid );
            UpdateSprite(asteroid );
            if  SpriteCollision(earth ,asteroid )    then
                CollideCircles(asteroid ,earth );
            RefreshScreen();
        
        until WindowCloseRequested();
        FreeSprite(earth );
        FreeSprite(asteroid );
        ReleaseAllResources();
    end;
    begin
        Main();
    end.
    Download Now"
  • #include 
    #include "SwinGame.h"
    int main() 
    {
        sprite  earth;
        sprite  asteroid;
        open_graphics_window("Sprites With Different Mass" ,800 ,600 );
        clear_screen(color_white );
        load_bitmap_named("earth" ,"earth.png" );
        load_bitmap_named("asteroid" ,"asteroid.png" );
        earth = create_sprite(bitmap_named("earth" ) ) ;
        sprite_set_x(earth ,700 );
        sprite_set_y(earth ,100 );
        sprite_set_velocity(earth ,vector_to(-0.8 ,0.6 ) );
        sprite_set_mass(earth ,10 );
        asteroid = create_sprite(bitmap_named("asteroid" ) ) ;
        sprite_set_x(asteroid ,100 );
        sprite_set_y(asteroid ,500 );
        sprite_set_velocity(asteroid ,vector_to(1 ,-0.6 ) );
        sprite_set_mass(asteroid ,1 );
        do 
        {
            process_events();
            clear_screen(color_white );
            draw_sprite(earth );
            update_sprite(earth );
            draw_sprite(asteroid );
            update_sprite(asteroid );
            if ( sprite_collision(earth ,asteroid )  )
                collide_circles(asteroid ,earth );
            refresh_screen();
        }
        while ( ! (window_close_requested() ) );
        free_sprite(earth );
        free_sprite(asteroid );
        release_all_resources();
        return 0;
    }
    
    Download Now"
  • Source Code Coming Soon

  • Source Code Coming Soon

  • Source Code Coming Soon

In reality if an asteroid hits the earth which is 10 times heavier than that asteroid, there will be explosion and they both will be destroyed (Unless a superhero comes and saves the earth). If you want to make it look real, you can do the animation (read animation).

Possibilities

Suggested projects will appear here.