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

Author Topic: Circe Shape only moves when variable change or key press?  (Read 1604 times)

0 Members and 1 Guest are viewing this topic.

Mike64

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Circe Shape only moves when variable change or key press?
« on: December 12, 2017, 11:25:47 pm »
Hi, ive been coding a game, a bit like asteroids, but i have come around a weird problem, i have 2 variables, a AccelerationX and AccelerationY. I have a line of code outside the if statements for the keys and in the loop, but for some reason it only moves when i press a key. I think its because its only changing when the variable changes and i cant find out how to fix it.

What im trying to do is make it so it constantly moves and the Acceleration Variables go up or down as I press up or down, and the character moves constantly with the values of the acceleration.

 Here is what i have so far.

Main.cpp
#include <SFML/Graphics.hpp>
#include "iostream"
#include "Main.h"
#include "Ship.h"
#include "Test.h"
#include "Backround.h"

using namespace std; // Allowing the use of std
static float FrameSelection = 60.0f;
bool FrameRateMenu = false;

int Output()
{
        cout << FrameSelection << endl;
        cin.get();
        return 0;
};

int Frames3()
{
        sf::Time TickSpeed = sf::seconds(1.0f) / FrameSelection;
        cout << TickSpeed.asSeconds() << endl;
        Output();
        return 0;
};

int Input()
{
        while (FrameRateMenu = true);
        {


                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                {
                        FrameSelection += 30;
                        Frames3();
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                {
                        if (FrameSelection != 30)
                        {
                                FrameSelection -= 30;
                                Frames3();
                        }
                        if (FrameSelection < 30)
                        {
                                FrameSelection = 30;
                        }

                }
        }
        return 0;
};



// - This is For window ( Selecting Window Size and Creating it )

// Variables


// Setting Window Size


int Window::WindowSizeH;
int Window::WindowSizeL;
bool FirstRun = true;
bool DrawCheck = false;
bool ColorAssigned = false;
int  RCol = 0;
int GCol = 0;
int BCol = 0;
int ShipRCol;
int ShipGCol;
int ShipBCol;
char Response;

sf::RenderWindow Window::RWindow;
sf::View Window::MainView;

Window::Window()
{
        RWindow.create(sf::VideoMode(WindowSizeL,WindowSizeH), "RenderWindow");

        MainView.reset(sf::FloatRect(0, 0, WindowSizeL, WindowSizeH));
        MainView.setViewport(sf::FloatRect(0, 0, 1.0f, 1.0f));

        // This while loop is our main loop

        while (RWindow.isOpen())
        {
                // check all the window's events that were triggered since the last iteration of the loop
                sf::Event event;
                while (RWindow.pollEvent(event))
                {
                        // "close requested" event: we close the window
                        if (event.type == sf::Event::Closed)
                                RWindow.close();

                }

                // And under here is everything we want in our main loop

                RWindow.setView(MainView);
                MainView.setCenter(Ship::PlayerPos);
                RWindow.clear(sf::Color::Black);

                // Draw Checks
                Background();
                Ship();

               

                RWindow.display();


        }


}


int WindowI()
{
        int static WindowSizeH = 100;
        int static WindowSizeL = 100;
        int static WindowSizeLSet = 0;
        int static WindowSizeHSet = 0;
        WindowSize();
        return 0;
}


int WindowSize()
{
       
        cout << "Please Insert Window Size" << endl;
        cout << "Insert Window Length" << endl;
        cin >> WindowSizeLSet;
        cout << "Insert Window Height" << endl;
        cin >> WindowSizeHSet;
        Window::WindowSizeL = WindowSizeLSet;
        Window::WindowSizeH = WindowSizeHSet;
       
        Window();
       
        return 0;
};

// Window Creation


int Debug()
{
        cout << "I Work" << endl;

        return 0;
}

// - Main

int main()
{
        Frames3();
        WindowI();
};

Main.h
#pragma once

int WindowSize();
int static WindowSizeLSet;
int static WindowSizeHSet;

class Window
{
public:
        Window();
        static sf::RenderWindow RWindow;
        static sf::View MainView;
        int static WindowSizeH;
        int static WindowSizeL;
};
 

Ship.cpp
#include "Ship.h"
#include <SFML/Graphics.hpp>
#include "Main.h"
#include "iostream"

using namespace std;

float Ship::AccelX;
float Ship::AccelY;
float Ship::rotation;
sf::Vector2f Ship::PlayerPos;
sf::CircleShape Ship::PlayerShip;
int check = 0;

int RotationMovement()
{
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
                Ship::PlayerShip.rotate(-0.6);
                cout << "Left" << endl;
                Ship::rotation = Ship::PlayerShip.getRotation();
                cout << Ship::rotation << endl;
        }

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
                Ship::PlayerShip.rotate(0.6);
                Ship::rotation = Ship::PlayerShip.getRotation();
                cout << "Right" << endl;
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
                Ship::AccelY += cos(Ship::rotation) * 0.6 * -1;
                Ship::AccelX += sin(Ship::rotation) * 0.6 * -1;
                cout << "Acceleration X" << endl;
                cout << Ship::AccelX << endl;
                cout << "Acceleration Y" << endl;
                cout << Ship::AccelY << endl;
                cout << "Up" << endl;
        }
   
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
                Ship::AccelY += cos(Ship::rotation) * 0.6;
                Ship::AccelX += sin(Ship::rotation) * 0.6;
                cout << "Down" << endl;
        }


return 0;
};

int Movement()
{
check += 1;
cout << Ship::AccelX << endl;
cout << Ship::AccelY << endl;
cout << check << endl;
Ship::PlayerShip.setPosition(+Ship::AccelX, +Ship::AccelY);

return 0;
}
Ship::Ship()
{
        ShipSize = Window::WindowSizeH / 30;
        PlayerShip.setRadius(ShipSize);
        PlayerShip.setPointCount(3);
        PlayerShip.setOrigin(20, 20);
        PlayerShip.setPosition(Window::WindowSizeL / 2, Window::WindowSizeH / 2);

        if (ColorAssigned == false)
        {
                cout << "Choose Your Ships R Color" << endl;
                cin >> RCol;
                cout << "Choose Your Ships G Color" << endl;
                cin >> GCol;
                cout << "Choose Your Ships B Color" << endl;
                cin >> BCol;
                ColorAssigned = true;
        }

        // Input

        Movement();
        RotationMovement();
        PlayerShip.setOutlineColor(sf::Color(RCol, GCol, BCol));
        PlayerShip.setOutlineThickness(2);
        PlayerShip.setFillColor(sf::Color(0, 0, 0));
        Window::RWindow.draw(PlayerShip);
        PlayerPos = PlayerShip.getPosition();
}
 

Ship.h
#pragma once
#include <SFML/Graphics.hpp>
class Ship
{
public:
        Ship();
        static sf::Vector2f PlayerPos;
        int ShipSize;
        static sf::CircleShape PlayerShip;
        static float AccelX;
        static float AccelY;
        static float rotation;
};


bool extern ColorAssigned;
bool extern DrawCheck;
int extern RCol;
int extern GCol;
int     extern BCol;
 

Any help would be appreciated, and also the cos and sin values are just a place holder and once i get the constant movement i should be able to fix that aswell.

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Circe Shape only moves when variable change or key press?
« Reply #1 on: December 13, 2017, 12:53:40 am »
This code is quite hard to read through. It may be useful to spend more time learning C++ and object oriented programming before diving into a library like SFML. Specifically, it seems odd that you are creating and destroying your "ship" object every loop of your game and keeping track of its state using static members.

To try and directly answer your question, though, it looks like your problem is that you are treating AccelX and AccelY as positions and not accelerations. Meaning, you are only changing those values when a key is pressed, and then using those values directly to set the position of your circle.

Also, to clarify, it sounds like what you are actually wanting is a way to modify your ship's Velocity (not Acceleration). The basic concept is that you will need 2 variables, a Position and a Velocity. Every loop of your game you should add Velocity to Position and set your circle to that updated Position. You would then increase or decrease Velocity whenever UP or DOWN is pressed.

Ship::PlayerShip.setPosition(+Ship::AccelX, +Ship::AccelY);
 

This is just a guess, but this line tells me you might be on the right track, but just don't know correct C++ syntax. I'm not sure what you're expecting those  '+'  signs to do.

kepler0

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Circe Shape only moves when variable change or key press?
« Reply #2 on: December 13, 2017, 07:27:02 am »
There are lots of issues I see in your code, I'll just go through a few:

  • You have 2 variables called AccelX & AccelY, first of all, assuming these are the accelerations, you want to use Ship::PlayerShip.move(Ship::AccelX, Ship::AccelY); instead of just setting the position. Secondly, use sf::Vector's, you're going to find yourself using the sf::Vector constructor with the X and Y for SFML function arguments, which isn't very human readable.
  • Re-organize your classes. In my 3 years of C++, I've never seen a file called "Main.h". You got the .cpp for implementation and .h for declaration down, you just need to start splitting the files up accordingly, for example, your Window class should probably be in Window.h & Window.cpp.
  • Member functions, Object-orientation, Static variables, you seem very confused about these three.
     Remove the static parts from your Ship class and put those stray functions which affect ship movement inside the Ship class, in fact, I would just make a single update function inside the Ship class and put the stuff from Movement() and RotationMovement()
     in there.

I know this isn't codereview, and I know if I didn't say this now, someone else definitely would've, and Arcade already sort of has, but there you go. Object-orientation would be the biggest factor.
Zzz

Mike64

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Circe Shape only moves when variable change or key press?
« Reply #3 on: December 13, 2017, 06:01:16 pm »
Thank you for the quick replies, i will take into consideration everything that was said and i got it working nice and smoothly (;

 

anything