Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a set of boxes which create a sort of path, they don't intersect very smoothy and to help that process I'm trying ot find a way to get an angle between them that can make it easier to generate another box between the intersection points.

Here's a pic of what I mean, the crudely drawn yellow arrows are basiclaly the angles I'm trying to find to put boxes there:

enter image description here

share|improve this question
How are you storing the boxes? Are they point, rotation, width, height or 4 points? – Tetrad Sep 10 '11 at 6:33
up vote 6 down vote accepted

That vector can be obtained simply by summing up the normals of the boxes faces and normalize. The normal vector even point outside the box so the sum vector points toward the direction where the boxes exit from the intersection. along with the vector you need the intersection segment too (if you still don't have it)

share|improve this answer

you can use atan2. for example:

float radians = Math.atan2(object2.y - object1.y, object2.x - object1.x);

and then if you want the degrees its like this:

float degrees = radians * 180 / PI
share|improve this answer

Since I can't comment yet, I'll just put it as an answer:

As stas said, use Atan2, but be careful. Atan2 returns 0 to PI like a normal angle, BUT it returns 0 to -PI for the lower half of the 'circle'.

And remember that in the computer, angles start at 3 o'clock for 0, 12 noon for (PI/2), 9 for PI, and 6 for (3PI/2) or -PI in this case. You'll need to convert anything that you receive as a negative.

To convert: if(radians < 0) { radians = (2 * PI) - radians; }

From there, convert to degrees if you so desire.

I'm doing this from memory, but I had 'fun' learning this on my own a couple years ago. If you need any more help, just ask.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.