# Utilizing vectors to move entities in 3D space

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

## Recommended Posts

Hi, folks and welcome to the topic!

First of all, I'd like to point out that I'm no math genius, but I really got hooked on vectors seeing their benefits in programming entity movement in virtual 2D and 3D environments.

There are several vector classes written in C/C++ to be found on the Internet. However, as I'm willing to learn more about vectors all the time, I dediced to create my own class template in C++ called Vector3.

As far as I know, vector consists of a direction and a magnitude. Direction can be represents with the help of other vectors or by an angle. Magnitude on the hand, is simply the line segment from one point to another. Sounds reasonably simple.

Now, I'm also willing to create a class called Entity. In this class I'd like to have a Vector3-class instance as a member. Via member function called "move" I'd like to use the vector to move some entity, like this:

 Entity bob("Bob"); bob.move(30, 0, 1); // '30' and '0' are both angles, '1' represents velocity, which is in meters per second. 

Now, if I know both the direction given in angles and the magnitude, not to mention the vector start point in 3D space, how is it possible to calculate the vector end point or do I need to calculate the vector end point in order to move the entity? For me, it makes sense that the movement of an entity happens from one point to another, but how is this movement usually implemented by vectors?

##### Share on other sites
Thanks, Kyall, for such a useful post! Now I see what are the benefits of using plain coordinates instead of angles to represent direction of where to go to. There's no need to recalculate direction when you can just sum it up to already existing coordinates in order to move requested entity. Simple yet satisfying.

Anyway, could somebody still prove why this is true to deepen my understanding (and possibly the other's following this topic):
[source lang="c++"]result.x = cos( direction.x ) * magnitude - sin( direction.y ) * magnitude
result.y = sin( direction.x ) * magnitude + cos( direction.y ) * magnitude[/source]

##### Share on other sites
I think Kyall got mixed up somewhere - the equations he gives look like those to rotate a 2D vector by some angle (which can be represented as another 2D vector, but with magnitude 1, i.e. a unit vector). I think what he meant was:

 result.x = cos(angle) * magnitude result.y = sin(angle) * magnitude 

Which is true by the very definition of the angle and the trigonometric angles cosine and sine. Think back to the unit circle (circle of radius 1) - for any angle, the cosine of the angle gives the x-coordinate of the point at which a line starting from the origin and going at said angle would intersect the circle. Similarly, the sine will give the y-coordinate of the point of intersection. So the point at which the line intersects the circle is:

(cos(angle), sin(angle))

Since this is in relation to the origin, this point can be represented as a vector (more accurately, a position vector, which means the vector of the movement required to get from the origin to that point). Now, the length of this segment (from the origin to the point) is going to be:

sqrt(cos(angle)^2 + sin(angle)^2)

And we know that cos(angle)^2 + sin(angle)^2 = 1, so the length of the segment is sqrt(1) = 1. This could also be deduced by noting that the point of intersection of the line with the unit circle is, by definition, located on the circle, which has radius 1, so the length to the origin must be 1.

So we just need to multiply this position vector by the magnitude, which is the length of the desired vector, to obtain the final vector:

(cos(angle), sin(angle)) * magnitude

Which is the same as:

(cos(angle) * magnitude, sin(angle) * magnitude)

Which is, in cartesian coordinates:
x = cos(angle) * magnitude
y = sin(angle) * magnitude

----

I find it very difficult to completely and intuitively explain vectors in a single post. If you really want to get a grip on it, I suggest following some sort of linear algebra course, which is basically all about vectors and matrices. It really does help.