### Author Topic: Rotate against sprite  (Read 2482 times)

0 Members and 1 Guest are viewing this topic.

#### noaboa

• Newbie
• Posts: 14
##### Rotate against sprite
« 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?

#### Perde

• Jr. Member
• Posts: 63
##### Re: Rotate against sprite
« Reply #1 on: March 21, 2016, 08:02:07 pm »

#### noaboa

• Newbie
• Posts: 14
##### Re: Rotate against sprite
« Reply #2 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

#### Perde

• Jr. Member
• Posts: 63
##### Re: Rotate against sprite
« Reply #3 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.
http://stackoverflow.com/questions/2339487/calculate-angle-of-2-points

#### noaboa

• Newbie
• Posts: 14
##### Re: Rotate against sprite
« Reply #4 on: March 21, 2016, 08:15:05 pm »

#### noaboa

• Newbie
• Posts: 14
##### Re: Rotate against sprite
« Reply #5 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);

#### Hapax

• Hero Member
• Posts: 3078
• My number of posts is shown in hexadecimal.
##### Re: Rotate against sprite
« Reply #6 on: March 21, 2016, 11:47:40 pm »
Does this seem right:
Have you tried it?
Selba Ward - SFML drawables
Kairos - Timing Library
Rectangular Boundary Collision - Rectangular SAT Collision

#### noaboa

• Newbie
• Posts: 14
##### Re: Rotate against sprite
« Reply #7 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

#### ka0s420

• Jr. Member
• Posts: 56
##### Re: Rotate against sprite
« Reply #8 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.
« Last Edit: March 22, 2016, 12:57:16 am by ka0s420 »