#Why is inverse square root helpful in normalizing vectors?

15 messages · Page 1 of 1 (latest)

glacial void
#

So an inverse square root, as I understand it, is a value that when multiplied by a square root, equals 1. sqrt(25) = 5 inverse_sqrt(25) = 1/5, 5*1/5 = 1

So in a two dimensional game, I have a vector (3,4), so if I do inverse_sqrt(3^2 + 4^2), I'll get 1/5.

But what I don't understand is why this helps. A normalized 2d vector should be a pair of coordinates where both x and y are between 0 and 1, and where the point they create lies on the unit circle. (If I understand correctly)

How does doing an inverse square root get you closer to a normalized vector?

gilded kite
#

you need the inverse to scale a and b so that the result becomes 1

#

the invese can just be applied by multiplication, instead of making a division

#

and I believe, multiplication is faster on a CPU than division.

gaunt shadow
#

and there are tricks to do fast inverse square root

gilded kite
#

a / sqrt(..) and b / sqrt(...) is 2 divisions, and x = 1/sqrt; a * x ... b * x is one.

gaunt shadow
#

one of which is old as hell, arcane, and mostly unused today, but nonetheless very clever:

float Q_rsqrt( float number )
{
    long i;
    float x2, y;
    const float threehalfs = 1.5F;

    x2 = number * 0.5F;
    y  = number;
    i  = * ( long * ) &y;                       // evil floating point bit level hacking
    i  = 0x5f3759df - ( i >> 1 );               // what the fuck? 
    y  = * ( float * ) &i;
    y  = y * ( threehalfs - ( x2 * y * y ) );   // 1st iteration
//    y  = y * ( threehalfs - ( x2 * y * y ) );   // 2nd iteration, this can be removed

    return y;
}
#

(comments are from original quake 3 source code)

gilded kite
#

(I like questions like these, keeps old knowledge fresh)

glacial void
#

So my normalized vector for vector (a,b) is a*inverse_sqrt(a), b*inverse_sqrt(b)?

gaunt shadow
#

no

#

you want a*inverse_sqrt(a^2+b^2), b*inverse_sqrt(a^2+b^2)

#

which can be written (a,b)*inverse_sqrt(a^2+b^2), because vector multiplication by a scalar is the same as multiplying each component of the vector by that scalar

glacial void
#

Thank you!