Personally, you're doing it the hard way.
Try vector math! Makes stuff like this much easier.
For example:
// constants
const sf::Vector2f CENTER = static_cast<sf::Vector2f>(window.getSize() / 2u);
constexpr float LENGTH = 35.f;
constexpr float VELOCITY = 122.f;
// the projectile
sf::VertexArray line(sf::Lines, 2);
// calculate projectile direction based off: mouse press location and center of window
sf::Vector2f mousePos = {static_cast<float>(event.mouseButton.x), static_cast<float>(event.mouseButton.y)};
sf::Vector2f projEndNorm = vecNormal(mousePos - CENTER);
line[0].position = CENTER + projEndNorm; // offset from the center slightly so: line[0].position - CENTER != {0, 0}
line[1].position = projEndNorm * LENGTH + CENTER;
// update
line[0].position += vecNormal(line[0].position - CENTER) * VELOCITY * dt.asSeconds();
line[1].position += vecNormal(line[1].position - CENTER) * VELOCITY * dt.asSeconds();
// the vecNormal function:
sf::Vector2f vecNormal(const sf::Vector2f& vec)
{
if(vec.x != 0 && vec.y != 0)
return vec / std::sqrt(vec.x * vec.x + vec.y * vec.y); // vector / length = unit vector of the vector
else
return {0, 0};
}
Unit Vector Definition
Minimal and Naive Example if you need it
Awesome thanks! I'll try that tomorrow and use your naive guide as a reference to recreate it.
However I have some questions beforehand, for the things I haven't encountered yet:
const sf::Vector2f CENTER =
static_cast<sf::Vector2f>(window.getSize() /
2u);
What is static_cast used for, it's a type of conversion from what I understand but what difference does it do to using ( sf::Vector2f ), or does that not work?
is 2u supposed to be something specific or just half the window size?
constexpr float LENGTH = 35.f;
From what I understand constexpr is like const used for objects and functions, does it have purpose for a simple data type?
sf::Vector2f mousePos = {static_cast<float>(
event.mouseButton.x), static_cast<float>(
event.mouseButton.y)};
This captures the position of the mouse as the mousebutton is pressed and doesn't update right?
As for using CENTER as a whole, what does it do? I assume that's why you don't need to calculate angles from the player.
I probably should just read up a lot more til I understand, but vectors, are they just positions for dots right, with x, y , z, etc and the normal is the product of two or more vectors, or does vectors always have a magnitude / normal include together with the position?