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

Author Topic: Rotating the Y Axis with the rotation of a Sprite (C++)  (Read 2437 times)

0 Members and 1 Guest are viewing this topic.

DarthBuzzKill

  • Newbie
  • *
  • Posts: 7
    • View Profile
Rotating the Y Axis with the rotation of a Sprite (C++)
« 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?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Rotating the Y Axis with the rotation of a Sprite (C++)
« Reply #1 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 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.
« Last Edit: November 17, 2014, 09:26:04 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything