Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Feedback on my projectile trajectory model.  (Read 1816 times)

0 Members and 1 Guest are viewing this topic.

taegos

  • Newbie
  • *
  • Posts: 2
    • View Profile
Feedback on my projectile trajectory model.
« on: December 06, 2016, 08:49:14 pm »
Hello! I am trying to design a model for a projectile trajectory. This is my first time doing something like this so what i have done is probably not the best solution. I just applied my basic math knowledge from high school   ;D The current flaws i know of is that its not very compatible with different screens (how could i solve this?) i also like to make the trajectory have a relation with a starting angle. (I am making a tank game where the goal is to be able to fire a projectile using a power and angle variable). This is the parabola that the projectile currently follows: https://www.wolframalpha.com/input/?i=y+%3D+-x%5E2%2F1800%2B2x and here is my code:

#include <SFML/Graphics.hpp>
#include "game.h"
#include <string>
#include <iostream>
#include <vector>
#include <math.h>

using namespace std;

int main() {

        sf::RenderWindow window(sf::VideoMode::getDesktopMode(),
                "Test", sf::Style::Fullscreen);

        window.setFramerateLimit(60);

        sf::CircleShape player(20, 5);
        player.setFillColor(sf::Color(150, 70, 250));
        player.setOutlineThickness(4);
        player.setOutlineColor(sf::Color(100, 50, 250));
        player.setPosition(200, window.getSize().y-400);
       
        bool shoot{ false };
        float yPos{};
        float timer{ 0 };
        float frametime = 1.0f / 60.0f;

        while (window.isOpen()) {
                sf::Event event;
                while (window.pollEvent(event)) {
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)){
                                shoot = true;
                        }
                        else if(sf::Event::Closed){
                                window.close();
                        }
                }
                if (shoot) {
                        timer += frametime*1000;
                        yPos = -pow(timer, 2)/1800+2*timer;
                        player.setPosition(200+timer, window.getSize().y-yPos-400);
                        if (player.getPosition().y > window.getSize().y) {
                                player.setPosition(200, window.getSize().y - 400);
                                timer = 0;
                                shoot = false;
                        }
                }
                window.clear(sf::Color(16, 16, 16, 255));
                window.draw(player);
                window.display();
        }
}

EDIT: Added link to the function i am using.
« Last Edit: December 06, 2016, 08:51:30 pm by taegos »

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: Feedback on my projectile trajectory model.
« Reply #1 on: December 07, 2016, 01:28:47 am »
as i remember the parabola is y = x^2 but your equation looks some special case of parabola graphs with uniform curving. it looks cool from the link but i don't think it is suitable for tank game that you are going to make. the real-time physics of projectile trajectory has to consider  some factors like the thrust force, velocity, angle of cannon, the gravity and air resistant  ...etc. here link for further info.

about your question how to contain the projectile within the window screen. this can be achieve from the equation itself. the constant 1800 represents the "max height from the origin". and you already calculate it when you set player position. so if you reuse it again in the equation it would be some thing like this:

projectile.setPosition(200, window.getSize().y - 400);
sf::Vector2f origin_pos = projectile.getPosition();
x += frametime * speed;
y = -pow(x, 2) / origin_pos.y + 2 * x;
projectile .setPosition(origin_pos + sf::Vector2f(x, -y));
« Last Edit: December 07, 2016, 01:52:16 am by Mortal »

jamesL

  • Full Member
  • ***
  • Posts: 124
    • View Profile
Re: Feedback on my projectile trajectory model.
« Reply #2 on: December 07, 2016, 08:39:08 am »

taegos

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Feedback on my projectile trajectory model.
« Reply #3 on: December 07, 2016, 02:46:30 pm »
Thanks for the input, i edited the main equation so that it uses initial "power" and angle instead:

        if (shoot) {
                        x += elapsed*800;
                        y = x*tan(angle*PI/180) - (g * pow(x, 2)) / (2 * pow(power, 2) * pow(cos(angle*PI / 180), 2));
                        player.setPosition(200+x, window.getSize().y - y-200);
                        if (player.getPosition().y > window.getSize().y) {
                                cout << player.getPosition().x << endl;
                                player.setPosition(200, window.getSize().y-200);
                                x = 0;
                                shoot = false;
                        }
                }

Angle and power are adjusted using the arrow keys. The trajectory now looks kind of good except for one thing. The "speed" of the shoot is only affected by travel distance in x. So when shooting with an angle close to 90 (straight up) the shoot moves extremely fast where as when i shoot with an angle close to and above 0 it moves extremely slow.