SFML community forums

Help => Graphics => Topic started by: Raincode on May 01, 2014, 04:00:18 pm

Title: Rotate Tower towards enemy
Post by: Raincode on May 01, 2014, 04:00:18 pm
Greetings,

How do I test in an update function whether a sf::Vector2f is in a certain radius around another sf::Vector2f?
As I understand it, calculate the distance between them and check whether it is smaller or equal to the radius? Distance formula d = sqrt( (x2-x1)² + (y2-y1)² ) correct?

I kinda have problems managing all entities/towers etc. though. Say I detect an entity in radius, I still get problems finding/using the entity afterwards, like "Great, which one was it again?"

But then, how do I know how to rotate, so my tower faces a player given the players position and towers position? (I have a graphic which illustrates what I am trying to achieve).

So I have a tower (which has sprite) and a player (which has sprite) and I want to rotate the tower accordingly, while the player is in range (say I could maybe figure this part out by myself).

P.S.:
I have the thor vectorAlgebra2D library at hand, if it helps. Couldn't find a distance function though

Sorry for seeming like a "noob" in video game terms, I try to do as much as possible by myself, but sometimes you just can't get further for some reason

Raincode
Title: Re: Rotate Tower towards enemy
Post by: Dantez on May 01, 2014, 05:37:59 pm
Distance formula is correct. Here you have an example of rotation to another object:
sf::Vector2f playerPos = player.getPosition();
sf::Vector2f towerPos = tower.getPosition();

float angle = atan2(playerPos.y - towerPos.y, playerPos.x - towerPos.x) * 180 / 3.141;

tower.setRotation(angle);

Don't forget to include <math.h> and set origin of the 'tower' to the center of sprite or it will spin around point (0, 0).
Title: Re: Rotate Tower towards enemy
Post by: Nexus on May 01, 2014, 06:10:11 pm
P.S.:
I have the thor vectorAlgebra2D library at hand, if it helps. Couldn't find a distance function though
Both distance and rotation angle are computed very easily with Thor:
sf::Vector2f direction = enemyPos - towerPos;
float distance = thor::length(direction);
float angle = thor::polarAngle(direction);
Title: Re: Rotate Tower towards enemy
Post by: Raincode on May 02, 2014, 03:24:26 pm
Hi and thanks for the replies.

I get a segmentation fault and I don't exactely know why. So here's some code, in case something is obviously wrong

// I presume this is where it happens
void Tower::update(sf::Time dt)
{
    float angle = 0;
    for(auto& i : *mEntities)
    {
        angle = getInRange(i.getPosition());
    }
    mTopSprite.rotate(angle);
}

float Tower::getInRange(sf::Vector2f point)
{
    sf::Vector2f direction = point - getPosition();
    float distance = thor::length(direction);
    if(distance <= mRange)
        return thor::polarAngle(direction);
    return 0;
}
 

In my hpp there is this part (so you know all types)
// ...
private:
    EntityManager* mEntities;
    sf::Sprite mTopSprite;
    float mRange;
 

Hope someone knows what's going on there. The head of my for-loop seems wrong, but I'm not sure.
Till then, I'll try to find the problem myself

Kind Regards,
Raincode
Title: Re: Rotate Tower towards enemy
Post by: Nexus on May 02, 2014, 06:59:58 pm
Is mEntities correctly initialized? Have you already used a debugger?

And why are you overriding angle again and again?

By the way, it's a bad idea to return 0 in Tower::getInRange(), when there's no one in range. How do you differentiate it from the case where an enemy is in range at angle zero? You should use a different signature, maybe bool(sf::Vector2f, float&).
Title: Re: Rotate Tower towards enemy
Post by: Raincode on May 03, 2014, 11:42:12 am
Hi again,

Thanks for your reply.

The reason being I A have a rather large function already returning the actual angle instead of just a bool (which I had at first) is because I couldn't figure out how else to get the angle. All I am stuck with is a bool and still don't know, how to rotate the tower.

My thought on 0 was simply, if nothing is in range I don't have to rotate. Of course, know that I think about it, once I want to implement the tower firing, 0 can mean don't fire or keep shooting in same direction. So thanks for that hint

About the other things you mentioned - I will work on my code a little, change structure and such, look it over again

And thanks again, I appreciate the help a lot
Raincode