Making two vectors parallel

Started by
7 comments, last by FLeBlanc 11 years, 6 months ago
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
Advertisement
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.
v2 = v1;

now the second vector points in the same direction as the first.
"There will be major features. none to be thought of yet"
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

vector newVector = v2 * (v1.length / v2.length);


Remember to take account the special case when v2.length == 0, if that can happen in your context.
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 "camera position=object position+camera offset".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
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.
"camera position=object position+camera offset"


so, it'd look like: camera.position = object.position + object.forwardVector*(camera.offset.length/object.forwardVector.length);
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.
Thank you all for replying

"slicer4ever" 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 "camera.position = object.position + object.forwardVector*(camera.offset.length/object.forwardVector.length)" to me.Thank you
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:


point = p1 + t*vector


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.

This topic is closed to new replies.

Advertisement