okay this is driving me crazy. I want to rotate a sprite so that it follows the mouse cursor. I know there's quite a few posts on the subject ( I've looked through all that I could find) and my code seems correct so I don't know what to do.
The issue is when I implement the code all inside of main() it runs the way it should. When I put the code inside of a class though, it does this
http://youtu.be/03r05NjPoTAthe rotation doesn't follow the cursor all the way around the screen, nor does it track the angle at a consistent rate.
here's the original code that works:
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <math.h>
#include <iostream>
int main()
{
sf::Texture playerTexture;
playerTexture.loadFromFile("StickFig.png");
sf::Sprite playerSprite;
playerSprite.setTexture(playerTexture);
playerSprite.setOrigin(50,50);
playerSprite.setPosition(400,300);
playerSprite.setRotation(0);
sf::Vector2i mouse;
float angle;
sf::Vector2f playerPosition;
double a, b;
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML works!");
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
playerPosition = playerSprite.getPosition();
a = mouse.x - playerPosition.x;
b = mouse.y - playerPosition.y;
mouse = sf::Mouse::getPosition(window);
angle = -atan2( a , b) * 180 / 3.14;
playerSprite.setRotation(angle);
window.clear(sf::Color::White);
window.draw(playerSprite);
window.display();
}
return 0;
}
and here's the code using the class that is messing up:
main()
#include <SFML/Graphics.hpp>
#include "player.h"
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML works!");
while (window.isOpen())
{
player playerOne;
playerOne.initialize();
playerOne.loadContent();
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear(sf::Color::White);
playerOne.draw(window);
window.display();
}
return 0;
}
player.cpp
#include "player.h"
#include <iostream>
player::player()
{
//ctor
}
player::~player()
{
//dtor
}
void player::initialize()
{
playerSprite.setOrigin(50,50);
playerSprite.setPosition(400,300);
}
void player::loadContent()
{
playerTexture.loadFromFile("player.png");
playerSprite.setTexture(playerTexture);
}
void player::draw(sf::RenderWindow &window)
{
this->trackMouse(true, playerSprite, window);
window.draw(playerSprite);
}
void player::update(sf::RenderWindow &window)
{
}
void player::trackMouse(bool value, sf::Sprite &sprite, sf::RenderWindow &window)
{
if(value == true)
{
window.convertCoords(mouse);
//gets sprite origin coordinates and mouse coordinates
this->spritePosition = sprite.getPosition();
this->mouse = sf::Mouse::getPosition();
mouseAngle = -atan2( mouse.x - spritePosition.x , mouse.y - spritePosition.y) * 180 / 3.14159; //angle in degrees of rotation for sprite
playerSprite.setRotation(mouseAngle);
}
}
any help would just be fantasmical