1
\$\begingroup\$

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.

Points A and B are both points on a 2D grid.

\$\endgroup\$
10
  • 1
    \$\begingroup\$ Errr... B - A? \$\endgroup\$
    – Vaillancourt
    Feb 13, 2017 at 2:24
  • \$\begingroup\$ Do you need an angle or a vector? \$\endgroup\$
    – Vaillancourt
    Feb 13, 2017 at 2:24
  • \$\begingroup\$ I need a vector i did add that too my question a while back \$\endgroup\$ Feb 13, 2017 at 2:25
  • 2
    \$\begingroup\$ 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! \$\endgroup\$
    – Vaillancourt
    Feb 13, 2017 at 2:28
  • 2
    \$\begingroup\$ 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. \$\endgroup\$
    – Theraot
    Feb 13, 2017 at 2:48

2 Answers 2

5
\$\begingroup\$

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

\$\endgroup\$
6
  • \$\begingroup\$ i used direction = targetPosition.sub(startPosition); inside my bullets constructor (just in case anyone ever reads this and needs help with same thing) \$\endgroup\$ Feb 13, 2017 at 2:44
  • \$\begingroup\$ 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 \$\endgroup\$ Feb 13, 2017 at 3:26
  • \$\begingroup\$ 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 \$\endgroup\$ Feb 13, 2017 at 3:31
  • 1
    \$\begingroup\$ @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. \$\endgroup\$
    – Vaillancourt
    Feb 13, 2017 at 10:58
  • \$\begingroup\$ 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 :) \$\endgroup\$ Feb 13, 2017 at 11:23
0
\$\begingroup\$

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

\$\endgroup\$

You must log in to answer this question.

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