Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to create Vector2 which starts from the players sprite and points towards other point. And I need its starting point to move along with player sprite. But however whatever I do all my vectors start from the origin and point towards the point I want. Is there any way to make Vector2 to start from the

share|improve this question
You might want to read up on vector addition and/or extend the question with math/code so we can see how you're using your vectors. – sarahm Apr 20 '13 at 23:37
up vote 3 down vote accepted

All vectors 'start' at the origin. They are just directions. You can define a ray, which is two vectors together, one to define position (P) and one to define direction (D). Then you can find any point on the ray by using a scalar parapeter (t):

point_on_ray(t) = P + D * t

By anyway, from the sound of it that's not what you need. In your case, you just need a vector (V) to point from your player position (P) towards a goal point (G).

All you need to do is construct V every frame.

V = G - P

This is your direction vector. It starts at the origin but is in the direction the goal is to the player.

Finally, the length of this vector is the distance between player and goal. Often, you need a normalised vector wich has a length of 1 and is a pure direction. To do this you just divide all components by length. I don't know the library you are using in question, but they will have a 'normalize' or 'unit' method to do that.

share|improve this answer

This is a basic linear algebra question. A vector is the difference between two points, such that A + V = B. You can find the correct V by taking B - A. If you need both a reference point and the vector, you need to store both of them. The combination of a starting point and a vector pointing to an end point is a line segment, not just a vector. You can represent a point as a vector since A - 0 = A, hence a point is just a vector relative to the origin. If A is the player position and B is the target, V = B - A is the vector, and if you need to remember the player position, you'll just need to copy it into another variable.

At least until you get to affine transforms, where points and vectors have an additional component, 1 for points and 0 for vectors, such that the B - A = V equation works out to 1 - 1 = 0 for that component. Thus in 2D you can sti treat points as vectors, just remember that the origin is (0,0,1) so a point referenced by vector A comes to (0,0,1) + (x,y,0) = (x,y,1). Meaning that any math needing to do affine transformations will need to implicitly treat the extra component as 1 for vectors representing points and 0 for regular vectors. Same frameworks have an extra Point class to make all this automatic.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.