Sup,
While trying to calculate a distance to a line, I’m failing to do it (at least I think that’s the point where I’m failing). I’m trying to calculate if a target is within a certain distance between the projection of the source’s future position. Something like that image.
http://www.red3d.com/cwr/steer/gdc99/figure7.gif
Todo so, I’m trying to use a Line. Given the following inputs:
[java]Vector3f location, Vector3f velocity, float collisionRadius, float speed[/java]
Where LOCATION is the location in the world of the source spatial.
VELOCITY is the a direction telling wich is the source is pointing to
COLLISIONRADIUS is the diameter of the spatial’s collision radius (a sphere)
SPEED determines the spatial’s current speed
So, to solve this problem, I generated this code:
[java]float cautionRange = speed * 15;
Line line = new Line(location, velocity);
Plane plane = new Plane(velocity, collisionRadius / 4);
Vector3f closest = Vector3f.POSITIVE_INFINITY;
for (Vector3f target : targets) {
Vector3f loc = target.subtract(location);
// If the obstacle isn’t ahead of him, just ignore it
if (plane.whichSide(loc) != Side.Positive) {
continue;
}
// Check if the target is inside the check range
if (location.distance(target) <= cautionRange) {
// Check if the target will collide with the source
if (line.distanceSquared(target) - (collisionRadius / 2) <= 0) {
// Store the closest target
if (target.distanceSquared(location) < closest.distance(location))
closest = target;
}
}
}
// If any target was found
if (!closest.equals(Vector3f.POSITIVE_INFINITY)) {
// Find in wich side the target is
// To do that, we do a signed distance by
// subtracing the location from the target
// and the dot product between the line’s normal
float dot = closest.subtract(location).dot(line.getDirection().cross(Vector3f.UNIT_Y));
if (dot <= 0)
return velocity.cross(Vector3f.UNIT_Y);
else
return velocity.cross(Vector3f.UNIT_Y).negate();
}
// No target found, just return a zero value
return Vector3f.ZERO;
[/java]
But the problem is that, for some reason, it’s only detecting a target when it’s really, really close to the source. I don’t know what else to do. Thoughts and attemps:
- Maybe I’m messing up while instantiating the Line?
- I’ve tried to set cautionRange to near positive infinite values (the default entry is something like 1-2 * 15, so I’ve tried from 1f to 100000f), but nothing was changed (really nothing). Changing to near zero obsviously disables the code.
- In the end, does the distance method checks distance from the ORIGIN of the Line, or from the closest point of the line relative to the given point?
Can someone help me into this, I need to fix this algo to make progress, and it need to detects targets that are more far away than the current. If it helps, take a look at the running example: http://dl.dropbox.com/u/3279456/dist.7z (if offline, it’s because it’s still uploading).