From you code I can't tell what the
ownerv vector is so I can't tell you why it's giving you the wrong result.
But to do it from scratch, according to Wikipedia's article on vector projection, you can do the following;
- Create a vector a that is the direction from o to v.
- Create a vector b that is the direction from o to n.
- Let d be the dot product of a and b divided by the length of b (this will give you a scalar that is the distance from o in the direction of b).
- Create the result, target, by adding a vector of length d to o.
In code those steps could look like this;
Vector2 o = new Vector2(6f, 2f);
Vector2 n = new Vector2(1f, 4f);
Vector2 v = new Vector2(3f, 2f);
// The direction from o to v
Vector2 a = (new Vector2(v)).sub(o);
// The direction from o to n
Vector2 b = (new Vector2(n)).sub(o);
// d is the length along b where the project a will fall.
float d = (new Vector2(a)).dot(b) / b.len();
// Take the vector that is the normal of b, scale it by d and add it to the start point o,
Vector2 target = (new Vector2(o)).add(b.nor().scl(d));
(Note that you probably don't want to new up as many
Vector2s as I do in this example in your actual game code).