SFML/SFML

`operator*` overload in sf::Vector2<T> and sf::Vector3<T> template class

alberto-cardini opened this issue · 2 comments

Prerequisite Checklist

Describe your feature request here

operator* overload in sf::Vector2 and sf::Vector3 template class

Now is available only the Vector and scalar product. Would be nice to also implement dot product between same dimensions vectors.

Use Cases

It can be useful to do calculations between vector without needing to access the single components. It is often used to do vector calculations in physic evaluations.
So it would be faster for the programmer to implement calculations with dot product and would also be less verbose and more pragmatic.

API Example

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode({1280, 720}), "Minimal, complete and verifiable example");
    window.setFramerateLimit(60);
    sf::Vector2f A(10,-10);
    sf::Vector2f B(-10,10);
    sf::Vector2f C = A * B;
    // instead of doing C = sf::Vector2f(A.x * B.x, A.y * B.y);
    while (window.isOpen())
    {
        while (const auto event = window.pollEvent())
        {
            if (event.is<sf::Event::Closed>())
                window.close();
        }

        window.clear();
        window.display();
    }
}

Pretty sure I've seen this discussion before and the usual issues of the intent of * for vectors not making sense when written. I think this is a ill-fitting use of the operator *. Component-wise multiplication is something the new extension methods already cover for SFML and it's much more clear at a glance what vec.cwiseMul(otherVec) means, whereas vec * otherVec does not convey the meaning well.

As @Bambo-Borris mentioned, * can have double meanings for vectors, which is why we've implemented both function as separate methods and refrained from using the operator*.