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.


Topics - taegos

Pages: [1]
1
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