Programming 2D Games
http://programming2dgames.com/forum/

Windows Numerics normalize function bug.
http://programming2dgames.com/forum/viewtopic.php?f=6&t=441
Page 1 of 1

Author:  profkelly [ Thu Mar 21, 2019 4:50 pm ]
Post subject:  Windows Numerics normalize function bug.

The normalize function in Windows::Foundation::Numerics does not work correctly with very small numbers. It returns infinity in both terms of the vector. I corrected the issue like this:

Code:
//=============================================================================
// Normalize the vector.
//=============================================================================
inline void Normalize(Vec2 &v)
{
    if (std::abs(v.x) <= EPSILON)   // Set very small numbers to 0 to avoid
        v.x = 0;                    // problems with Numerics::normalize().
    if (std::abs(v.y) <= EPSILON)
        v.y = 0;

    v = Windows::Foundation::Numerics::normalize(v);
    // If normalize function returns NaN (Not a Number) for X or Y
    // component in vector.
    if (v.x != v.x)     // If v.x is Nan
        v.x = 0;        // Force to 0
    if (v.y != v.y)     // If v.y is Nan
        v.y = 0;        // Force to 0
}

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/