Archived

This topic is now archived and is closed to further replies.

directrix

vector projection question

Recommended Posts

I''ve been working on some code to project one vector onto another, however, I only get a correct result if the two vectors used in the projection are in the same quadrant. Do the two vectors have to be in the same quadrant or am I doing something wrong? digital radiation

Share this post


Link to post
Share on other sites
Thanks for the reply. I guess I'll post the code. This is so simple, I can't figure out what I'm doing wrong.


struct vec3
{
float x, y, z;
};

// projection of A onto B

vec3 projectVector(vec3& A, vec3& B)
{
vec3 result;

float AdotB = A.x*B.x + A.y*B.y + A.z*B.z;
float magBSq = B.x*B.x + B.y*B.y + B.z*B.z;

float coeff = AdotB / magBSq;

result.x = B.x * coeff;
result.y = B.y * coeff;
result.z = B.z * coeff;

return result;
}


As an example of the problem, suppose I have the following:

vector A = <2,3,4>
vector B = <-1,-1,1>

I expect the result to be a vector in the direction of B with a length of |A.B| / |B| (assume the "." is the dot product)

Instead I get, <0.33333,0.33333,-0.33333>
The length of the vector is correct (it's 0.57735), but the direction is wrong.


digital radiation

[edited by - directrix on May 26, 2004 11:06:20 PM]

Share this post


Link to post
Share on other sites
You're doing it right. The vector is on the same line as b as it should. It's going in the negated direction b/c that's where the projection lies, if you visualize the vectors starting from the origin, the angle between them will be greater than 90 degrees. Try printing the angle between them and see. A vector projected onto a vector that lies more than 90 degrees away will project onto that vector's negation , that is to say, take be and negate the sign of every component. This is now consistent with your result. As you can see b's x and y components are negative while z is positive. Now, b/c the projection is on it's opposite vector the signs are negated: x and y are positive and z is negative. Your function is perfect

edit: to make it more clear:

_
/| A
/
/ theta < 90
___.--------->B

period at projection of a onto b. If angle btw is pi/2 rads
then the vector is 0, if it's greater than 90,
then the project vector is on B in the opposite direction:
_
|\A
\ theta > 90
.__\---------->B

period is proj(a, b)


To make sure the angle btw your given vectors A and B,

vector A = <2,3,4>
vector B = <-1,-1,1>

is Greater than 90 degrees you can do a simple dot product btw them. Since the alternitive def of dot product is |A||B|cos(t), where t is angle between, if the result is negative, meaning the radius of a unit circle was in the 2nd or 3rd quadrant, or the adjacent side of the triangle formed is a negative vector, then you know the angle is greater than 90. Again, if it's 0, then they're perpendicular, and if it's positive then they form an acute angle.

A.B = 2(-1) + 3(-1) + 4(1) = -1


[edited by - temp_ie_cant_thinkof_name on May 26, 2004 12:04:31 AM]

Share this post


Link to post
Share on other sites