Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: angle calculated but sprite doesn't rotate  (Read 1341 times)

0 Members and 1 Guest are viewing this topic.

Lauro

  • Newbie
  • *
  • Posts: 10
    • View Profile
angle calculated but sprite doesn't rotate
« on: January 29, 2020, 11:22:25 am »
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?

SFMLNewGuy

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: angle calculated but sprite doesn't rotate
« Reply #1 on: January 29, 2020, 11:43:49 am »
Have you tried something like this...

 
//helper function
template < typename T >
    const T toDegrees ( const T degrees )
    {
        return degrees * ( 180 / PI );
    }
//helper function
    template <typename T>
    const float getRotation ( T dx, T dy )
    {
        return toDegrees ( atan2 ( dy,dx ) ) + 180;
    }

// rotate the sprite
void lookAtTarget(){
    sf::Vector2f targetPos = getTarget()->getPosition();
    sf::Vector2f enemyPos = m_enemy->getPosition();

    float dx = enemyPos .x - targetPos .x;
    float dy = enemyPos .y - targetPos .y;

    m_enemy->setRotation( Math::getRotation( dx, dy ) );

}

 

Do you know about radians and degrees? Rotate I believe is the degrees relative to the sprite.

EDIT: Short little google:
Quote
"Degrees measure angles by how far we tilted our heads. ... or angle in radians (theta) is arc length (s) divided by radius (r).
A circle has 360 degrees or 2pi radians — going all the way around is 2 * pi * r / r.
So a radian is about 360 /(2 * pi) or 57.3 degrees."

You can also check out the source code for the book "Beginning Game Programming in C++" which as an arena game, that accomplished this task very easy.
« Last Edit: January 29, 2020, 11:50:38 am by SFMLNewGuy »

Lauro

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: angle calculated but sprite doesn't rotate
« Reply #2 on: January 29, 2020, 12:06:38 pm »
For what I know, read it somewhere some time ago, atan2 return the angle in radians, isn't so? Anyway, even if I don't set it in radians, a kind of rotation should be seen since the angle changes. Also I know that sfml method setRotation() must receive an angle in degrees.

EDIT: reading my own words i found the solution. The angle passed to set rotation was in radians lol.
so after I update dx and dy I run
angle /= RADIANS;
and it works well now... I pass to much time with these codes..
« Last Edit: January 29, 2020, 12:16:47 pm by Lauro »

 

anything