I'm having a problem where one of my functions are resulting in nan and inf values. Here is the offending function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
void Ship::SetThrustSpeed(float ThrustSpeed)
{
/*
* Imagine a triangle ABC. Direction is the measure of angle A,
* ThrustSpeed is the measure (in pixels) of segment AC, and also the
* hypotenuse. SpeedX is the measure of segment BC and SpeedY is
* the measure of segment AB. We need to find SpeedX and SpeedY, so we
* get the sine of Direction to find the ratio of SpeedX to
* ThrustSpeed, then we multiply it by ThrustSpeed (because it's SpeedX
* over 1), to get SpeedX. SpeedY is just a matter of Pythagorean
* Theorem.
*/
float tmp = std::sin(Direction);
SetSpeedX(ThrustSpeed * tmp);
SetSpeedY(std::sqrt((ThrustSpeed * ThrustSpeed) /
(GetSpeed().x * GetSpeed().x)));
}
<cmath> is included.
The function takes a ThrustSpeed and translates it into a SpeedX and SpeedY based on Direction, which is an angle. Direction is a private member of Ship.
SetSpeedX and SetSpeedY are not doing anything but setting the values.
GetSpeed returns an sf::Vector2f, which is just a vector holding 2 floats: x and y.
After executing this function (ThrustSpeed = 50, Direction = 45), SpeedX is set to 0, and SpeedY is set to inf.