Mr. Kenkron wrote:
public void handleCollision(Rectangle a, Rectangle b){
if (a.getX()+a.getWidth>b.getX()&&//if a is not to the left of b
a.getX()<b.getX()+b.getWidth()&&//and a is not to the right of b
a.getY()+a.getHeight>b.getY()&&//and if a is not above b
a.getY()<b.getY()+b.getHeight())//and a is not below b
{//then these rectangles intersect.
With rectangles, or any shapes, you can just use intersect() instead of all that.
Mr. Kenkron wrote:
//now see which edges are the closest
float overlapLeft=Math.abs(a.getX()+a.getWidth-b.getX());//the left edge of a
float overlapRight=Math.abs(a.getX()-(b.getX()+b.getWidth()));//the right edge of a
float overlapUp=Math.abs(a.getY()+a.getHeight-b.getY());//the top of a
float overlapDown=Math.abs(a.getY()<(b.getY()+b.getHeight()));//the bottom of a
float minOverlap=Math.min(overlapLeft,overlapRight,overlapUp,overlapDown);//the smallest of these
if (minOverlap==overlapLeft){
//Here is what you do if a is hitting on the left side
}
}
}
That could work (I don't have time to test it), but here is the code I had for determining direction of polygon collision:
Code:
if(polyCam.getMaxX() < polyPlayer.getMinX())
{
//player is off screen to right
}
else if(polyCam.getMinX() > polyPlayer.getMaxX())
{
//player is off screen to left
}
if(polyCam.getMaxY() < polyPlayer.getMinY())
{
//player is off screen to bottom
}
else if(polyCam.getMinY() > polyPlayer.getMaxY())
{
//player is off screen to top
}
I hope one of these methods works for you!
_________________
http://www.wrapperapps.comfor more info on Prime Mover, my first game based on Java/Slick 2D.