Sign in to follow this  
gameplayprogammer

Getting a vector from an angle

Recommended Posts

I have two directional vectors A and B. I want to find a directional vector that is 30 degrees to the right of A. Using the dot product i have found the angle between A and B. I now want to find a directional vector that is at an angle of 30 degrees to the right of A. Any ideas?

Share this post


Link to post
Share on other sites
Ezbez    1164
Forget about B, it doesn't have anything to do with anything. Your problem seems simple enough, prehaps you should read up on matrices, since a rotation matrix would do the trick here. Google transformation matrices, and you'll find plenty on what you're doing.

Share this post


Link to post
Share on other sites
The sceanario i described above is a camera(B) looking at an object(A), as the object moves i would like the camera to stay with it but always at a position that is 30 degrees to the objects right. I have the camera following the object but the camera will move around and over the object instead of maintaining it's position at 30 degrees to the right of the object. How can you make this happen?

Share this post


Link to post
Share on other sites
TheAdmiral    1122
Ezbez is right - you should read up on vector arithmetic as this is a fairly simple problem. Just to get you started though:

If I understand you correctly, A and B are position vectors of your object and camera respectively and you want the position vector of a point that is 30°, as the camera looks, to the right of A, rotating about your vertical axis.
Just to be clear, I'll assume your position origin is (0,0,0) and 'up' is (0,1,0).

First calculate the difference vector from B to A:
X = A - B

Rotate this by 30° anticlockwise about your up vector:
X' = RX

where R is the rotation matrix:

cos(30) 0 sin(30)
0 1 0
-sin(30) 0 cos(30)


(I have assumed your trig functions work in degrees. In reality they probably take radians. If this 30° is constant, you may want to save the processor some work by observing that cos(30) = sqrt(3)/2 and sin(30) = 1/2)

Now X' is a vector from B to your target point. The angle between X and X' is 30°.
To turn this into the position vector for your camera target, add B again:

C = B [plus] X'.

As matrix arithmetic is associative, the process can be simplified into two operations, but I'll leave this to you as an exercise [wink].

Regards
Admiral

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this