# Calculating angles between 3d vectors

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

## Recommended Posts

Hi everyone, This has been asked before, but i was unable to find exactly what i need (which is pretty simple really)... Basically I'm moving my game from 2d to 3d, and I've stumbled upon a few issues. Let's say i have a blast center point (vector A), and I want to move all vectors in a certain radius away from it so it would look real. Untill now I used to calculte the angle between the two vectors using Arc-tangens (deltaX/deltaY), and then used sin/cos to move the affected vector accordingly in the direction away from the center. However, Now that I'm in 3d, it gets a bit more complicated... I'm not sure how i can represnt angle in 3d at all, how to calulate that angle and how to get my delta X/Y/Z values from it. So, any insight would be really appreciated! Ehud.

##### Share on other sites
The first thing I would say is that regardless of dimension it seems the conversion to an angle or angles is unnecessary, given that the direction in which the force is applied is parallel to the vector from the blast center to the particle. Does that make sense? Or perhaps I'm missing something...

If you do need to associate angles with a direction in 3D, spherical coordinates might be a natural choice. Still, I don't see how this would be useful.

To be more specific, it seems it would be sufficient to compute the force vector as the normalized difference between the particle and the center of the explosion, scaled by the desired magnitude - no angles required.

##### Share on other sites
Hi Jyk,
I must say you got me a bit confused. I was pretty sure up till now that a noraml of a vector is the vector perpenicular to it or something of the sort...
I did some reading and I just wanted to make sure I got it right:
a normalized vector is a vector in the same direction with length 1 (as described here: http://mathworld.wolfram.com/NormalizedVector.html)?

Can someone please elaborate a bit on the subject (I know it's very basic, but still).
Thanks!

##### Share on other sites
I am pretty sure what he's saying is that since you just want to do something simple, that is depending on the radius you are away from the object, you want something to be given a certain amount of force. He's saying you don't need to calculate angles, just the distance that the object is from to figure out the amount of force on the object. BUT, you still want to know the direction the object will be moved in.

Treat points as vectors. Then if I have a blast at p1 = 1,1,2 and an object at p2 = 3,4,8 then the object p2 will be pushed in the direction v = p2 - p1 = 3,4,8 - 1,1,2 = 2,3,6. To get the magnitude or length, ||v||, from p2 to p1, we already have the vector as 2,3,6. The length is ||v|| = sqrt(vx^2 + vy^2 + vz^2) = sqrt(4 + 9 + 36) = sqrt(49) = 7.

So, say you have a blast at 0,0,0 and several objects at various locations. You could if you wanted compute the vector from the blast to each object by Pobject - Pblast, and then calculate the magnitude. Then calculate the unit vector u(hat) = u / ||u||. Each time step, the object will move in the direction of the unit vector by a function of the magnitude.

So in the example in my second paragraph, you have Pobject = 3,4,8; Pblast = 1,1,2; v = 2,3,6; ||v|| = 7; v(hat) = 2/7, 3/7, 6/7; then each time step Pobject's location is translated (added) by v. Say you want objects REALLY close to the blast to get shot far, and and objects further to not get shot that far, then you could do the POWER = (1/||v||)^2 = (1/7)^2 = 1/49. So Pobject will get translated by v(hat) * POWER.

Hope this helps some. Get a really simple intro to linear algebra so you can see hwo to do vector manipulation like this.

##### Share on other sites
Hmm, I was thinking and I thought that maybe you are having problems with something else than I was talking about. If you already have objects moving with in 3d with a certain vector, then it is also simple. Just treat the velocity vectors as you would points.

Blast at p1 = 0,0,0; Object at p2 = 3,4,2 with velocity v = 1,3,4 (note this is v(hat) * ||v||. Then with the new blast, the new velocity is simply v2 = v + (p2 - p1).

##### Share on other sites
I think rpreller probably pretty much covered it, but here's some further clarification, just in case...

First of all, yes, to normalize a vector is to retain its direction but make it of length 1.

If I'm interpreting your problem correctly, then a) no angles should be required, and b) the solution should be more or less the same in any dimension (2D and 3D in this case).

Given a particle with position p and an explosion with center c, we wish to derive a force vector f to feed into a physics simulator (or use directly to modify the particle's velocity):
// The vector from the center of the explosion to the particlevector3 f = p-c;// The distance between the twofloat dist = f.length();// Normalize 'f'f /= dist;// Compute a magnitude for the force based on the strength of the// explosion, and possibly attenuated by the distance to the explosionfloat magnitude = /* ... */;// Apply the magnitude to the force vectorf *= magnitude;
Does that help? Or are we perhaps misunderstanding the problem?

##### Share on other sites
Thanks guys, you were both right on target.
That was exactly what i was looking for...
Sometimes I tend to take the hard way of doing simple things :)

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633298
• Total Posts
3011260
• ### Who's Online (See full list)

There are no registered users currently online

×