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

Author Topic: Rotation 1.6  (Read 785 times)

0 Members and 1 Guest are viewing this topic.

Tyrfing

  • Newbie
  • *
  • Posts: 1
    • View Profile
Rotation 1.6
« on: April 08, 2013, 07:41:04 pm »
I am creating a tower defense game, I create the tower and the enemy and I want the tower to shoot at the enemy, I am rotating the bullet to face the enemy based on the position of the tower using this code right after creating the bullet

Bullets Temp;
Temp.SetX(TowerVector[a].GetX());
Temp.SetY(TowerVector[a].GetY());
BulletVector.push_back(Temp);

sf::Vector2f curPos = TowerVector[a].Sprite.GetPosition();
sf::Vector2f position =EnemyVector[b].Sprite.GetPosition();

const float PI = 3.14159265;

float dx = curPos.x - position.x;
float dy = curPos.y - position.y;

float rotation = (atan2(dy, dx)) * 180 / PI + 180;

BulletVector[BulletVector.size()].Sprite.SetRotation(rotation);
 
Its causes my game to break when it should shoot, is there anything wrong with the rotation code or is it something else?
« Last Edit: April 08, 2013, 07:45:35 pm by Tyrfing »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Rotation 1.6
« Reply #1 on: April 08, 2013, 08:38:26 pm »
BulletVector[BulletVector.size()] is not a valid element. It is 1 past the last one.
Laurent Gomila - SFML developer

 

anything