I'm trying to follow this Actionscript tutorial for homing missiles, translating it to C++ with SFML.
But I seem to have a problem with this piece of code:
In Actionscript:
var rotation:int = Math.atan2(targetY, targetX) * 180 / Math.PI;
if (Math.abs(rotation - missile.rotation) > 180)
{
if (rotation > 0 && missile.rotation < 0)
missile.rotation -= (360 - rotation + missile.rotation) / ease;
else if (missile.rotation > 0 && rotation < 0)
missile.rotation += (360 - rotation + missile.rotation) / ease;
}
else if (rotation < missile.rotation)
missile.rotation -= Math.abs(missile.rotation - rotation) / ease;
else
missile.rotation += Math.abs(rotation - missile.rotation) / ease;
Translated in C++ with SFML:
int current = rocket_sprite.getRotation();
if( abs( rotation - current ) > 180 )
{
if( rotation > 0 && current < 0 ) rotation -= ( 360 - rotation + current ) / ease;
else if( current > 0 && rotation < 0 ) rotation += ( 360 - rotation + current ) / ease;
}
else if( rotation < current ) rotation -= abs( current - rotation ) / ease;
else rotation += abs( rotation - current ) / ease;
With this code, the missile acts crazy, getting stuck in places, continually rotating in place for no reason and other stuff like that.
I suspect that the problem lies in the fact that .getRotation() returns absolute values, but I don't know how to even test it.
Here is my complete code:
#include <math.h>
#define PI 3.14159265
class rocket
{
private:
sf::Texture rocket_texture;
sf::Sprite rocket_sprite;
sf::Vector2f position;
int rotation;
public:
rocket();
bool rocket_load();
void rocket_show( sf::RenderWindow * window );
void set_position( sf::Vector2f new_position );
void rocket_rotate( sf::RenderWindow * window );
void rocket_move();
};
rocket::rocket()
{
rotation = 0.f;
}
bool rocket::rocket_load()
{
rocket_texture.loadFromFile( "rocket.png" );
rocket_sprite.setTexture( rocket_texture );
return true;
}
void rocket::rocket_show( sf::RenderWindow * window )
{
rocket_sprite.setOrigin( rocket_texture.getSize().x / 2 , rocket_texture.getSize().y / 2 );
rocket_sprite.setPosition( position );
window->draw( rocket_sprite );
}
void rocket::set_position( sf::Vector2f new_position )
{
position = new_position;
}
void rocket::rocket_rotate( sf::RenderWindow * window )
{
sf::Vector2i mouse_position = sf::Mouse::getPosition( *window );
int targetx = mouse_position.x - position.x;
int targety = mouse_position.y - position.y;
rotation = atan2( targety , targetx ) * 180 / PI;
int ease = 5;
int current = rocket_sprite.getRotation();
if( abs( rotation - current ) > 180 )
{
if( rotation > 0 && current < 0 ) rotation -= ( 360 - rotation + current ) / ease;
else if( current > 0 && rotation < 0 ) rotation += ( 360 - rotation + current ) / ease;
}
else if( rotation < current ) rotation -= abs( current - rotation ) / ease;
else rotation += abs( rotation - current ) / ease;
rocket_sprite.setRotation( rotation );
}
void rocket::rocket_move()
{
int speed = 5;
float speedx = 0;
float speedy = 0;
speedx = speed * ( 90 - abs( rotation ) ) / 90;
if( rotation < 0 ) speedy = -speed + abs( speedx );
else speedy = speed - abs( speedx );
position.x += speedx;
position.y += speedy;
}
If anyone can point me to the right direction, I would really appreciate it. :-)
Thank you in advance. :-)