SFML community forums

Help => General => Topic started by: taegos on December 06, 2016, 08:49:14 pm

Title: Feedback on my projectile trajectory model.
Post by: taegos 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 (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.
Title: Re: Feedback on my projectile trajectory model.
Post by: Mortal 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 (https://en.wikipedia.org/wiki/Trajectory_of_a_projectile) 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));
Title: Re: Feedback on my projectile trajectory model.
Post by: jamesL on December 07, 2016, 08:39:08 am
these are in javascript, but the math and concepts stay the same

https://www.youtube.com/playlist?list=PL7wAPgl1JVvWkDTlqUQtgFLd_QX2bEY5H

he has lots of good stuff
https://www.youtube.com/user/codingmath/playlists

as does this guy
https://www.youtube.com/playlist?list=PLW3Zl3wyJwWOpdhYedlD-yCB7WQoHf-My


Title: Re: Feedback on my projectile trajectory model.
Post by: taegos 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.