Home > Net >  no operator "/" matches these operands
no operator "/" matches these operands

Time:06-18

trying to make player shoot 360 is there something wrong or misspelled?

#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>



using namespace sf;

int main()
{
    RenderWindow window(VideoMode(800, 600), "360 shooting object");

    CircleShape circle(25.f);
    circle.setFillColor(Color::White);

    Vector2f circleCenter;
    Vector2f mousePosWindow;
    Vector2f aimDirection;
    Vector2f aimDirectionNorm;

    while(window.isOpen())
    {
        Event event;
        while(window.pollEvent(event))
        {
            if(event.type==Event::Closed) window.close();
            if(Keyboard::isKeyPressed(Keyboard::Escape)) window.close();
        }

        draw(window, circle);

        update(window, circle, circleCenter, mousePosWindow, aimDirection, aimDirectionNorm);

    }
    return 0;
}

void update(RenderWindow &window, CircleShape &shape, Vector2f circleCenter, 
    Vector2f mousePosWindow, Vector2f aimDirection, Vector2f aimDirectionNorm)
{
    circleCenter = Vector2f(shape.getPosition().x   shape.getRadius(), 
        shape.getPosition().y   shape.getRadius());
    mousePosWindow = Vector2f(Mouse::getPosition(window));
    aimDirection = mousePosWindow - circleCenter;
    aimDirectionNorm = aimDirection / sqrt(pow(aimDirection.x, 2))   sqrt(pow(aimDirection.y, 2));

}

im using sfml error at the aimDirectionNorm part no operator matches the '/' operand what wrong with the '/' operator i don't understand

i delete some code i

CodePudding user response:

Firstly your math is wrong

aimDirectionNorm = aimDirection / sqrt(pow(aimDirection.x, 2))   
    sqrt(pow(aimDirection.y, 2));

should be

aimDirectionNorm = aimDirection / sqrt(pow(aimDirection.x, 2)   
    pow(aimDirection.y, 2));

Secondly operator/ requires a Vector2f and a float but sqrt returns a double. Because Vector2f is a template the normal double to float conversion does not happen.

Simple way to get a float would be to use sqrtf and powf

aimDirectionNorm = aimDirection / sqrtf(powf(aimDirection.x, 2)   
    powf(aimDirection.y, 2));
  • Related