**1**

8 replies to this topic

###
#1
Members - Reputation: **111**

Posted 29 October 2012 - 04:29 PM

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

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

Sponsor:

###
#4
Members - Reputation: **1842**

Posted 30 October 2012 - 05:13 AM

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

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

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.

Me+PC=clb.demon.fi | C++ Math and Geometry library: MathGeoLib, test it live! | C++ Game Networking: kNet | 2D Bin Packing: RectangleBinPack | Use gcc/clang/emcc from VS: vs-tool | Resume+Portfolio | gfxapi, test it live!

###
#5
Members - Reputation: **111**

Posted 30 October 2012 - 07:09 AM

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 "

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 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 youMy 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, 30 October 2012 - 07:22 AM.**

###
#6
Crossbones+ - Reputation: **5596**

Posted 30 October 2012 - 01:23 PM

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.

###
#7
Crossbones+ - Reputation: **4667**

Posted 30 October 2012 - 01:32 PM

"

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.

###
#8
Members - Reputation: **111**

Posted 31 October 2012 - 11:55 AM

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###
#9
Crossbones+ - Reputation: **3129**

Posted 31 October 2012 - 01:37 PM

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:

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.

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.