Skip to main content
deleted 12 characters in body
Source Link
LLL79
  • 311
  • 1
  • 8

As Alex indicated, your collision response is a very sharp "snap to edge" type of reaction, where if the two rectangles collide, one of them will instantaneously adjust its position to place it adjacent to the other.

That's not ideal behavior, but it isn't necessarily a problem either. The problem is that you return from each of your sequential if(collision) checks, which is why you only get a top/bottom edge alignment.

The early exit conditions (which are correct for AABB collision detection) areis not necessary in your collision response. Try something more like:

            if (actor1.Bounds.Top > actor2.Bounds.Bottom) //Hit From Top
            {
                actor1.y_position += Rectangle.Intersect(actor1.Bounds, actor2.Bounds).Height;
            }
            else if (actor1.Bounds.Bottom > actor2.Bounds.Top) //Hit From Bottom
            {
                actor1.y_position -= Rectangle.Intersect(actor1.Bounds, actor2.Bounds).Height;                
            }

            if (actor1.Bounds.Left > actor2.Bounds.Right)
            {
                actor1.x_position += Rectangle.Intersect(actor1.Bounds, actor2.Bounds).Width;
            }
            elseif (actor1.Bounds.Right > actor2.Bounds.Left)
            {
                actor1.x_position -= Rectangle.Intersect(actor1.Bounds, actor2.Bounds).Width;
            }

As Alex indicated, your collision response is a very sharp "snap to edge" type of reaction, where if the two rectangles collide, one of them will instantaneously adjust its position to place it adjacent to the other.

That's not ideal behavior, but it isn't necessarily a problem either. The problem is that you return from each of your sequential if(collision) checks, which is why you only get a top/bottom edge alignment.

The early exit conditions (which are correct for AABB collision detection) are not necessary in your collision response. Try something more like:

            if (actor1.Bounds.Top > actor2.Bounds.Bottom) //Hit From Top
            {
                actor1.y_position += Rectangle.Intersect(actor1.Bounds, actor2.Bounds).Height;
            }
            else if (actor1.Bounds.Bottom > actor2.Bounds.Top) //Hit From Bottom
            {
                actor1.y_position -= Rectangle.Intersect(actor1.Bounds, actor2.Bounds).Height;                
            }

            if (actor1.Bounds.Left > actor2.Bounds.Right)
            {
                actor1.x_position += Rectangle.Intersect(actor1.Bounds, actor2.Bounds).Width;
            }
            elseif (actor1.Bounds.Right > actor2.Bounds.Left)
            {
                actor1.x_position -= Rectangle.Intersect(actor1.Bounds, actor2.Bounds).Width;
            }

As Alex indicated, your collision response is a very sharp "snap to edge" type of reaction, where if the two rectangles collide, one of them will instantaneously adjust its position to place it adjacent to the other.

That's not ideal behavior, but it isn't necessarily a problem either. The problem is that you return from each of your sequential if(collision) checks, which is why you only get a top/bottom edge alignment.

The early exit (which are correct for AABB collision detection) is not necessary in your collision response. Try something more like:

            if (actor1.Bounds.Top > actor2.Bounds.Bottom) //Hit From Top
            {
                actor1.y_position += Rectangle.Intersect(actor1.Bounds, actor2.Bounds).Height;
            }
            else if (actor1.Bounds.Bottom > actor2.Bounds.Top) //Hit From Bottom
            {
                actor1.y_position -= Rectangle.Intersect(actor1.Bounds, actor2.Bounds).Height;                
            }

            if (actor1.Bounds.Left > actor2.Bounds.Right)
            {
                actor1.x_position += Rectangle.Intersect(actor1.Bounds, actor2.Bounds).Width;
            }
            elseif (actor1.Bounds.Right > actor2.Bounds.Left)
            {
                actor1.x_position -= Rectangle.Intersect(actor1.Bounds, actor2.Bounds).Width;
            }
Source Link
LLL79
  • 311
  • 1
  • 8

As Alex indicated, your collision response is a very sharp "snap to edge" type of reaction, where if the two rectangles collide, one of them will instantaneously adjust its position to place it adjacent to the other.

That's not ideal behavior, but it isn't necessarily a problem either. The problem is that you return from each of your sequential if(collision) checks, which is why you only get a top/bottom edge alignment.

The early exit conditions (which are correct for AABB collision detection) are not necessary in your collision response. Try something more like:

            if (actor1.Bounds.Top > actor2.Bounds.Bottom) //Hit From Top
            {
                actor1.y_position += Rectangle.Intersect(actor1.Bounds, actor2.Bounds).Height;
            }
            else if (actor1.Bounds.Bottom > actor2.Bounds.Top) //Hit From Bottom
            {
                actor1.y_position -= Rectangle.Intersect(actor1.Bounds, actor2.Bounds).Height;                
            }

            if (actor1.Bounds.Left > actor2.Bounds.Right)
            {
                actor1.x_position += Rectangle.Intersect(actor1.Bounds, actor2.Bounds).Width;
            }
            elseif (actor1.Bounds.Right > actor2.Bounds.Left)
            {
                actor1.x_position -= Rectangle.Intersect(actor1.Bounds, actor2.Bounds).Width;
            }