The collision work but I don't know how know the collsiion direction

I have do a collision with my map the collision work but when my
player have a collision I stop the movement of the player and he don’t
walk because I don’t know how stop the player for one direction

its my code collision under the player and the map
public bool IsCollisionTile(Rectangle player) { foreach(Rectangle rect in this._collisionObject) { if (rect.Intersects(player)) { return true; } } return false; }

Its the code for the player don’t move is in the Game1 class
if (mapLoader.IsCollisionTile(player.getDestinationRect()))
{
player.setCantWalk(true);
}
Its the function update and setCantWalk in the class Player.cs

private bool _cantWalk;

    public void Update()
    {
    this._destinationRectangle.X = (int)_position.X;
    this._destinationRectangle.Y = (int)_position.Y;
    this._destinationRectangle.Width = _texture.Width;
    this._destinationRectangle.Height = _texture.Height;
    if(Keyboard.GetState().IsKeyDown(Keys.Up))
            {
                if (_cantWalk == false)
                {
                    _position.Y--;
                }
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.Down))
            {
                if (_cantWalk == false)
                {
                    _position.Y++;
                }
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                if (_cantWalk == false)
                {
                    _position.X++;
                }
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                if (_cantWalk == false)
                {
                    _position.X--;
                }
            }

        }

        public void setCantWalk(bool walk)
        {
            _cantWalk = walk;
        }

Thinks to help me

I’m sorry, but I’m had a hard time understanding the english in your post. However, from what I gather, the problem you have is that your collision test isn’t providing any data about the collision, other than “Yes, Collision”, or “No Collision”.

Like you say, you need to find the collision direction, or whats called the “normal” of the collision. The normal of the collision is the vector that is perpendicular to the intersected side of the rectangle. So, if you walked left, into the vertical right side of the rectangle, the normal would be vector pointing in the X direction.

There are a bazillion ways to go about calculating this, and they all depend on your use case. I don’t really understand your use case, but I can point you to some useful links…


I don’t know how to know the collision direction

Hi.

There is a very simple solution to your problem.

Store the last position of things.

Consider the problem always as a pair of events that occur over a mandatory time that is more then one frame as follows (To say consider the past frame and present frame as the collision event itself).
for example: two frames.

In Frame 1
Fred our player is not colliding with anything he is at position 5,1

In Frame2
Freds (Last) position is then set to 5,1
His (New) position is now at 5,2
and…
We find we have a collision.
but…
No information for direction.

However by simply saving that last position we will soon find we have a virtual treasure trove of information.

Now to work this out logically… There are 2 possibility’s he collided with something or something collided with him.

If the object he collides with is static like a wall or a rock we know he did the colliding.
We can use his position now minus his last position to know his direction of travel.
5,2 - 5,1 = 0,1
Direction = CurrentPosition - LastPosition;

If the object is colliding into him then that object should enter into his last position. Or at least have moved from its last position towards his Freds last position in its new position.
Which in this case is, what we need to know, when Fred is standing still && someone threw a rock && hit him with it. We can find the rocks directional motion in the same manner as fred’s.

We also know that Fred is standing still, when (if) his last position is (==) his current position.
This should always apply for static objects (like a wall) if you mark them unmovable or not.

We can also find Normals from directions by simply calling .Normalize() on them.
But by now you should see that you don’t really need them.

There can be a possible 3rd situation were fred and a object are colliding into each other.
In that case both basically must move from there last positions towards or closer to each others last positions in there new positions. As that is a requirement that defines the condition.

So with only recording last positions of things you have tons of information about direction with or without actual collisions that you can use for many sorts of situations including this one.

in general the first thing you update on a object is its last position.
UpdateMe()
{
LastPosition = CurrentPosition; // this is actually at that moment left over from the last frame

// then calculate the new current position by its physics the user input or whatever.

Note by this pattern in frame 1 his last position may have been 5,1 or anything but he would have had a last position then as well.

When you initialize objects with position at game or level start simply set last position to be currentposition on creation and your then setup to use that past data to see… (puns and hard luck songs aside!) Were you are going to depends on were you came from. I.e. you find momentum direction from time and position.