Given a rectangle, and a point with a vector direction towards the rectangle. How can I find the closest point on the outside of that rectangle to the point in question?

|
|
One technique which you could use is called "ray casting". It is commonly used for rendering graphics, but has other applications such as line-of-sight (as you are wanting to do) and path-finding. In general terms it works by finding the intersection of a ray and an object. In your example the ray is the vector for the character's direction. A useful reference for ray/object intersections (and incidentally other object/object intersections) is www.realtimerendering.com/intersections.html (look under the references for ray/aabb and ray/obb). |
|||
|
|
|
The rectangle has four sides. Each side is a line segment. Test each of the four sides for intersection with the ray. Track the closest hit. Here's some code to find out where on the segment the ray hits:
|
||||
|
|
|
Well, you can use just linear algebra (analytic geometry, to be more specific) to solve this. It depends on how you modeled the rectangle. Here's a general case: http://paulbourke.net/geometry/lineline2d/ |
|||
|
|