-4

How do you calculate the angle between one object and another assuming your first object is your origin and the up vector is, well up? I struggled with this problem in android and java for about six hours and there wasn't any really good questions or answers that gave a correct way to calculate it.

If the question is not super clear: I have an object on the screen and I want to know what the angle is from this object with up being the y axis (or 90 degrees) to another object on the screen. So if the first object is at 1,1 and the second object is at 2,2, then the angle should be 315. This is because 0 degrees is to the right, 90 degrees is up (y axis), 180 degrees is left, and 270 degrees is down.

Lokiare
  • 1,119
  • 1
  • 13
  • 23
  • You might try the math stack exchange site. – AHungerArtist Mar 09 '12 at 20:44
  • [sigh] I think you want `arctan`, but your question is very unclear. – Beta Mar 09 '12 at 20:45
  • This is off-topic. But the answer is the [*dot product*](http://en.wikipedia.org/wiki/Dot_product#Geometric_interpretation)., – Oliver Charlesworth Mar 09 '12 at 20:46
  • What is unclear about it? I'm posting the question because I found the answer but it took me about 2 days. I'm hoping it will help others. BTW I didn't find the answer on the internet even though I searched for hours. In 8 hours I'll post the answer I found, but feel free to answer it. I'll choose a better answer any day. – Lokiare Mar 09 '12 at 20:49

3 Answers3

2

There might be more elegant solutions, but I found this works well:

float angle = (float)Math.toDegrees(Math.atan2((double)(y1 - y2),(double)(x1 - x2)));
angle = (angle + 180.0f) % 360.0f;
angle = 360.0f - angle;

This gives the angle starting with 0 right, 90 up, 180 left, and 270 down. I answered my own question because it took me over a day to find this and it wasn't on the internet. I hope it helps someone. I'll leave the question unanswered for a day or two, to see if anyone can find a more elegant solution or a better way of expressing the answer. This answer is in java and has been tested on Android.

Lokiare
  • 1,119
  • 1
  • 13
  • 23
0

The answer is to use the dot product, thus:

theta = arccos( a.b / (||a|| ||b||) )

where . denotes the dot product, and || || denotes the vector magnitude.

So in your case, you pick a to represent "up" (so probably (0,0,1)), and b to be the difference between your two points.

Oliver Charlesworth
  • 252,669
  • 29
  • 530
  • 650
  • @JamesHolloway: I'm not in the habit of just giving people complete coded solutions. I suggest taking a look at the article I linked to; if you hit a specific problem then let me know. – Oliver Charlesworth Mar 09 '12 at 21:03
  • I've already solved it, but it took so long, that I'm hoping to help someone else out with the correct answer so they don't have to spend 2 days trying to figure it out. – Lokiare Mar 09 '12 at 21:56
0

In Java there is a function called Math.atan2(). This takes in a "vector". Generally you have 2 points and you need to calculate the angle between them. This can be done like:

Point p1 = new Point(2,6);
Point p2 = new Point(3,1);
double angle = Math.atan2(p2.y-p1.y,p2.x-p1.x);

Math.atan2 returns the results in the format specified here: Math.atan2

Devon_C_Miller
  • 15,714
  • 3
  • 40
  • 68
Dale Myers
  • 2,532
  • 2
  • 24
  • 45
  • This is in 2D. What about 3D? – Oliver Charlesworth Mar 09 '12 at 20:59
  • Close, but it doesn't come out with the correct answer with 0 degrees to the right 90 degrees up, 180 degrees left, and 270 degrees down. It gives an inverse of this, which is not helpful. It also gives the answer (when converted to degrees) from -180 to +180. – Lokiare Mar 09 '12 at 21:01
  • True. It returns it as specified in the above document. If the result is below 0 then subtract the result from 360. That should give you what you need. – Dale Myers Mar 09 '12 at 21:55
  • Yep, but it still gives it in the range of -180 to +180 after you use Math.toDegrees(angle) to convert... so "angle = 360 - Math.toDegrees(angle)" still doesn't come out right... – Lokiare Mar 09 '12 at 22:08