Sign in to follow this  
Merlin_MLN The Magician

Homing Missiles Problem

Recommended Posts

Dear Sirs,

some weeks ago I found Amarillions great tutorial and i leand a lot from it.

http://www.helixsoft.nl/articles/circle/sincos.htm

I was really fascinated, so i decided to translate your tutorial into german. But i updated the codes to get rid of the fixed stuff. Nowadays our gfx cards are fast enough to perform animations by calculating double float or short values!
Now i'm trying to translate and update the codes too. I want to translate them into C++ with grphics.h(WinBGIM), C++ with SFML, C++ with SDL, C++ with Allegro 5, Java, Java-Script, Blitz 3D, Blitz Max, FreeBasic and Pure Basic. Perhaps there are more coding languages to come. I decided to use graphics.h first because it is very easy to use. But i've got a problem with the circ7 (home_in) from the homing missiles example. My rocket never hits the target. I attached the hole project to this post.

Here is the code:

#include<windows.h>
#include<graphics.h>
#include<cmath>

#define PI 3.141592654

// circ7

void home_in();

int SCREEN_W = 600;
int SCREEN_H = 400;

main(int argc, char*argv[])
{
   // Deklaration und Initialisierung des Grafiktreibers
    int graphdriver, graphmode;
    graphdriver = DETECT;
    initgraph(&graphdriver, &graphmode, "Draw_Sine"); // graphics.h kann keine Windowstitel setzen

    setbkcolor(0); // Hintergrundfarbe des Ausgabefensters schwarz
    cleardevice(); // Löschen des Ausgabefensters

    setcolor(15); // Textfarbe weiss (15)
    settextstyle(10, HORIZ_DIR, 2); // Fontsyle, Richtung und Größe
    //outtextxy(20, 120, "Sinuskurve zeichnen: "); // Textausgabe

    home_in(); // Funktionsaufruf von draw_circle()

 return 0;
}

void home_in ()
{
    // the x, y position of the homing missile
    int x = SCREEN_W / 2;
    int y = SCREEN_H / 2;
    // the angle and length of the missile's velocity vector
    double angle = 0.0;
    int length = 1;
    double angle_stepsize = 1*  PI / 180;
    // determines whether the missile has reached
    // the target and a new one should be chosen
    bool new_target = true;
    // angle to the target
    double target_angle;
    // position of the target
    int target_x;
    int target_y;

    while (!GetAsyncKeyState(VK_ESCAPE))
    {
        setbkcolor(0); // Hintergrundfarbe des Ausgabefensters schwarz
        cleardevice(); // Löschen des Ausgabefensters
        // choose new target randomly when needed
        if (new_target == true)
        {
            target_x = ((SCREEN_W + rand() % (2 * SCREEN_W)) / 4);
            target_y = ((SCREEN_H + rand() % (2 * SCREEN_H)) / 4);
            new_target = false;
        }

        // draw a pixel where the target is
        putpixel ((target_x), (target_y),15);

        // draw the missile
        // (actually a circle with a line representing the angle)
        setcolor(WHITE);
        circle (x, y, 20);
        line (x, y, x +  (9 * cos (angle)), y +  (9 * sin (angle)));

        // move the missile
        x = x + length * cos (angle);
        y = y + length * sin (angle);

        // if we are very close to the target, set a new target
        if (abs (x - target_x) + abs (y - target_y) < 10)
        {
             new_target = true;
        }

        // calculate the angle from the missile to the target
        target_angle = atan2 (target_y - y, target_x - x);

        if (fmod(angle - target_angle, 2*PI) < PI)
        {
            angle = (angle - angle_stepsize) ;
        }
        else
        {
            angle = (angle + angle_stepsize) ;
        }
        delay (10);
    }
}
 

Best regards,

Merlin

 

 

Share this post


Link to post
Share on other sites

But i updated the codes to get rid of the fixed stuff. Nowadays our gfx cards are fast enough to perform animations by calculating double float or short values!

 

 

Then do so and change your x and y variable types. You are adding the results of cos and sin to them and the result is truncated (in positive directions) because the type is int.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this