SFML community forums

Help => General => Topic started by: DarthBuzzKill on November 17, 2014, 09:09:49 pm

Title: Rotating the Y Axis with the rotation of a Sprite (C++)
Post by: DarthBuzzKill on November 17, 2014, 09:09:49 pm
How would I go about doing this in SFML? So if I have a ship that rotates, it's missiles should fire based on it's direction. If I can't rotate the Y axis with the sprite, what other method could I use to achieve this?
Title: Re: Rotating the Y Axis with the rotation of a Sprite (C++)
Post by: Nexus on November 17, 2014, 09:23:58 pm
Why would you rotate the axis?

Just align the missile's velocity vector with the ship's direction vector. Maybe the Thor.Vectors (http://www.bromeon.ch/libraries/thor/v2.0/doc/group___vectors.html) module can help you, it contains many utility functions, e.g. to rotate, project or multiply vectors.

With Thor, you can get the direction from a rotation angle as follows (2 possibilities):
sf::Vector2f direction = thor::PolarVector2f(length, angle);
sf::Vector2f direction = thor::rotatedVector(sf::Vector2f(length, 0), angle);

Given a direction vector from a ship, the missile's velocity is then just a scaled version of it:
const float missileSpeed = ...;
sf::Vector2f shipDirection = ...;
sf::Vector2f missileVelocity = missileSpeed * thor::unitVector(shipDirection);

Of course, you can also do this in one step if you store rather a rotation angle than a direction vector in the ship.