After a bit of effort, I think I managed to compress my code into the basics to represent my problem. Unfortunately, my problem depends on a few complicated systems. Too long? The original was over 220 lines long on three files. This is 120 lines in one document. You're welcome.
Anyway, the problem I present to you is one I don't quite understand. When I usually get problems like these, the solution seems evident enough, but I don't think things quite add up in this case. You see, there is an object in the middle of the screen that can move around and follow your cursor when you press the Up key. When your mouse is still, it goes at a constant pace. However, whenever the mouse moves, the object speeds up. That's not the end of it. If I'm outputting text to the console window in the game loop, the game moves at the same pace as if the mouse were moving in the previous explanation. HOWEVER, the game slows to the calm pace when the object in the middle faces directly right. (Why right? It has to do with some math, but it used to be directly up before I subtracted 90 from an angle.) I think this has to do with me trying to control the speed of the game based on the frame rate of the game. (e.g. Game has half the expected frame rate? Make everything move twice as fast per frame.)
I've been looking at this for a long time, and I've gotten lost in a sea of possibilities. Any help is appreciated. Thanks.
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <math.h>
#include <iostream> //For bug fixing
#define PI 3.1415926535
class playerCharacter {
public:
sf::Vector2f Position;
short int Rotation;
sf::Vector2f Velocity;
sf::CircleShape Visual;
char Direction;
friend void Update(playerCharacter *plyr, sf::Int32 passedTime);
};
void Update(playerCharacter *plyr, sf::Int32 passedTime){ //If I use Int64 here, it doesn't build. Probably because this computer doesn't use 64 of them thingamabobs.
float timeFactor = 60.0/(1.0/((float)passedTime / 1000000.0)); //I suspect all troubles come from this line, where I try to make sure that the speed of the game is independant of the frame rate.
//std::cout<<timeFactor<<","<<passedTime<<std::endl;
plyr->Position.x = plyr->Position.x + plyr->Velocity.x * timeFactor;
plyr->Position.y = plyr->Position.y + plyr->Velocity.y * timeFactor;
}
int main(){
//System-related
sf::Clock gameClock;
sf::Clock rClock;
sf::VideoMode vm(400,300);
sf::RenderWindow window(vm, "Placeholder", 7);
sf::Vector2f mousePosition;
playerCharacter teabot;
teabot.Position = sf::Vector2f((float)window.getSize().x/2, (float)window.getSize().y/2);
//System-related END
//Graphics-related
sf::View view(sf::FloatRect(teabot.Position.x-(float)80, teabot.Position.y-((float)vm.height/(float)vm.width) * (float)80, (float)160, ((float)vm.height/(float)vm.width) * (float)160));
teabot.Visual.setRadius(128);
teabot.Visual.setPointCount(4);
teabot.Visual.setFillColor(sf::Color::Blue);
teabot.Visual.setOrigin(128, 128);
teabot.Visual.setScale(1.f/32.f, 1.f/32.f);
sf::CircleShape mReference(0.4, 5.0);
mReference.setFillColor(sf::Color::White);
//Graphics-related END
//Window options
window.setVerticalSyncEnabled(true);
window.setFramerateLimit(60);
window.setView(view);
//Game loop
while(window.isOpen()){
gameClock.restart();
sf::Event event;
while(window.pollEvent(event)){
switch(event.type){
case sf::Event::Closed :
window.close();
break;
case sf::Event::KeyPressed :
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)){
window.close();
}
//Key presses
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
teabot.Direction = 'F';
//Key presses END
break;
case sf::Event::MouseMoved :
mousePosition = (sf::Vector2f) sf::Mouse::getPosition(window); //Whenever I move the mouse, the game's speed increases
break;
}
}
view.setCenter(teabot.Position);
window.setView(view);
teabot.Visual.setPosition((sf::Vector2f)teabot.Position);
teabot.Rotation = 90 + atan2( (float)(mousePosition.y + (view.getCenter().y - window.getSize().y/2)) - teabot.Position.y, (float)(mousePosition.x + (view.getCenter().x - window.getSize().x/2)) - teabot.Position.x) / (float)PI * 180;
teabot.Visual.setRotation(teabot.Rotation);
//Work on stuff below. Note: Character slows down when facing directly right, investigate.
//When I get rid of output to cout, the entire game seems to slow to a constant rate, and it speeds up when the mouse moves.
//Curiouser and curiouser. Because the slow down caused by facing the object to the right is relatively unnoticeable due to the new slowing by lack of output.
//Theory: The output does cause the game to speed up, but something about all the values outputted being so simple when the object faces the right makes the console window update in a different way. Perhaps, not update at all?
//Then why does the mouse speed up the game when there's no output? Should the game be as slow as it is when the mouse isn't moving? Does the update of a mouse cause the window to update at the rate at which the mouse position updates?
if(teabot.Direction == 'F'){
teabot.Velocity.x = cos(((float)teabot.Rotation - 90.0) / 180.0 * (float)PI)*50.0;
teabot.Velocity.y = sin(((float)teabot.Rotation - 90.0) / 180.0 * (float)PI)*50.0;
}
if(rClock.getElapsedTime().asSeconds() >= 1.0){
mReference.setPosition(teabot.Position);
rClock.restart();
}
//std::cout<<teabot.Direction<<teabot.Velocity.x<<", "<<teabot.Velocity.y<<std::endl; //Whenever the console window updates, the game window updates. This seems to make the game go faster somehow.
Update(&teabot, gameClock.getElapsedTime().asMicroseconds());
window.clear(sf::Color::Black);
window.draw(teabot.Visual);
window.draw(mReference);
window.display();
}
return 0;
}