SFML community forums

Help => General => Topic started by: noaboa on March 21, 2016, 07:49:22 pm

Title: Rotate against sprite
Post by: noaboa on March 21, 2016, 07:49:22 pm
Hi, I am trying to make a tower defence game. I have made the map, which loads files so I don't have to rewrite everything, an enemy that moves within the path, and bullets. Since I am making a tower defence game, I think I should rotate the tower against the enemy. What is the algorithm, or how should I do it?
Title: Re: Rotate against sprite
Post by: Perde on March 21, 2016, 08:02:07 pm
http://www.sfml-dev.org/tutorials/2.3/graphics-sprite.php

Press Ctrl + F and type "rotate".
Title: Re: Rotate against sprite
Post by: noaboa on March 21, 2016, 08:05:14 pm
I know how to rotate a sprite. I am wondering how do i rotate the tower towards the enemy that is moving
Title: Re: Rotate against sprite
Post by: Perde on March 21, 2016, 08:12:18 pm
Well, it's not moving during your calculation for the current frame, so you take the two positions and calculate the appropriate angle, which is really a matter of trigonometry and not programming.

It's been a while for me, but there's a plethora of links to be found on Google on this topic.
Does something like this help you?
http://stackoverflow.com/questions/2339487/calculate-angle-of-2-points
Title: Re: Rotate against sprite
Post by: noaboa on March 21, 2016, 08:15:05 pm
Yes, thanks, just had about IT in school
Title: Re: Rotate against sprite
Post by: noaboa on March 21, 2016, 08:53:59 pm
Does this seem right:


        float angle = atan2(this->getPosition().y - enemy->getPosition().y, this->getPosition().x - enemy->getPosition().x);
        float degrees = angle * 180 / PI;

        this->setRotation(degrees);
Title: Re: Rotate against sprite
Post by: Hapax on March 21, 2016, 11:47:40 pm
Does this seem right:
Have you tried it?
Title: Re: Rotate against sprite
Post by: noaboa on March 22, 2016, 12:04:16 am
I am currently using this code:

Code: [Select]
enemy->setOrigin(enemy->getGlobalBounds().width / 2, enemy->getGlobalBounds().height / 2);

float degrees = atan2(this->getPosition().y - enemy->getPosition().y, this->getPosition().x - enemy->getPosition().x) * 180 / PI;
degrees -= 90;

this->setRotation(degrees);

degrees -= 90;

if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space) && !this->spaceKey){
this->entityManager->Add("Bullet", new Bullet(this->map, this->getPosition().x, this->getPosition().y, degrees, 500));
}

But I don't think I should have to add the -90. Also how should I fix the bullets. As I am shooting and the enemy is moving up, it does not hit the enemy
Title: Re: Rotate against sprite
Post by: ka0s420 on March 22, 2016, 12:50:36 am
This is how i rotate the player. In some situations you need to add or subtract 90 depending on what direction your sprite is facing. The only time you dont add or minus 90 is when your sprite texture faces towards the right, which is where 0 degrees is in sfml.

float angle = atan2(player.y - sprite.getPosition().y, player.x - sprite.getPosition().x);
        angle = ((angle * 180) / 3.14159265) + 90;
        {
                angle = 360 - (-angle);
        }

        sprite.setRotation(angle);
 


and for a bullet for example, use the above to rotate it towards it's target and then :

float x = sprite.getPosition().x;
float y = sprite.getPosition().y;
float xR = sin((sprite.getRotation()*3.14159265 / 180)) * speed;
float yR = cos((sprite.getRotation()*3.14159265 / 180))* -speed;
sprite.setPosition(x + xR * dt, y + yR * dt);
 

to move towards the direction it's rotated towards. As for the bullets not hitting, either make them travel faster, or perhaps make sure that bullets and turrets update before the enemies otherwise the angle will usually be behind where the enemies are unless they are walking directly away from the turret. You could also try and base the turrets firing angle based on where the enemy will be before they're there, i.e a step ahead of them.