# Making two vectors parallel

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

## Recommended Posts

Hello,
i am stuck at some basic xna math.I have two 3D vectors .The first vector is pointing in some direction and the second one is pointing in another direction.Now i want to make the second vector point in the same direction as the first one,hence making both vectors parallel.Please suggest a built in method in xna or a way i can accomplish this?.Thank you

##### Share on other sites
What do you mean by that? 2 vectors are parallel when they are proportionals, like (x, y, z) and (2x, 2y, 2z). just multiply the first by the factor of the length of both. Edited by Such1

##### Share on other sites
v2 = v1;

now the second vector points in the same direction as the first.

##### Share on other sites
The question makes sense only if you're thinking vectors as being some kind of "entities" of their own identity. Therefore the (mathematically correct) answers given above can seem a bit silly.

If you want to rotate vector v1 so that it faces to the same direction of v2, you should do as Such1 said, and construct a new vector that points to the direction of v2, but has the length of v1, i.e. the vector you are looking for is
[code]
vector newVector = v2 * (v1.length / v2.length);
[/code]

Remember to take account the special case when v2.length == 0, if that can happen in your context.

##### Share on other sites
First of all ,Thank you for your replies.

I think I have made a mistake in describing my problem.Actually I am implementing a chase camera.I have two classes,the camera class and the object class(the 3D object the camera is chasing). Initially the camera is behind the object as some predefined offset.Now as I rotate the object its forward vector rotates along y-axis.In the camera class I have a camera position and a camera offset.The camera position is determined as "[b]camera position=object position+camera offset[/b]".Now in order to keep the camera always behind the object I want to rotate the offset so that its parallel to object forward and then to get the new position of the camera I use the above equation(with the new transformed offset).What I need is a way or built in function that rotates the offset to object's forward every time I rotate my object.Thank you

My vector math has become a little rusty over the years of "no practice".So plz bear with me:).Thank You for your help Edited by neogeocg

##### Share on other sites
I think it's just as others have described. The camera offset vector is the object's forward vector, normalized, negated (assuming the camera is "behind" the object) and then scaled by the desired distance between the object and the camera.

##### Share on other sites
[quote name='neogeocg' timestamp='1351602555' post='4995390'] "[b]camera position=object position+camera offset[/b]"
[/quote]

so, it'd look like: camera.position = object.position + object.forwardVector*(camera.offset.length/object.forwardVector.length);

##### Share on other sites

"[b]slicer4ever[/b]" what you wrote worked but (even though the camera was behind the object) I had to explicitly add an offset value to the Y component of camera position to keep the camera up because it was at the same level as the object.I will really appreciate if you explain this "[b]camera.position = object.position + object.forwardVector*(camera.offset.length/object.forwardVector.length)[/b]" to me.Thank you

##### Share on other sites
Your object has a set of transformations, part of which indicates what direction it is facing. The code that slicer4ever present takes this facing direction (a vector is basically a direction) and calculates a new spot on the line that the direction describes, and places the camera there. The reason you had to translate your camera vertically is because the object's forward vector points forward only, so going backward along it will result in a camera location sitting on the same elevation as the object. To go above the object's head does require the vertical translation.

To break down that equation, you have the object's position. You have its forward-facing direction vector. Any point on the line defined by the vector can be calculated as an offset from the object's point. The generalized form for this is:

[code]
point = p1 + t*vector
[/code]

where t determines the distance from the point p1; in slicer4ever's post, t=camera.offset.length. This form assumes that the vector is unit length. If the vector is not unit-length, then you can divide t by the length of the vector (which is really actually the same as dividing vector by its length, which is the operation typically used to normalize a vector to unit length). Failing to normalize to unit length can result in the camera being either closer or further away than you expect it to be based on its offset.

Once you have the camera at the right distance behind the player, then you can translate the camera vertically to put it above.