So...erm, yes. My first approach would be to google for ready-make answers.
To work out closest distance between vector and point, I think there is a simple bit of vector maths that does the trick. I have a function in my javascript that does this...hang on...here it is, dunno if it's what you need?
function LineCircleIntersection(circleX, circleY, radius, p1X, p1Y, p2X, p2Y) { // finds whether the line intersects the circle // also returns the square of the shortest distance from centre to line // usually squared distance can be used for comparisons: this saves a square root operation // circleX, circleY defines circle centre // radius is circle radius // p1X, p1Y defines start of line // p2X, p2Y defines end of line // note the two points must be different to avoid error (line must not have 0 length)
var dirX = p2X - p1X; // find vector pointing along line var dirY = p2Y - p1Y;
var diffX = circleX - p1X; // find vector pointing from line start to circle centre var diffY = circleY - p1Y;
// find proportion of line length travelled to reach the point closest to the circle centre // (projection of diff onto dir) var t = DotProduct(diffX, diffY, dirX, dirY) / DotProduct(dirX, dirY, dirX, dirY); if (t < 0) { t = 0; } // if closest point before start of line, find distance to line start if (t > 1) { t = 1; } // if closest point after end of line, find distance to line end
var nearestX = p1X + (t * dirX); // find coordinates of point on line nearest circle centre var nearestY = p1Y + (t * dirY);
var distanceX = circleX - nearestX; // find vector from circle centre to closest point on line var distanceY = circleY - nearestY;
// find square of distance from circle centre to line var distance_squared = DotProduct(distanceX, distanceY, distanceX, distanceY);
// return true if distance to line less than radius return {_collision: (distance_squared <= radius * radius), _distance_squared:distance_squared}; }
no subject
To work out closest distance between vector and point, I think there is a simple bit of vector maths that does the trick. I have a function in my javascript that does this...hang on...here it is, dunno if it's what you need?
function LineCircleIntersection(circleX, circleY, radius, p1X, p1Y, p2X, p2Y)
{
// finds whether the line intersects the circle
// also returns the square of the shortest distance from centre to line
// usually squared distance can be used for comparisons: this saves a square root operation
// circleX, circleY defines circle centre
// radius is circle radius
// p1X, p1Y defines start of line
// p2X, p2Y defines end of line
// note the two points must be different to avoid error (line must not have 0 length)
var dirX = p2X - p1X; // find vector pointing along line
var dirY = p2Y - p1Y;
var diffX = circleX - p1X; // find vector pointing from line start to circle centre
var diffY = circleY - p1Y;
// find proportion of line length travelled to reach the point closest to the circle centre
// (projection of diff onto dir)
var t = DotProduct(diffX, diffY, dirX, dirY) / DotProduct(dirX, dirY, dirX, dirY);
if (t < 0) { t = 0; } // if closest point before start of line, find distance to line start
if (t > 1) { t = 1; } // if closest point after end of line, find distance to line end
var nearestX = p1X + (t * dirX); // find coordinates of point on line nearest circle centre
var nearestY = p1Y + (t * dirY);
var distanceX = circleX - nearestX; // find vector from circle centre to closest point on line
var distanceY = circleY - nearestY;
// find square of distance from circle centre to line
var distance_squared = DotProduct(distanceX, distanceY, distanceX, distanceY);
// return true if distance to line less than radius
return {_collision: (distance_squared <= radius * radius), _distance_squared:distance_squared};
}