sousou03 / glmatrix

Automatically exported from code.google.com/p/glmatrix
0 stars 0 forks source link

Change the processing order of arguments in the direction() function #53

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Let's take the relevant part of the direction function (before the 
normalization part):

/*
 * vec3.direction
 * Generates a unit vector pointing from one vector to another
 *
 * Params:
 * vec - origin vec3
 * vec2 - vec3 to point to
 * dest - Optional, vec3 receiving operation result. If not specified result is written to vec
 *
 * Returns:
 * dest if specified, vec otherwise
 */
vec3.direction = function(vec, vec2, dest) {
        if(!dest) { dest = vec; }

The provided documentation states that vec2 is the supposed destination (let's 
call it point B) while vec is the starting point (let's call it point A).

Let's call vec3.direction(pointA, pointB, dest)...

Given what we know about the arguments, I expect dest to contain a vector going 
from pointA to pointB, but if we look at the rest of the function we see it 
does this:

        var x = vec[0] - vec2[0];
        var y = vec[1] - vec2[1];
        var z = vec[2] - vec2[2];

this is like saying  dest = pointA - pointB

this produces a vector that is anti-parallel to the vector we want.

Example: 

        pointA = [0,0,0]; pointB = [4,0,0];
        var dest = vec3.create();
        vec3.direction(pointA, pointB, dest);
        console.log("dest = " + vec3.str(dest));

We want the vector going from pointA to pointB, so calling the 
vec3.direction(pointA, pointB, dest); method should produce the correct result 
of [1,0,0]

The result I see in the web console is this though:
[12:29:23.360] "dest = [-1, 0, 0]"

Original issue reported on code.google.com by Pana...@gmail.com on 12 Apr 2011 at 10:30