# Why is my collision code blocking the player's movement in XNA?

I have the following code to check for collisions in my game, but this code doesn’t work it as should. For example:

• I go from up to down and if collision is detect I can’t turn right;

• if I go from right to left and collision is detect I can’t turn down.

What is wrong with this code and how could I improve it?

foreach (var o in ObjectList) {
previousPosition = player.Position;
if (player.CollisionRectangle.Intersects(o.Rectangle) && o.isCollision == true) {
if (o.ObjectUpdateLeftRight(player, o.Position) == 0) player.CanRight = false;
if (o.ObjectUpdateLeftRight(player, o.Position) == 1) player.CanLeft = false;
if (o.ObjectUpdateUpDown(player, o.Position) == 2) player.CanUp = false;
if (o.ObjectUpdateUpDown(player, o.Position) == 3) player.CanDown = false;
}
}


and ObjectUpdateLeftRight and UpDown:

public int ObjectUpdateLeftRight(Player.Player player, Vector2 objectToCheck) {
if (player.Position.X < objectToCheck.X) return 0; // right
if (player.Position.X >= objectToCheck.X) return 1; // left
return -1;
}

public int ObjectUpdateUpDown(Player.Player player, Vector2 objectToCheck) {
if (player.Position.Y >= objectToCheck.Y) return 2; // up
if (player.Position.Y < objectToCheck.Y) return 3; // down
return -1;
}


The problem with the current code is that there is no displacement after a collision has been detected on one of the axis and thus when the player's rectangle intersects with the block's rectangle, the positional check is done on both sides without calculating which direction's depth is more prevalent.

Since these checks are not separated once the player enters a block from below, both the vertical and horizontal movement are blocked since your current conditions check for both after one another.

I would advise that you do additional depth checking based on the rectangle intersections either on both axis at the same time or separately as suggested by Nils. (Which is the safer the and as far as I know cleaner way to do so.)

Answered by Razeal on December 3, 2020

You are not taking into account what direction you were moving in so this isn't very surprising.

The simplest way to handle this is to split your movement into horizontal and vertical movement. Move horizontally first. If you collide with something move the player back to where he was. Then move vertically and if you collide move the player back again.

This has the advantage that you can slide along walls as long as they are vertically or horizontally aligned.

Once this works you can make additional improvements like figuring out how far exactly you have to move an object back for it to not intersect anymore.

Guaranteeing that no two objects that can move intersect at any frame makes a lot of things easier to handle, since you don't need code to handle unstucking.

Answered by Nils Ole Timm on December 3, 2020

## Related Questions

### Calculate hockey puck bouncing off rubber bumper

0  Asked on March 5, 2021 by jinxy

### Trying to understand 3D transforms by making an aircraft control script. How would you implement 3D transforms correctly in this example?

1  Asked on March 2, 2021 by pocketonion

### Best way to save game data in Unity

1  Asked on February 28, 2021 by tair-galili

### How do I fix missing anti-aliasing in the Unity scene view and game?

0  Asked on February 27, 2021 by pale_rider

### Unity – Sprite vertices in 3D world

1  Asked on February 24, 2021 by muckington

### Should UI systems update naively with events and data from outside, or should they be coupled to some game state/singleton to know to change itself?

1  Asked on February 22, 2021 by yookakim

### How to represent a modular FSM for AI using ECS?

2  Asked on February 22, 2021 by christian-ivicevic

1  Asked on February 21, 2021 by amin007

### Box2d on authoritative server for Unity client

0  Asked on February 19, 2021 by antonio-agustin

### UE4 C++ – Add generated source code folders to Visual Studio solution?

1  Asked on February 12, 2021 by json-brody

### How to prevent a sprite to move in an angle that will lead to collision

1  Asked on February 4, 2021 by user3150201

### SetTrigger Only Works In Start and Not OnTriggerEnter

1  Asked on February 3, 2021 by kit-k

### How do I detect image clicks in Dark GDK?

1  Asked on February 2, 2021 by bobman

### FIx color banding in unity

0  Asked on February 1, 2021

### OnMouseOver not registering collisions

3  Asked on January 29, 2021 by hyperific

### How to copy morphs from one SkeletalMesh to another in UE4?

0  Asked on January 29, 2021

### Call native code from Unity iOS build error

1  Asked on January 29, 2021 by atlantis

### Unity – Smooth player input direction when changing camera

1  Asked on January 24, 2021 by herewego