View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Intersection Between 2D Ray and Rectangle Borders

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1hdlopesrocha  Members

Posted 19 October 2012 - 01:01 PM

Hello! I need to find this point, if someone could help...

dir is always centered in rectangle.

Thanks ;-)

Edited by hdlopesrocha, 19 October 2012 - 01:04 PM.

### #2Inferiarum  Members

Posted 21 October 2012 - 07:05 AM

lets say h and w are width and height of the rectangle
c is the center point.

1. Determine how far you can move:

d = min( h/2/abs(dir.y), w/2/abs(dir.x) );

2. Move:

x = c + d * dir

### #3xissburg  Members

Posted 21 October 2012 - 09:22 AM

You could use the tangent of the angle of the dir vector. Depending on the direction it is pointing, you use a different formula based on the angle of the diagonal of the AABB. I've came up with this:

[source lang="cpp"]// WARNING: Untested codePoint Intersect(Point min, Point max, float alpha) // alpha is the dir vector angle{ float beta = atan2(max.y - min.y, max.x - min.x); // AABB diagonal angle float hw = (max.x - min.x)*0.5; // AABB width/2 float hh = (max.y - min.y)*0.5; // AABB height/2 float tanAlpha = tan(alpha); if (fabsf(alpha) < beta) // right section return Point(max.x, min.y + hh + tanAlpha*hw); else if (fabsf(PI - alpha) < beta) // left section return Point(min.x, min.y + hh - tanAlpha*hw); else if (fabsf(PI*0.5 - alpha) < PI*0.5 - beta) // top section return Point(min.x + hw + 1.f/tanAlpha*hh, max.y); else if (fabsf(1.5f*PI - alpha) < PI*0.5 - beta) // bottom section return Point(min.x + hw - 1.f/tanAlpha*hh, min.y); else // should not happen o_ O return WTF;}[/source]
.

### #4hdlopesrocha  Members

Posted 06 November 2012 - 03:05 PM

thanks ;-) problem solved!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.