Collision bug fix [Solved]

I have a simple collision system in my game where it uses rectangles to detect when another rectangle intersects with it. whenever the player collides from the top or left of the rectangle the collision acts fine but when it collides with the bottom of right sides, the player goes through the block until it hits the other side. anyone else have this problem. anyone have a fix?

thanks

ps. i didnt post any code because i am assuming that the process is well known and popular since it is so simple. if im wrong please tell me

Could you post your code? It sounds like you implemented collision but not fully (thats why it doesn’t work on all sides)

here it is:

Player collision (collision box in the middle of screen):


public static void Collision(){
		for (int i=0;i<e.size();i++){
			if (bounds().intersects(e.get(i).bounds())){	
				//collision stuff here
				if(e.get(i).enNum==5){
					System.out.println("hit a wall");
					if(direction==1){
						goRight=false;
						goUp=false;
						goLeft=false;
						goDown=false;
						speedx=0;
						x-=1;
						canChange=false;
					}
					else if(direction==2){
						goRight=false;
						goUp=false;
						goLeft=false;
						goDown=false;
					x+=5;
					speedx=0;
					canChange=false;
				}
					else if(direction==3){
						goRight=false;
						goUp=false;
						goLeft=false;
						goDown=false;
					speedy=0;
					y-=5;
					canChange=false;
				}
					else if(direction==4){
						goRight=false;
						goUp=false;
						goLeft=false;
						goDown=false;
					speedy=0;
					y+=1;
					canChange=false;
				}
			}
				else{
					goRight=true;
					goUp=true;
					goDown=true;
					goLeft=true;
					//speed=8;
				}
		}

}
}

here is the collision to the tiles:


public Rectangle bounds(){
		return (new Rectangle(-Player.x+x,-Player.y+-y, size,size));
	}

and finally the player collision box:


public static Rectangle bounds(){
		return (new Rectangle(Main.width/2*Main.Scale, Main.height/2*Main.Scale,32,32));
		
		
	}

I would instead use the top left point as the anchor of the rectangle.

Here my [icode]getBounds()[/icode] method.


public Rectangle getBounds()
{
    // reuse a single rectangle to save memory
    this.bounds.x = this.x;
    this.bounds.y = this.y;
    this.bounds.width = this.width;
    this.bounds.height = this.height;

    return this.bounds;
}

And use this code to know the side of overlapping.


// Calculate distances
int xd = Math.abs( (rect1.x + rect1.width)/2 - (rect2.x + rect2.width)/2 );
int yd = Math.abs( (rect1.y + rect1.height)/2 - (rect2.y + rect2.height)/2 );

if (xd > yd)
{
    // Collision on either top or bottom
}
else if (yd > xd)
{
    // Collision on either left or right
}
else
{
    // Collision on corners
}

@SHC, I just noticed that your doing twice as many divisions as you need to in your “Calculate distances.”


int xd = Math.abs( (rect1.x + rect1.width)/2 - (rect2.x + rect2.width)/2 );
int yd = Math.abs( (rect1.y + rect1.height)/2 - (rect2.y + rect2.height)/2 );

Could instead be:


int xd = Math.abs( ( rect1.x + rect1.width - rect2.x - rect2.width ) / 2 );
int yd = Math.abs( ( rect1.y + rect1.height - rect2.y - rect2.height ) / 2 );

Just trying to help.

@quew8

Yeah, that would be more faster.

thanks guys this really helped ;D