# vectors Problem (SOLVED)

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

## Recommended Posts

hi guys check this pic please on this address i want to know the angle between 2 vectors v1,v2 and v1,v3 . i am using the following rule : angle= math.Acos(vector3.Dot(v1,v2)/(v1.length()*v2.length())); to calculate the 2 angles and i am getting the same result 45 but the angles are 45 and -45 so how i could know if the angle is positive or negative ??? http://tinypic.com/fxtpow.jpg [Edited by - jad_salloum on November 28, 2005 3:55:09 AM]

##### Share on other sites
In 3D space it would be hard to call one of those angles as -45 degrees and the other positive. But if they are like they appear on screen (ie in 2D space and with v1 pointing up--with a value like (0, 1)), then you can do something like this:
Let vx = (1, 0);if (vector3.Dot(vx,v2) >= 0){    // v2 is in the +ve direction}else{    // v2 is in the -ve direction}

##### Share on other sites
well, i don't think acos() will be any help since the cosine doesn't change when you negate the angle. This formula is often used when you want to have a simple measure of how far apart two vectors are, from actually coinciding...

Let vector v1=(v1x, v1y) and v2=(v2x, v2y), then the angle from V2 TO V1 is

phi = atan((v2y-v1y)/(v2x-v1x))
This returns negative angles when it should.
As usual, angles are positive when measured counter-clockwise
If you need this for 3d vectors, let me know. This won't work at all in 3D.
Just keep in mind that you'll need a more strict system of reference and two angles instead of one.
I hope this helps.

##### Share on other sites
Just to reinforce what has been said, in 3d you need a reference to determine the sign of the angle. For example, you could do something like this:
float angle = atan2(length(cross(vec1,vec2)),dot(vec1,vec2));if (dot(cross(v1,v2),reference) < 0.0f) angle = -angle;

##### Share on other sites

i am working in 3d space , what i am really doing is that i am moving a car along a cubic bezier curve acoording to a scalar which varies between 0 and 1 but my car is not taking the correct direction of the path so how can i solve this problem ??

##### Share on other sites
Quote:
 i am working in 3d space , what i am really doing is that i am moving a car along a cubic bezier curve acoording to a scalar which varies between 0 and 1 but my car is not taking the correct direction of the path so how can i solve this problem ??
I know you're working in 3d, but does the curve lie in a plane? (e.g. a road or something...) And if not, what sort of path does it take, that is, are there any restrictions on where the curve can go? The answers to those questions would help in determining the best method to use for orienting the car along the curve...

##### Share on other sites
no the curve don't lie in a plane coz it is for a race stage and the curve goes up and down also . and it takes curved paths using cubic siplines ( about 30 Curve ) and the car should get far from the path by a constant distance to it's right or left as maximum hope this helps

##### Share on other sites
Quote:
 Original post by jad_salloumno the curve don't lie in a plane coz it is for a race stage and the curve goes up and down also . and it takes curved paths using cubic siplines ( about 30 Curve ) and the car should get far from the path by a constant distance to it's right or left as maximum hope this helps

Do you have a way to extract the normal to the road surface? if you do, then there might be a way to get the sense of the angle:

Let n be the normal vector, v1 and v2 are your vectors as usual.

int factor = sign ((v1 x v2) . n);

sign(...) returns 1 if the argument is positive, -1 if negative, 0 if 0. 'x' is the cross product, '.' is the dot product.

factor will be 1 here if v2 is to the left of v1 when you are looking down on the road. Please note that this hasn't been tested or anything--just an idea, but it should work as far as I can see now.

/edit: I'm becoming a retard ... you don't need the signum function at all! Just check if ((v1 x v2) . n) is positive. [smile]

[Edited by - deavik on November 23, 2005 6:32:51 AM]

##### Share on other sites

http://www.gamedev.net/community/forums/topic.asp?topic_id=359774

Have you already noticed that? It doesn't deal with how to compute the angle correctly but how to compute a suitable rotation.

##### Share on other sites
It looks like you've got two discussions going on this, but have you considered creating a coordinate frame from the cruve tangent and a fixed up vector? This would also give you a 'side' vector, perpendicular to the curve, that you could use to move the car right or left relative to the track.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 23
• 12
• 12
• 11
• ### Forum Statistics

• Total Topics
634779
• Total Posts
3019240
×