What I'm trying to do.

So I want to have a projectile that starts at point (x1, y1) and head in the direction of my mouse say point (x2, y2).

To clarify I'm pretty sure it's a vector but I don't know much about them.

Language / Librarys

I'm making the game in Java and LibGDX so Java.MathUtils would come in to play i'm pretty sure. If anyone that can answer don't know these languages, psuedocode will be just fine.

How I've tried it

I'm pretty sure the formula is something alongs the lines of velX := cos(radians) * speed velY := sin(raidans) * speed

And radians starts out as Pi/2 though the tutorial was pretty rough.

Then it would be x += velx * deltatime and same with y.

Can anyone help me out with the translation formula


2 Answers 2


You need to learn a little bit more about vectors but I'm going to explain it anyways.

First of all, let's say that your 'game unit' is in pixels and that points (coordinates) and vectors have two components x and y.

Let's assume that you have two given points A = (x1, y1) and B = (x2, y2).

To get the vector between A and B, you have to do the following formula: V = B - A. Since B and A have two components, we can translate this into V = (xB-xA, yB-yA)

enter image description here

However, this vector is pretty much useless because the length of it is the length of the line between A and B. If you have something like this

V = mouse.pos - projectile.pos
projectile.pos += V

it will instantly move the projectile to the mouse because - well - you're asking to move the projectile to the mouse..

The idea here is to get a direction from your projectile (point A) to your mouse or whatever object (point B).

enter image description here

So.. how do we get this direction ? Well, it's easy, the formula is u = V/length(V)

This procedure is normalizing where you just get the direction of a vector and completely ignore its length (sometimes called 'magnitude').

Adding this vector u to your position will move your object 1 unit away from its current position.

The point of it ? Moving your projectile towards a point with a fixed 'speed', like this:

projectile.pos += u * 10;

this code will move your projectile 10 units (let's say pixels) towards your mouse.

EDIT: Note that with this method you don't need to use trigonometry to compute the normalized vector; that means that neither you have precision loss (floats, pi, ...) nor confusing conversions with radians. That's a win :)

EDIT2: For consistence and clarity, I used the word vector and position but these "concepts" are the same: a point has an X and Y component as well as a vector.

In the code you would use the same data structure. Unfortunately I don't know java but I can provide you a c++ example:

struct MyData //That would be public class in java I beleive
    float x;
    float y;

Here MyData is a structure that allows two components: x and y. This structure is used for points as well as vectors; eg:

MyData point_a;
MyData point_b;
MyData vector_between;

//Instantiate and/or initialize values ...

vector_between = point_b - point_a;

In C++ there is a concept (operator overloading) that makes you able to perform custom operations between data structures (such as point_b - point_a in the above example). This is really useful because the program won't know how to subtract two "MyData" together unless you write your own method.

I don't think Java enables you to do this "hack", so instead of

vector_between = point_b - point_a;

you could write

vector_between.x = point_b.x - point_a.x;
vector_between.y = point_b.y - point_a.y;

this is what I meant where I directly used "pos" without the associated component.

Moreover, when I used length(V) I meant sqrt(V.x*V.x + V.y*V.y), that is the length/distance between two points (Pythagorean theorem, right ?)

NB: Usually the MyData data structure is, by "convention" called a Vector2 in 2D, Vector3 in 3D etc...

  • \$\begingroup\$ You are a legend. \$\endgroup\$
    – helpztheme
    Commented Jun 4, 2016 at 17:30
  • \$\begingroup\$ Is a vector a coordinate or a direction and how do i translate and X and Y coordinate by this , there isn't a .pos is there? Isn't it just a .x and .y \$\endgroup\$
    – helpztheme
    Commented Jun 4, 2016 at 20:09
  • \$\begingroup\$ Let me restate, projectile.pos += u * 10; is what you said for translation but i don't understand .pos , I'm not using sprites, just raw for loops for projectiles and X&Y Coordinates. \$\endgroup\$
    – helpztheme
    Commented Jun 4, 2016 at 20:20
  • \$\begingroup\$ Length of what? From point a to point b as a line? \$\endgroup\$
    – helpztheme
    Commented Jun 4, 2016 at 20:30
  • \$\begingroup\$ Edited my post, take a look at it :) In this case, the "pos" was a "MyData"/Vector2 instance \$\endgroup\$
    – Riptide
    Commented Jun 4, 2016 at 20:38

To get from point A to point B where both A and B are vectors, the direction is the vector normalize(B - A), lets call it C. The velocity you're looking for is the direction-vector C times the speed in units per second.

So if A is your actual position, B is your target position, 10 is the speed in units per second and delta is your deltatime, then your velocity is velocity = normalize(B - A) * 10; and your new position is A + velocity * delta.

  • \$\begingroup\$ What is Normalize, I can't find it in the java docs. & Would I do the same formula for both X & Y \$\endgroup\$
    – helpztheme
    Commented Jun 3, 2016 at 23:25
  • \$\begingroup\$ Do i do this velocity formula for both X & Y?? \$\endgroup\$
    – helpztheme
    Commented Jun 3, 2016 at 23:51
  • \$\begingroup\$ You do this once for the vector (x, y). Also, normalize is a function known in vector math, so either read about vector math or let your vector-library do it for you, I'm sure it has a normalize function. \$\endgroup\$
    – tkausl
    Commented Jun 4, 2016 at 0:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .