-
I'm in an odd situation right now:
//point playerRect at mouse pos
std::cout << mouseX << "\n";
dx = mouseX - playerX;
dy = mouseY - playerY;
trigresult = atan2(dy,dx) * 180/PI;
playerRect.setRotation(trigresult + 90);
For some reason, whenever I comment out the "std::cout << mouseX << "\n"", the trigresult only outputs 90 or -90 degrees with cout(so playerRect is either pointing straight up or straight down). And whenever it is there/isn't commented out, it works correctly and trigresult prints out correct results. Anyone know why this is?
Thanks in advance.
-
Usually such strange behavior comes from uninitialized variables/ undefined behavior.
Does mouseX always hold a valid value?
You need to provide a complete and minimal example.
-
int main()
{
//variables
float playerX = 400;
float playerY = 300;
float mouseX = 0.0;
float mouseY = 0.0;
double dx = 0;
double dy = 0;
float trigresult = 0.0;
//shapes
sf::RectangleShape playerRect;
playerRect.setSize(sf::Vector2f(BLOCK_SIZE,BLOCK_SIZE));
playerRect.setPosition(playerX,playerY);
playerRect.setOrigin(BLOCK_SIZE/2,BLOCK_SIZE/2);
playerRect.setTexture(&tex_playerRect);
//gameloop//////////////////////
while(bPlay == true)
{
//EVENTS//
while(window.pollEvent(event))
{
/////////////////////////////////////
//mouse
//check if mouse moved
if(event.type == sf::Event::MouseMoved)
{
mouseX = event.mouseMove.x;
mouseY = event.mouseMove.y;
}
}
//LOGIC//
//point playerRect at mouse pos
std::cout << mouseX << "\n";
std::cout << mouseX << "\n";
dx = mouseX - playerX;
dy = mouseY - playerY;
trigresult = atan2(dy,dx) * 180/PI;
playerRect.setRotation(trigresult + 90);
//RENDERING//
window.clear();
window.draw(playerRect);
window.display();
}
}
I hope this is enough code for you to help(also note that the first std::cout for mouseX didn't actually output anything to the console, but when I inserted it twice it worked ( ??? )
-
some quick modifications so your example would compile.. but I'm not exactly seeing what the problem you are having is. the rectangle follows my mouse and outputs ? also trig result is displaying correctly on my end?
http://i.imgur.com/axAAfBb.jpg
#include <iostream>
#include <cmath>
#include <SFML/Graphics.hpp>
int main()
{
//variables
float playerX = 400;
float playerY = 300;
float mouseX = 0.0;
float mouseY = 0.0;
double dx = 0;
double dy = 0;
float PI = 3.14;
float trigresult = 0.0;
int BLOCK_SIZE = 10;
sf::RenderWindow window(sf::VideoMode(1024, 768), "SFML works!");
window.setFramerateLimit(30);
//shapes
sf::RectangleShape playerRect;
playerRect.setSize(sf::Vector2f(BLOCK_SIZE,BLOCK_SIZE));
playerRect.setPosition(playerX,playerY);
playerRect.setOrigin(BLOCK_SIZE/2,BLOCK_SIZE/2);
//playerRect.setTexture(&tex_playerRect);
bool bPlay = true;
//gameloop//////////////////////
while(bPlay == true)
{
sf::Event event;
//EVENTS//
while(window.pollEvent(event))
{
/////////////////////////////////////
//mouse
//check if mouse moved
if(event.type == sf::Event::MouseMoved)
{
mouseX = event.mouseMove.x;
mouseY = event.mouseMove.y;
}
}
//LOGIC//
//point playerRect at mouse pos
//std::cout << mouseX << "\n";
//std::cout << mouseX << "\n";
dx = mouseX - playerX;
dy = mouseY - playerY;
trigresult = atan2(dy,dx) * 180/PI;
std::cout <<trigresult << std::endl;
playerRect.setRotation(trigresult + 90);
//RENDERING//
window.clear();
window.draw(playerRect);
window.display();
}
return 0;
}