Skip to content

Instantly share code, notes, and snippets.

@Bocom
Created May 29, 2012 12:13
Show Gist options
  • Save Bocom/2828104 to your computer and use it in GitHub Desktop.
Save Bocom/2828104 to your computer and use it in GitHub Desktop.
if (movement.X > 0)
currX = bounds.Right;
else if (movement.X < 0)
currX = bounds.Left;
if (movement.Y > 0)
currY = bounds.Bottom;
else if (movement.Y < 0)
currY = bounds.Top;
// X Axis
if (movement.X != 0)
{
startTop = new Vector2(currX, bounds.Top);
endTop = new Vector2(currX + velocity.X, bounds.Top);
Ray topRay = new Ray(map, startTop, endTop);
startBottom = new Vector2(currX, bounds.Bottom);
endBottom = new Vector2(currX + velocity.X, bounds.Bottom);
Ray bottomRay = new Ray(map, startBottom, endBottom);
int lengthTop = (int)velocity.X,
lengthBottom = (int)velocity.X,
lengthX = 0;
bool cTop = topRay.CastRayX(ref lengthTop);
bool cBottom = bottomRay.CastRayX(ref lengthBottom);
lengthX = Math.Min(lengthTop, lengthBottom);
if (cTop || cBottom)
{
if (movement.X > 0)
position.X += lengthX;
else if (movement.Y < 0)
position.X -= lengthX;
}
else
position.X += velocity.X;
bounds = CollisionBounds;
}
// Y Axis
if (movement.Y != 0)
{
startLeft = new Vector2(bounds.Left, currY);
endLeft = new Vector2(bounds.Left, currY + velocity.Y);
Ray leftRay = new Ray(map, startLeft, endLeft);
startRight = new Vector2(bounds.Right, currY);
endRight = new Vector2(bounds.Right, currY + velocity.Y);
Ray rightRay = new Ray(map, startRight, endRight);
int lengthLeft = (int)velocity.Y,
lengthRight = (int)velocity.Y,
lengthY = 0;
bool cLeft = leftRay.CastRayY(ref lengthLeft);
bool cRight = rightRay.CastRayY(ref lengthRight);
lengthY = Math.Min(lengthLeft, lengthRight);
if (cLeft || cRight)
{
if (movement.Y > 0)
position.Y += lengthY;
else if (movement.Y < 0)
position.Y -= lengthY;
}
else
position.Y += velocity.Y;
bounds = CollisionBounds;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment