Lóránt Gerber Lóránt Gerber -4 years ago 135
Java Question

Getting a point from begginning coordinates, angle and distance

I want to get a Vector containing a coordinate. I know my beginning coordinates, angle and distance. So far I've tried doing:

Vector2 pos = new Vector2(beginningX, beginningY).add(distance, distance).rotate(angle);


But it doesn't work as I expect it to. When the rotation isn't
0
the coordinates become big, and the ending point isn't where I expect it to be. I know this must be a simple problem, but I just can't solve it.

EDIT:
Tried doing:

Vector2 pos = new Vector2(beginningX, beginningY).add(distance, 0).rotate(angle);


(Adding
distance
to
x
only) Still no success.

Answer Source

I'd say you're doing it wrong: you need to rotate the distance vector and add it to the position vector:

Vector2 pos = new Vector2(beginningX, beginningY).add(new Vector2(distance, 0).rotate(angle) );

You might want to read up on vector math but basically it amounts to this (if I correctly understood what you're trying to do):

If you rotate a vector you're always rotating around point 0/0. Thus you'll want to create a vector that covers the distance from 0/0 to your distance on the x-axis:

0---------------->d

Now you rotate that vector by some angle:

       d
      /
     /
    /
   /
  /
 0

Then you offset that vector by your starting point, i.e. you add the two vectors (for simplicity I assume your starting point lies on the y-axis):

      d
     /
    /
   /
  /
 /   
s
|
|
|
0
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download