# Need help with getting a direction vector between two given points

I need to find the angle of rotation from a bullet traveling from point A to point B, like in my attached diagram. I tried searching, but I got a little lost in which exact formula to use.

I want to find the direction of a bullet, expressed as a vector with X and Y coordinates.

• Errr... B - A? – Vaillancourt Feb 13 '17 at 2:24
• Do you need an angle or a vector? – Vaillancourt Feb 13 '17 at 2:24
• I need a vector i did add that too my question a while back – Big T Larrity Feb 13 '17 at 2:25
• When you edit a question, don't just add, modify the content to reflect what you actually need, specially when there are no answers yet :) In this case, you should edit the title as well to reflect the fact that you don't need an angle, but a vector! – Vaillancourt Feb 13 '17 at 2:28
• This similar to How to calculate a path to the mouse? over at StackOverflow. There in my answer I explain and demostrate code to follow the mouse (so it needs to solve the vector from the current position to the current pointer position), done without any vector library. – Theraot Feb 13 '17 at 2:48

In your situation you need a simple subtraction, with a normalization.

vectDistance = B - A
vectDirection = vectDistance / lenght(vectDistance)


This will give you the direction.

In LibGDX, you can use the sub method from the Vector2 class, along with the nor from the same class:

vectDirection = b.sub(a).nor();


To help figure out if you need to subtract A from B or B from A, try to translate (in your mind) the 'origin' to (0, 0), and move the 'target' along with it. From that perspective, the "direction" should be taken from the position of the 'target'.

Another clue that will make you notice that you used the wrong order: the bullets will fly in the opposite direction of what you're expecting ;P

• i used direction = targetPosition.sub(startPosition); inside my bullets constructor (just in case anyone ever reads this and needs help with same thing) – Big T Larrity Feb 13 '17 at 2:44
• Im still having a little trouble implementing this to me game. I have this in my bullet update to move it : setPosition(getX() + direction.x * speed, getX() + direction.y * speed); but with this it works backwards and also only travels either up-right or down-left in diagonal motion only – Big T Larrity Feb 13 '17 at 3:26
• i dont think the sub method is what i need. I need the direction Vector expressed as a figure between 0-1.0f, if you know what i mean – Big T Larrity Feb 13 '17 at 3:31
• @SuperMegaBroBro Yes, you need the sub method, along with the nor method. The sub method will give you the vector from A to B. This vector contains 2 things: the direction and the distance. You don't need the distance, so you need to normalize this vector. To do so you use the nor method, which will normalize it, i.e. give you a unit vector, i.e. a vector of length 1. – Vaillancourt Feb 13 '17 at 10:58
• you're amazing thanks! I was trying it out with 'vector.angle' all last night. will get onto this as soon as home. cheers again :) – Big T Larrity Feb 13 '17 at 11:23

OK thanks to the help of this community (above) Here is actually how I implemented it:

  public Bullet(Texture texture, Vector2 startPosition, Vector2 targetPosition){
super(texture);
setSize(0.1f,1f);
setPosition(startPosition.x, startPosition.y);

direction = targetPosition.sub(startPosition);
direction.nor();
System.out.println("direction " + direction.x + "," + direction.y);

}

public void update(float delta){
setPosition(getX() + (direction.x * speed), getY() + (direction.y * speed));
}


This works with the player character standing near the middle of the screen, it will fire bullet anywhere around him that I press. Thanks