How To Use Vector To Determine A Path

This how to will explain how to draw a line path using a vector function in SwinGame. SwinGame has built in support for the use of Vectors.

howtousevectortodetermineapath

Level

Advanced

Read these first

How To Draw A Bitmap

Details

Using SwinGame functions, it is a fairly simple process to determine the path and speed of a shape or object using vectors. To find the distance between objects using vectors you need to follow the steps below:

Begin by locating a point on in the Game Window, and call it the OriginPoint. Use VectorTo to apply direction and speed to your image. In the Game loop use the function SubtractVector and store the result in a variable.

Use LineFromVector to create a new line that you will draw on the screen. Now Load images into various locations in the Game Window and call DrawLine to draw lines between the images by calling LineFromVector.

The code below shows a small program that demonstrates these vector functions in action.

Source Code Widget

  • program HowToUseVectorToDetermineAPath;
    uses SwinGame,sgTypes;
    
    procedure Main();
    var
        origin : Point2D;
        ufoVector : Point2D;
        resultant : Point2D;
        distance : LineSegment;
        planetVector : Point2D;
    begin
        OpenGraphicsWindow('Determine Vector Path' ,800 ,600 );
        ClearScreen(ColorWhite );
        origin := PointAt(0 ,0 );
        planetVector := VectorTo(700 ,100 );
        ufoVector := VectorTo(150 ,520 );
        LoadBitmapNamed('planet' ,'planet.png' );
        LoadBitmapNamed('ufo' ,'ufo.png' );
        repeat
            ProcessEvents();
            ClearScreen(ColorWhite );
            resultant := SubtractVectors(planetVector ,ufoVector );
            distance := LineFromVector(ufoVector ,resultant );
            DrawBitmap('planet' ,planetVector );
            DrawBitmap('ufo' ,ufoVector );
            DrawLine(ColorBlue ,origin ,planetVector );
            DrawLine(ColorRed ,origin ,ufoVector );
            DrawLine(ColorGreen ,distance );
            RefreshScreen();
        
        until WindowcloseRequested();
        ReleaseAllResources();
    end;
    begin
        Main();
    end.
    Download Now"
  • #include 
    #include "SwinGame.h"
    int main() 
    {
        point2d  origin;
        point2d  ufo_vector;
        point2d  resultant;
        line_segment  distance;
        point2d  planet_vector;
        open_graphics_window("Determine Vector Path" ,800 ,600 );
        clear_screen(color_white );
        origin = point_at(0 ,0 ) ;
        planet_vector = vector_to(700 ,100 ) ;
        ufo_vector = vector_to(150 ,520 ) ;
        load_bitmap_named("planet" ,"planet.png" );
        load_bitmap_named("ufo" ,"ufo.png" );
        do 
        {
            process_events();
            clear_screen(color_white );
            resultant = subtract_vectors(planet_vector ,ufo_vector ) ;
            distance = line_from_vector(ufo_vector ,resultant ) ;
            draw_bitmap("planet" ,planet_vector );
            draw_bitmap("ufo" ,ufo_vector );
            draw_line(color_blue ,origin ,planet_vector );
            draw_line(color_red ,origin ,ufo_vector );
            draw_line(color_green ,distance );
            refresh_screen();
        }
        while ( ! (windowclose_requested() ) );
        release_all_resources();
        return 0;
    }
    
  • 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.