/// <summary> /// Adds the amount to the vector based on the angle. This basically keeps the ship part at the same position on the ship as the ship rotates. /// </summary> public static Vector3 AddToVector(Vector3 OrigionalVector, float angle, Vector3 amountToAdd) { //TODO: Make this work with all 3 axies. Vector3 returnVector = new Vector3(); returnVector.X = OrigionalVector.X + (float)Math.Cos(angle) * amountToAdd; returnVector.Y = OrigionalVector.Y + (float)Math.Sin(angle) * amountToAdd; return returnVector; }

# Add to Vector based on Angle

Page 1 of 1## 10 Replies - 5891 Views - Last Post: 30 July 2012 - 03:25 PM

### #1

# Add to Vector based on Angle

Posted 29 July 2012 - 07:38 PM

Do you know of a good function that will add a certain amount to a vector based on its rotation in 3D space? I got it to work in 2D but my 3D has confused me.

##
**Replies To:** Add to Vector based on Angle

### #2

## Re: Add to Vector based on Angle

Posted 29 July 2012 - 08:27 PM

It seems a bit odd to add an amount to a vector. Usually, you either assign an amount to a vector, or add another vector to a vector.

But if you want to add an amount to a vector, the easiest way is to copy the vector. Then normalize the copy. Then multipy the copy times the scalar value that you want to add as the amount. Then add the two vectors together.

But if you want to add an amount to a vector, the easiest way is to copy the vector. Then normalize the copy. Then multipy the copy times the scalar value that you want to add as the amount. Then add the two vectors together.

### #3

## Re: Add to Vector based on Angle

Posted 30 July 2012 - 12:50 AM

Can you explain what you're attempting to accomplish here? This just looks like voodoo math.

### #4

## Re: Add to Vector based on Angle

Posted 30 July 2012 - 06:49 AM

I am trying to calculate the position to add smoke behind my ship (or any other place around the ship). As the ship rotates I want to be able to calculate the same position relative to the ship. What do you recommend?

### #5

## Re: Add to Vector based on Angle

Posted 30 July 2012 - 07:05 AM

So you want to rotate a vector relative to the ship?

Your ship probably has what is called a 'transform', which stores translation(position), scale, and rotation. It may be stored as a Matrix, or as a coupling of vectors and quaternions, or even a custom type (Unreal Engine uses a special rot struct for rotation).

Use the rotation value to transform the vector.

with matrix (a matrix has scale, rot, and translation all in one):

http://msdn.microsof...y/bb199526.aspx

with quaternion (used for rotation only):

http://msdn.microsof...ibrary/bb464184

Note - if you use matrix, it has translation, scale, and rotation all in one. If you only want to rotate the vector, and not translate it, then you'll need to remove the translation bit from the transform matrix.

Your ship probably has what is called a 'transform', which stores translation(position), scale, and rotation. It may be stored as a Matrix, or as a coupling of vectors and quaternions, or even a custom type (Unreal Engine uses a special rot struct for rotation).

Use the rotation value to transform the vector.

with matrix (a matrix has scale, rot, and translation all in one):

http://msdn.microsof...y/bb199526.aspx

with quaternion (used for rotation only):

http://msdn.microsof...ibrary/bb464184

Note - if you use matrix, it has translation, scale, and rotation all in one. If you only want to rotate the vector, and not translate it, then you'll need to remove the translation bit from the transform matrix.

This post has been edited by **lordofduct**: 30 July 2012 - 07:08 AM

### #6

## Re: Add to Vector based on Angle

Posted 30 July 2012 - 07:18 AM

rex64, on 30 July 2012 - 07:49 AM, said:

I am trying to calculate the position to add smoke behind my ship (or any other place around the ship). As the ship rotates I want to be able to calculate the same position relative to the ship. What do you recommend?

I assume your ship's facing is stored as a Vector3? If that's correct, then this is a very easy calculation. If the ship's position is stored as a vector and the ship's facing/heading is stored as a vector, then here's what you do:

First determine how far behind the ship you want the position to be. Let's call the new position SmokeStart position and it will be a Vector3. (This also works for Vector2's as well). We decide that we want the distance from the ship to SmokeStart position to be 0.6f.

Let's say we have two Vector3's. ShipPosition is the coordinates of where the ship is at. ShipFacing is a vector that points in the direction the ship faces.

SmokeStart = ShipPostion + (-ShipFacing.Normalize * 0.6f);

You are multiplying ShipFacing times 0.6 to set it's length/amount to 0.6. It should be a normalized vector (length = one) at all times, but if it isn't you can just normalize it. I don't know that the way I normalized it above is correct. Off the top of my head I can't remember. But if you normalize it it will set it's length to one and keep the same direction. It has to have a length of one for this multiplication trick to work and set the vector's length to whatever number you multiply it with.

Then you add ShipPosition to the ShipFacing vector. This will put SmokeStart in front of the ship by 0.6 units. But you want it to be behind the ship. You can reverse a vector 180 degrees by simply multiplying it times negative one. In other words, if you just put a negative before the vector you will exactly reverse it's direction.

Once you get used to vectors, they make life for a game programmer a WHOLE lot easier.

This post has been edited by **BBeck**: 30 July 2012 - 07:21 AM

### #7

## Re: Add to Vector based on Angle

Posted 30 July 2012 - 07:53 AM

I'd hope an objects rotation in 3d was stored with more than just a 'facing' vector. If that is all OP is using to store rotation... well that's their problem in the first place.

### #8

## Re: Add to Vector based on Angle

Posted 30 July 2012 - 11:37 AM

Since I am using Quaternions for rotations everywhere else to avoid Gimbal Lock, I figured I might as well give it a try here as well. Am I close here?

(Sorry I had accidentally posted this on the wrong thread)

By the way, I use the following:

Ship rotation: Quaternion

Ship position: Vector3

Smoke Offset: Vector3

(Sorry I had accidentally posted this on the wrong thread)

By the way, I use the following:

Ship rotation: Quaternion

Ship position: Vector3

Smoke Offset: Vector3

/// <summary> /// Adds the amount to the vector based on the angle. This basically keeps the ship part/smoke at the same rotation and distance on the ship as the ship rotates. /// </summary> public static Vector3 AddToVector(Vector3 OrigionalVector, Quaternion heading, Vector3 amountToAdd) { return Vector3.Transform(OrigionalVector + amountToAdd, heading);

This post has been edited by **rex64**: 30 July 2012 - 11:38 AM

### #9

## Re: Add to Vector based on Angle

Posted 30 July 2012 - 11:42 AM

lordofduct, on 30 July 2012 - 08:53 AM, said:

I'd hope an objects rotation in 3d was stored with more than just a 'facing' vector. If that is all OP is using to store rotation... well that's their problem in the first place.

Umm. Actually, you're right on that one (face turning red). I mean in this case the math works, but you need more than a Vector3 to store facing information because the Vector3 doesn't know which direction is "up". I'm still partially thinking in terms of the quaternion example, where the quaternion

**can**store facing info by itself because it keeps track of "up".

Probably, what you would do is store the orientation as a world matrix and then get a facing vector from that matrix. Or just handle it straight from the matrix.

But that's probably what you were saying earlier.

### #10

## Re: Add to Vector based on Angle

Posted 30 July 2012 - 11:49 AM

I am just very glad to have some help figuring this stuff out. I have learned SO MUCH. I did not want to admit it to anyone when I joined this form I did not know C#, XNA, or any real 3D experience. Now I am very proud of my accomplishments. I will have to post a screenshot... of our new game sometime.

This seems to work, does this look correct?

This seems to work, does this look correct?

/// <summary> /// Adds the amount to the vector based on the angle. This basically keeps the ship part/smoke at the same rotation and distance on the ship as the ship rotates. /// </summary> /// <param name="OrigionalLocationVector">The point we are starting at.</param> /// <param name="heading">This is the direction the ship is facing.</param> /// <param name="amountToAdd">The is the ship part.</param> /// <returns></returns> public static Vector3 AddToVector(Vector3 OrigionalLocationVector, Quaternion heading, Vector3 amountToAdd) { return Vector3.Transform(amountToAdd, heading) + OrigionalLocationVector; }

### #11

## Re: Add to Vector based on Angle

Posted 30 July 2012 - 03:25 PM

rex64, on 30 July 2012 - 12:49 PM, said:

I am just very glad to have some help figuring this stuff out. I have learned SO MUCH. I did not want to admit it to anyone when I joined this form I did not know C#, XNA, or any real 3D experience. Now I am very proud of my accomplishments. I will have to post a screenshot... of our new game sometime.

This seems to work, does this look correct?

This seems to work, does this look correct?

/// <summary> /// Adds the amount to the vector based on the angle. This basically keeps the ship part/smoke at the same rotation and distance on the ship as the ship rotates. /// </summary> /// <param name="OrigionalLocationVector">The point we are starting at.</param> /// <param name="heading">This is the direction the ship is facing.</param> /// <param name="amountToAdd">The is the ship part.</param> /// <returns></returns> public static Vector3 AddToVector(Vector3 OrigionalLocationVector, Quaternion heading, Vector3 amountToAdd) { return Vector3.Transform(amountToAdd, heading) + OrigionalLocationVector; }

Will it work? I like to always say "Never argue with working code." If it works... it works.

So, Vector3.Transform has an overload that rotates a vector by the rotation stored in a quaternion. So, that will give a Vector3 based on rotating the heading amount away from the amountToAdd Vector. Then that vector will be used to find a new Vector3 position away from the OrigionalLocationVector.

I look forward to seeing what you are working on!

I wouldn't be afraid to admit what you don't know. There are a few people on here that might give you a hard time for it, but the XNA forum is one of the more friendly. Plus, we all started out knowing none of this stuff. And even now, I think most of us are

**still**learning C# and XNA more than we would like to admit.

I usually have no problem just coming straight out and admitting I'm figuring out this stuff as I go. I've been programming forever, but I only started working with XNA like 3 or 4 years ago. And I've only been working with C# since a little before that. There's still a lot of stuff that I probably should be embaressed to admit that I don't know about C#, and maybe even about XNA. I could name a lot of topics in XNA that I know very little about.

I'm usually able to figure things out as I go because I have a very strong background in almost all of the related subjects. For example, the vector stuff mostly makes sense to me because I know trigonometry well enough to teach a college class on it, not because I've worked with vectors in XNA all that much.

Really, I figure most people trying to learn XNA are struggling to figure it out just like I am. The only reason I figure that I'm a good teacher for it is that I may be a couple of steps ahead of them on the journey, and I also have spent years learning all the interrelated stuff like modeling and lighting and physics and so fourth.

I'm doing tutorials to teach people, not because I have years of experience writing professional games, but because A) there really isn't anyone else already out there teaching this stuff and if I can figure it out, at least I can show people code that works and gets the job done. And working code examples are better than no examples at all.

I may not always know the best way to do something in XNA, but having something that works is better than nothing.

And I learn in the process. I have to understand something pretty well in order to teach it to someone else.

Anyway, I'm glad if anything I've writen has helped.

3D game programming is tough!!! It's hard. There's just a whole lot of really complicated stuff that you have to know pretty well to get even something simple accomplished.

Don't be suprised if it takes a lot of reading and re-reading and reading once more to understand just about every bit of it. I've been trying to learn 3D game programming for the better part of a decade and I still feel like I don't know half of what you would need to know to program a game like GTAIV. On the other hand, sometime I suprise myself with what I'm able to figure out on my own.

Page 1 of 1