Hi all. I'm making a little 2D tank game, it works relatively to where you click on the screen. So, when you click on the screen it grabs a current location, desired location, does some ATAN2, normalisation and works out where the click was located, how much to rotate by and then when to shoot.
The tank moves by the perfect amount, stops rotating and then fires it projectile to that position.
I have on issue. The rotation of the projectile isn't correct for where the rotation of the tank sprite is. I know the issue, but I'm not quite sure on how to fix it.
At the moment the projectile is being rotated on the top left corner (Default position). I need some help on how to change it to the bottom middle position, this will make the sprite rotate correctly when I call the rotate function.
How can I accomplish this?
Any help would be really, really helpful!
Thanks
-- EDIT --
Code
Projectile class
void projectile::init()
{
laserShape.setTexture(*laserTexturePointer);
laserShape.setOrigin(laserShape.getLocalBounds().width/2.0f, laserShape.getLocalBounds().height/2.0f);
laserShape.setPosition(580,280);
}
void projectile::input(sf::RenderWindow &window)
{
p.current.x = laserShape.getPosition().x - laserShape.getGlobalBounds().width/2.f;
p.current.y = laserShape.getPosition().y - laserShape.getGlobalBounds().height/2.f;
p.desired.x = sf::Mouse::getPosition(window).x;
p.desired.y = sf::Mouse::getPosition(window).y;
laserShape.setRotation(p.rotateToTarget(p).x);
//std::cout<<"X Rotation Amount :"<<p.rotateToTarget(p).x<<std::endl;
}
void projectile::update()
{
laserShape.move(p.fireTo(p).x, p.fireTo(p).y);
}
void projectile::draw(sf::RenderWindow &window)
{
window.draw(laserShape);
}
Rotation function
vector2D position::rotateToTarget(position p)
{
delta.x = desired.x - current.x;
delta.y = desired.y - current.y;
rotationAmount = atan2(delta.x, delta.y)*180/PI;
return rotationAmount;
}
How I call it all
if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
bullet.input(window);
bulletVec.push_back(bullet);
std::cout<<bulletVec.size()<<std::endl;
}
Then in my update function I just iterate over my bulletObject vector to call the projectile::update() function.
The code it pretty simple, not much happening at the moment.