Vector normalization gives very imprecise results

Posted by Kipras on Game Development See other posts from Game Development or by Kipras
Published on 2013-10-19T18:47:10Z Indexed on 2013/10/19 22:17 UTC
Read the original article Hit count: 139

Filed under:
|
|

When I normalize vectors I receive very strange results. The lengths of the normalized vectors range from 1.0 to almost 1.5. The functions are all written by me, but I just can't find a mistake in my algorithm. When I normalize I just divide all components of the vector by the vector's length.

public double length(){
    return Math.sqrt(x*x + y*y);
}

public void normalize(){
    if(length() > 0){
        x /= length();
        y /= length();
    }
}

Is this supposed to happen? I mean I can see the length ranging from 0.9 to 1.1 at worst, but this is just overwhelming.

Cheers

© Game Development or respective owner

Related posts about java

Related posts about math