• ### Popular Now

• 14
• 12
• 9
• 10
• 13

This topic is 3936 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, in my shader i'm trying to compute the angle between two vectors, but i can't seem to figure out what i'm doing wrong. I was hoping that since this is supposed to be a fairly straightforward procedure someone could point out where i've gone wrong. I'm not too sure what the Cg functions throw out their results in (degrees? radians?) which further adds complication.
 float3 angle;
angle = dot(oNormal, up);
float modX; modX = length(oNormal);
float modY; modY = length(up);
oAngle = acos(angle / (modX * modY));
oAngle *= 57.2957795;  // degrees
oAngle /= 180.0f;	//should in theory produce a float between 0.0f and 1.0f



##### Share on other sites
I'm not sure if this'll help, but here's what I've got. Please note that I'm copying it and modifying it for display here, so this may or may not compile perfectly...

Without optimizations:
double magnitude(double v[3]){    return sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);}void normalize(double v[3]){    double m = magnitude(v);    v[0] /= m;    v[1] /= m;    v[2] /= m;}double dot(double v1[3], double v2[3]){    return (v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2]);}

So, I guess the version using the code above would be (note that this modifies the vectors passed in; probably not what you want!!):
double angle;normalize(vector1);normalize(vector2);// in radiansangle = acosf(dot(vector1, vector2));// in degreesangle = acosf(dot(vector1, vector2)) * (180 / PI);

The only thing I see is that I do the order a bit differently in normalizing the vectors before figuring the angle. As for the Cg functions, not a clue, so I can't help you there. :/

I know it's not much, but I hope it helps!

vr