Double posting is bad, but I’m doing it anyway. I’ve come up with a new algorithm to take both X and Y movement into account at the same time:
/**
* Moves this object to the point of collision with obj, if
* there is one. As well, if there is a collision, return true
*/
public boolean moveToCollision(int xDist, int yDist, EObject obj)
{
// We start at this object's x position
Point start = new Point (xDist, yDist);
// The point where this object should end up if
// there is no collision
Point end = new Point (x + start.x, y + start.y);
// Get bounding shape of obj
Rectangle objBound = obj.getBounds();
// Create a line of possible intersection
Line2D.Double line = new Line2D.Double (start, end);
// Test for collision
if (line.intersects (objBound)) {
// TODO If there is a collision, move this
//object to the point of the collision
return true;
}
// Move this object to the normal coordinates
moveTo (end.x, end.y);
return false;
}
My issue is, if they do intersect, how do I get the object to move to the proper location? Like, how do I accurately move it to the point of the collision? Do I just check the direction of movement and move the object to the opposite bounding edge of the other object? What if it collides at a diagonal? Should I stop movement altogether? Or should I continue movement towards the axis that didn’t receive the collision?