Hey everyone,
Currently I am in the process of improving collisions in my top-down game. I want to make it so that if you move diagonally and you are facing, let’s say, left, you can still slide up and down, according to which keys you are holding. I’m bad at explaining, so maybe the below code that detects my collisions will do it for me:
Rectangle newCollisionBox = new Rectangle(CollisionBox.X + (int)Velocity.X, CollisionBox.Y + (int)Velocity.Y,
Sprite.SpriteWidth * (int)Game1.Scale / 2, Sprite.SpriteHeight * (int)Game1.Scale / 4);
for (int index = 0; index < Game1.CurrentLocation.TileCollisions.Count; ++index)
{
if (newCollisionBox.Intersects(Game1.CurrentLocation.TileCollisions[index]))
{
if (newCollisionBox.Right > Game1.CurrentLocation.TileCollisions[index].Center.X)
{
Console.WriteLine("Coming from the right");
Velocity = new Vector2(0f, Velocity.Y);
}
if (newCollisionBox.Left < Game1.CurrentLocation.TileCollisions[index].Center.X)
{
Console.WriteLine("Coming from the left");
Velocity = new Vector2(0f, Velocity.Y);
}
if (newCollisionBox.Bottom > Game1.CurrentLocation.TileCollisions[index].Center.Y)
{
Console.WriteLine("Coming from the bottom");
Velocity = new Vector2(Velocity.X, 0f);
}
if (newCollisionBox.Top < Game1.CurrentLocation.TileCollisions[index].Center.Y)
{
Console.WriteLine("Coming from the top");
Velocity = new Vector2(Velocity.X, 0f);
}
}
}
So how would I go about this? I already did research to AABB collisions, but I am not sure how to implement this. My current problem with the code above is that if I move diagonally, my character does not move at all. Please provide me with some example code!
Thanks in advance. If you still have any questions regarding what I exactly mean, please let me know.
EDIT: (Solution)
With your help I managed to implement the collision detection! Check out my new code below:
`
if (CanMove)
{
if (Velocity.Y != 0f)
{
Vector2 oldPosition = Sprite.Position;
Sprite.Position = new Vector2(Sprite.Position.X, Sprite.Position.Y + Velocity.Y);
UpdateCollisionBox();
for (int index = 0; index < Game1.CurrentLocation.TileCollisions.Count; ++index)
{
if (CollisionBox.Intersects(Game1.CurrentLocation.TileCollisions[index]))
{
if (CollisionBox.Bottom > Game1.CurrentLocation.TileCollisions[index].Center.Y)
{
Sprite.Position = new Vector2(Sprite.Position.X, oldPosition.Y);
UpdateCollisionBox();
}
else if (CollisionBox.Top < Game1.CurrentLocation.TileCollisions[index].Center.Y)
{
Sprite.Position = new Vector2(Sprite.Position.X, oldPosition.Y);
UpdateCollisionBox();
}
}
}
}
if (Velocity.X != 0f)
{
Vector2 oldPosition = Sprite.Position;
Sprite.Position = new Vector2(Sprite.Position.X + Velocity.X, Sprite.Position.Y);
UpdateCollisionBox();
for (int index = 0; index < Game1.CurrentLocation.TileCollisions.Count; ++index)
{
if (CollisionBox.Intersects(Game1.CurrentLocation.TileCollisions[index]))
{
if (CollisionBox.Right > Game1.CurrentLocation.TileCollisions[index].Center.X)
{
Sprite.Position = new Vector2(oldPosition.X, Sprite.Position.Y);
UpdateCollisionBox();
}
if (CollisionBox.Left < Game1.CurrentLocation.TileCollisions[index].Center.X)
{
Sprite.Position = new Vector2(oldPosition.X, Sprite.Position.Y);
UpdateCollisionBox();
}
}
}
}
}
UpdateCollisionBox();
`
Once again, thank you to everybody who took their time to reply to this thread!