Add handling of zero length vector to normalization methods

- Avoids having division by zero
This commit is contained in:
Jan 2021-09-11 17:49:32 +02:00
parent d71dfd170b
commit d96780a88e

View File

@ -739,19 +739,25 @@ namespace Game
{
const float length = std::sqrt((vec[0] * vec[0]) + (vec[1] * vec[1]));
vec[0] /= length;
vec[1] /= length;
if(length > 0.0f)
{
vec[0] /= length;
vec[1] /= length;
}
return length;
}
float Vec3Normalize(vec3_t& vec)
{
const float length = static_cast<float>(std::sqrt(std::pow(vec[0], 2) + std::pow(vec[1], 2) + std::pow(vec[2], 2)));
const float length = std::sqrt(std::pow(vec[0], 2.0f) + std::pow(vec[1], 2.0f) + std::pow(vec[2], 2.0f));
vec[0] /= length;
vec[1] /= length;
vec[2] /= length;
if(length > 0.0f)
{
vec[0] /= length;
vec[1] /= length;
vec[2] /= length;
}
return length;
}