4
\$\begingroup\$

I am sorry if this was already answered but I couldn't find the answer. So I am doing a tile based platformer and when I was making collision I had quiet a struggle I looked here and googled for answers but they all seems to be overcomplicated for such a simple task/game.

I did come up with this code and it works almost fine:

Rectangle boundRect = new Rectangle((int)position.X, (int)position.Y,
            (int)(sprite.Texture.Width * scale), (int)(sprite.Texture.Height * scale));

        Vector2 previousPosition = position;
        Vector2 previousVelocity = velocity;

        if (boundRect.Intersects(collider))
        {
            velocity = Vector2.Zero;
            position = previousPosition - previousVelocity;
        }

It works almost as good as I want, axcept few things. When I constantly move (i.e.) to left of a collider object(tile) my player sprite is shaking, if thats right word to use, for like 1-3 pixels from edge of object, also in this state I can't move any other direction (i.e. jumping up) till I stop my current movement.

So my question is basicly: "Can I improve this while keeping simplicity, or I need to use better(more complex) approach?"

P.S. Sorry for my English, it is not my native language.

\$\endgroup\$

3 Answers 3

2
\$\begingroup\$

You are going to need to do your move/collision in two steps, once for the horizontal direction, and another time for the vertical direction.

  1. Move Horizontal
  2. Check for horizontal collision - adjust position if needed
  3. Move Veritcal
  4. Check for veritcal collision - adjust position if needed

Doing the collision check like this should solve your stuttering issue. Checking both directions at once is fine for collision detection, however, when dealing with collision response it is best to do each direction individually.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks very much, I think that helped! This is how I did it now: link I also extended Rectangle a bit but thats not my code I find it somewhere on internet \$\endgroup\$
    – user30405
    May 7, 2013 at 18:08
1
\$\begingroup\$

If it's wobbling like you describe, then the game is drawing both before the collision occurs, and after the collision occurs. Make sure your Update function moves all of your objects first, and then handles collisions before that same Update call returns. That way, only the result of all collisions is drawn.

\$\endgroup\$
2
  • \$\begingroup\$ @RedPanda I agree. It seems you are checking for collision before you move your object on screen. \$\endgroup\$
    – jgallant
    May 7, 2013 at 11:17
  • \$\begingroup\$ @Jon yeah big thakns guys, I did checked it before moving, but thats resolved only half of the problem. I still can't figure why I can't move in other direction when colliding and moving in other direction at same time. \$\endgroup\$
    – user30405
    May 7, 2013 at 12:46
0
\$\begingroup\$

I think the reason your sprite is shaking is because you're moving it back from the object based on previousVelocity. I'm assuming that when you press or hold the movement, it sets the velocity. If this is true, then I think what's happening is:

  1. Your player is moving towards an obstacle.
  2. Player's next move intersects obstacle. (boundRect.Intersects(collider))
  3. You move player in the opposite direction. (position = previousPosition - previousVelocity;)
  4. Player is holding down button and velocity gets set again since the player is not against the obstacle.
  5. GOTO 1

Hence, your player would appear to bounce back and forth.

So, what you need to do is make the player go backwards to the point where they are resting against the obstacle instead of the full velocity. Then, ensure that when they have the button pressed down, the velocity doesn't get set if they are against something or it doesn't get set if there is an obstacle within the initial movement range (i.e. position + velocity).

\$\endgroup\$

You must log in to answer this question.