Skip to content

Instantly share code, notes, and snippets.

@triktron
Last active December 11, 2019 12:24
Show Gist options
  • Save triktron/c7ae2318b4487530a17564a712d5e7ed to your computer and use it in GitHub Desktop.
Save triktron/c7ae2318b4487530a17564a712d5e7ed to your computer and use it in GitHub Desktop.
fixed switched bottom and top
using Microsoft.Xna.Framework;
namespace Colision_Monogame
{
class Colision // based on https://stackoverflow.com/a/46196426/6483652
{
public enum Side
{
Top,
Bottom,
Left,
Right,
None
}
public static Side GetColisonSide(Rectangle rect1, Rectangle rect2)
{
Point midRect1 = new Point(rect1.Left + rect1.Width / 2,
rect1.Top + rect1.Height / 2);
Point midRect2 = new Point(rect2.Left + rect2.Width / 2,
rect2.Top + rect2.Height / 2);
// no collision
bool coll_X = rect1.Right > rect2.Left && rect1.Left < rect2.Right;
bool coll_Y = rect1.Bottom > rect2.Top && rect1.Top < rect2.Bottom;
if (!(coll_X && coll_Y)) return Side.None;
// calculate bias flag in each direction
bool bias_X = midRect1.X < midRect2.X;
bool bias_Y = midRect1.Y < midRect2.Y;
// calculate penetration depths in each direction
float pen_X = bias_X ? (rect1.Right - rect2.Left)
: (rect2.Right - rect1.Left);
float pen_Y = bias_Y ? (rect1.Bottom - rect2.Top)
: (rect2.Bottom - rect1.Top);
float diff = pen_X - pen_Y;
// X penetration greater
if (diff >= 0)
if (bias_Y)
{
return Side.Bottom;
}
else
{
return Side.Top;
}
// Y pentration greater
else if (diff < 0)
if (bias_X)
{
return Side.Left;
}
else
{
return Side.Right;
}
return Side.None;
}
public static Rectangle Resolve(Rectangle rect1, Rectangle rect2)
{
Side side = GetColisonSide(rect1, rect2);
if (side == Side.Left) rect1.Location = new Point(rect2.X - rect1.Width, rect1.Location.Y);
if (side == Side.Right) rect1.Location = new Point(rect2.X + rect2.Width, rect1.Location.Y);
if (side == Side.Bottom) rect1.Location = new Point(rect1.Location.X, rect2.Y - rect1.Height);
if (side == Side.Top) rect1.Location = new Point(rect1.Location.X, rect2.Y + rect1.Height);
return rect1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment