Double to float

I’m having issues with finding total velocity instead of having xVelocity and yVelocity. Would the correct strategy be to find the square root of the squares added, like with hypotenuses, or some other strategy? Here’s my code:


I’ve done a lot of testing and keep getting the error message the double doesn’t match with float. I just need a total velocity, but can’t figure out why the square root doesn’t work.

1 Like

The Math class uses doubles throughout and Processing uses floats so I suggest you try

playerVel = sqrt(playerVelX * playerVelX + playerVelY * playerVelY);

this assumes all the variables are declared as float. If you want to use the Math class then try

playerVel = (float)(Math.sqrt(playerVelX * playerVelX + playerVelY * playerVelY));

Avoid using pow function to calculate the square, a simple multiplication is much better.


Thank you so much! I’ll do that