I have a sprite that has to chase a moving target. I calculate the angle of the vector between target and the sprite.
The chasing object is created with a angle = -90;
In every frame I calculate the angle and I update the position this way
dx=0;
dy=0;
destination = spaceship.getSprite().getPosition();
angle = (float)(atan2((destination.y-sprite.getPosition().y), (destination.x-sprite.getPosition().x)));
if(sprite.getPosition().x != destination.x && sprite.getPosition().y != destination.y){
dx=(float)(cos(angle)*ALIEN_ACCEL);
dy=(float)(sin(angle)*ALIEN_ACCEL);
}
xPos += dx;
yPos += dy;
The draw method is written this way
sprite.setOrigin(sprite.getLocalBounds().width/2,sprite.getLocalBounds().height/2);
sprite.setPosition(xPos, yPos);
sprite.setRotation(angle+90);
gameWindow.draw(sprite);
The chasing object chase (sorry) well the target one, but it keeps the same angle, it never rotate, I mean, it follows the target but the rotation remains the same.
The target object, which is the player, rotate this way: if the left key is pressed it call a method that does
angle -= bend_factor;
, while if the right key is pressed the method will do
angle += bend_factor;
then the same draw method is called and the rotation is done well (both the player class and the chasing one extend the same class, so the draw method is the same.
What am I missing?