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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - taegos

Pages: [1]
1
General / Re: Feedback on my projectile trajectory model.
« 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.

2
General / 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.

Pages: [1]
anything