BALLISTIC MISSILE SIMULATOR

2 posters

Go down

BALLISTIC MISSILE SIMULATOR Empty BALLISTIC MISSILE SIMULATOR

Post by salty December 7th 2014, 12:36 am

simulator made in c++ using newtonian physics in a vacuum

only compiles in windows and uses a very bad practice (system("cls") will get you lynched on a c or c++ forum)



bugs: screen doesnt scale so velocities greater than ~45m/s will just result in the projectile staying on the side
Code:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <windows.h>

using namespace std;

const int SIZE_X = 80; // 80 characters wide
const int SIZE_Y = 48; // 48 characters tall
const float PI = 3.14159; // Accuracy is more than sufficient with this precision

class PhysicsObject
{
public:
    float proj_posX, proj_posY, proj_posX_offset; // Position variables
    float proj_vel_x_i, proj_vel_x_f, proj_vel_y_i, proj_vel_y_f; // Velocity variables
    float proj_time; // Flight time of projectile
    float proj_max_height; // Maximum height of projectile.
    COORD position; // The on-screen coordinates of the object.

    void initialize(double vel_x_i,double vel_y_i)
    {
        proj_vel_x_i = vel_x_i;
        proj_vel_y_i = vel_y_i;
        proj_posX_offset = 0; // No x offset
        proj_posY = 0.0001; // Small amount off the floor so the projectile fires.
        proj_time = ((0-proj_vel_y_i)/(0-9.81))*2; // Time to reach maximum arc height of projectile motion times two for the full arc
        proj_max_height = ((proj_vel_y_i+0)/2)*(proj_time/2); // Maximum height of projectile when velocity_y_final = 0 (peak of parabolic arc)
    }

    void initialize(double vel_x_i,double vel_y_i, double offset)
    {
        proj_vel_x_i = vel_x_i;
        proj_vel_y_i = vel_y_i;
        proj_posX_offset = offset;
        proj_posY = 0.0001; // Small amount off the floor so the projectile fires.
        proj_time = ((0-proj_vel_y_i)/(0-9.81))*2; // Time to reach maximum arc height of projectile motion times two for the full arc
        proj_max_height = ((proj_vel_y_i+0)/2)*(proj_time/2); // Maximum height of projectile when velocity_y_final = 0 (peak of parabolic arc)
    }

    void step(double time)
    {
        proj_posX = (proj_vel_x_i*time)+proj_posX_offset; // Set projectile x position.
        proj_vel_y_f = proj_vel_y_i + (0-9.81)*time;
        proj_posY = ((proj_vel_y_i+proj_vel_y_f)/2)*time; // Set projectile y position.
        position = {proj_posX, SIZE_Y/2-proj_posY};
    }

    float getHeight() {
        return proj_posY;
    }

    float getFlightTime() {
        return proj_time;
    }

    float getMaxHeight() {
        return proj_max_height;
    }

    float getPositionX() {
        return proj_posX;
    }

    float getPositionY() {
        return proj_posY;
    }

    float getVelocity() {
        return proj_vel_y_f;
    }

    COORD getPosition() {
        if (position.X >= SIZE_X) {
            position.X = SIZE_X-3;
        }
        if (position.Y <= 0) {
            position.Y = 0;
        }
        if (position.Y >= SIZE_Y/2) {
            position.Y = SIZE_Y/2;
        }
        return position;
    }
};

int env_time = 0;
PhysicsObject missile;

int graphics_step(int refresh_rate) {
    Sleep(refresh_rate);
    system("cls");
}


void proj_validate(int *deg) {
    if(*deg > 90) {
        printf("WARNING: MAXIMUM LAUNCH ANGLE IS 90 DEGREES; ANGLE SET TO ANGLE MOD 90 DEGREES\n");
        *deg %= 90;
    }
    else if(*deg <= 0) {
        printf("WARNING: MINIMUM LAUNCH ANGLE IS 1 DEGREE; ANGLE SET TO 45 DEGREES\n");
        *deg = 45;
    }
}

void launch() {
    srand(420420420420); // Set random seed with arbitrary value
    // Define graphics variables
    int sim_refresh_rate = 15; // 50ms
    HANDLE console_output = GetStdHandle(STD_OUTPUT_HANDLE); // Get window handle for console.
    float sim_time = 0; // Local simulation time.
    COORD floor = {0,(SIZE_Y/2)+1}; // Note that the 'floor' is actually half of the width because otherwise the console scrolls to the bottom.
    COORD infobox = {0,1};
    // Define launch variables
    int debris_count = ((rand() % 6) + 1);
    system("cls"); // Clear the screen!
    // Launch the missile!
    while(missile.getHeight() > 0) {
        sim_time+=0.05; // Step forward 50ms in time.
        missile.step(sim_time);
        SetConsoleCursorPosition(console_output, missile.getPosition()); // Set the position of the cursor.
        printf("#"); // Print the missile.
        // Draw floor
        SetConsoleCursorPosition(console_output, floor); // Set the position of the cursor.
        printf("=====================================80 m======================================="); // Print the missile.
        // Draw room information
        SetConsoleCursorPosition(console_output, infobox); // Set the position of the cursor.
        printf("[SPEED: %f m/s |HEIGHT: %f m]",missile.getVelocity(),missile.getHeight()); // Print the missile.
        graphics_step(sim_refresh_rate); // Clear screen.
    }
    // Do an exploding thing.
    PhysicsObject debris[debris_count];
    // Initialize each piece of debris
    for(int i=0;i<debris_count;i++) {
        debris[i].initialize((rand() % 16)-8,(rand() % 8)+4,missile.getPositionX());
    }
    sim_time = 0; // Reset sim time.
    for(int j=0;j<25;j++) { // Do for 1 second's worth of motion.
        for(int i=0;i<debris_count;i++) {
            if(debris[i].getHeight() >= -5) {
                sim_time += 0.04;
                debris[i].step(sim_time);
                SetConsoleCursorPosition(console_output, debris[i].getPosition());
                printf("*");
                SetConsoleCursorPosition(console_output, floor); // Set the position of the cursor.
                printf("=====================================80 m======================================="); // Print the missile.
                SetConsoleCursorPosition(console_output, infobox); // Set the position of the cursor.
                printf("[SPEED: %f m/s |HEIGHT: %f m]",missile.getVelocity(),missile.getHeight()); // Print the missile.
            }
        }
    graphics_step(sim_refresh_rate); // Clear screen.
    }
    // Show flight results.
    // Grab data from missile
    float max_height = missile.getMaxHeight();
    float displacement = missile.getPositionX(); // Now that the flight is over, and the missile starts at x=0, the current value is equal to the displacement.
    float flight_duration = missile.getFlightTime();
    // Print results
    printf("Flight duration: %.3f seconds\n",flight_duration);
    if(max_height > 1000) {
        printf("Maximum height of projectile: %.3f kilometers\n",max_height/1000);
    }
    else {
        printf("Maximum height of projectile: %.3f meters\n",max_height);
    }
    if(displacement > 1000) {
        printf("Projectile displacement: %.3f kilometers\n",displacement/1000);
    }
    else {
        printf("Projectile displacement: %.3f meters\n",displacement);
    }
}

int main()
{
    int angle;
    float velocity,proj_vel_x_i, proj_vel_y_i;
    while(true) {
    system("cls");
    fflush(stdin);
    printf("PROJECTILE SIMULATOR!!\n\n");
    printf("ENTER PROJECTILE ANGLE (deg): ");
    // Get the launch angle
    scanf("%i",&angle);
    // Verify the launch angle
    proj_validate(&angle);
    printf("ENTER LAUNCH VELOCITY (m/s): ");
    // Get the launch velocity
    scanf("%f",&velocity);
    printf("\nCOMPUTING VELOCITY");
    // Determine the component vector velocities
    printf("."); // First step is done
    proj_vel_y_i = velocity*sin(angle*PI/180);
    printf("."); // Second step is done (sine)
    proj_vel_x_i = velocity*cos(angle*PI/180);
    printf(".\n\n"); // Third step is done (cosine)
    // Print values and prepare for launch.
    printf("INITIAL HORIZONTAL VELOCITY: %.3f m/s \nINITIAL VERTICAL VELOCITY: %.3f m/s \n\n",proj_vel_x_i,proj_vel_y_i);
    missile.initialize(proj_vel_x_i,proj_vel_y_i); // Initialize the missile
    Sleep(1000);
    launch();
    system("pause");
    }
    return 0;
}


Last edited by salty on December 7th 2014, 2:10 am; edited 2 times in total
salty
salty
STILL UNSTOPPABLE

Posts : 10341
volume of testosterone : 293647
Join date : 2013-07-02
Age : 99
Location : AMERICA

http://www.otthunderdome.com

Back to top Go down

BALLISTIC MISSILE SIMULATOR Empty Re: BALLISTIC MISSILE SIMULATOR

Post by salty December 7th 2014, 12:42 am

i didnt clean any of the code up so im pretty sure env_time is useless as well as some more
salty
salty
STILL UNSTOPPABLE

Posts : 10341
volume of testosterone : 293647
Join date : 2013-07-02
Age : 99
Location : AMERICA

http://www.otthunderdome.com

Back to top Go down

BALLISTIC MISSILE SIMULATOR Empty Re: BALLISTIC MISSILE SIMULATOR

Post by salty December 7th 2014, 1:43 am

the height and velocity of the missile is now displayed as well as the length of the floor
salty
salty
STILL UNSTOPPABLE

Posts : 10341
volume of testosterone : 293647
Join date : 2013-07-02
Age : 99
Location : AMERICA

http://www.otthunderdome.com

Back to top Go down

BALLISTIC MISSILE SIMULATOR Empty Re: BALLISTIC MISSILE SIMULATOR

Post by salty December 7th 2014, 11:05 pm

tomoko it would be a good learning exercise to read my code and figure out what it does (its very simple shit)/what can be improved if youre actually taking a cs class
salty
salty
STILL UNSTOPPABLE

Posts : 10341
volume of testosterone : 293647
Join date : 2013-07-02
Age : 99
Location : AMERICA

http://www.otthunderdome.com

Back to top Go down

BALLISTIC MISSILE SIMULATOR Empty Re: BALLISTIC MISSILE SIMULATOR

Post by Guest December 8th 2014, 1:42 pm

the little animation of it flying is neat.

Guest
Guest


Back to top Go down

BALLISTIC MISSILE SIMULATOR Empty Re: BALLISTIC MISSILE SIMULATOR

Post by salty December 18th 2014, 8:16 pm

tried adding air resistance to this and fucked up last week will add it over break
salty
salty
STILL UNSTOPPABLE

Posts : 10341
volume of testosterone : 293647
Join date : 2013-07-02
Age : 99
Location : AMERICA

http://www.otthunderdome.com

Back to top Go down

BALLISTIC MISSILE SIMULATOR Empty Re: BALLISTIC MISSILE SIMULATOR

Post by him December 19th 2014, 7:33 am

Sounds cool
him
him
Praetorian of Liberty

Posts : 6989
volume of testosterone : 300334
Join date : 2014-09-01
Location : 100 FT UNDER THE WATER CUZ FLORIDA IS LEEETERULLY SINKING!!!!!!

Back to top Go down

BALLISTIC MISSILE SIMULATOR Empty Re: BALLISTIC MISSILE SIMULATOR

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum