Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Made a collision tile map = character sometimes moves through the collision  (Read 1911 times)

0 Members and 1 Guest are viewing this topic.

vicot

  • Newbie
  • *
  • Posts: 1
    • View Profile
Hey. I'm making a 2D based platformer using C++ and SFML. I made a collision map based on a tile map, but my guy sometimes moves through the collision (if I tap a key couple times it would get stuck inside a collision, messing up my other controls because they are based on collision). Any tips how to fix this? It makes me unable to do gravity, because the guy keeps falling into collision.

Here's my code:

for (int i = 0; i < collisionArray.size(); i++)
      {
         if (playerBoundingBox.intersects(collisionArray[i].getGlobalBounds()))
         {
            player.cialo.setFillColor(Color::Red);
            player.canMove = false;
            cout << "collision" << endl;
            break;
         }
         else
         {
            player.cialo.setFillColor(Color::Blue);
            player.canMove = true;
            cout << "not collision" << endl;
         }
      };


This is my collision code, it's based on an array that I generate from my collision tile map.

Vector2f movement(0.0f, 0.0f);
       
        if (Keyboard::isKeyPressed(Keyboard::W))
        {
                if (canMove == true)
                        movement.y -= velocity * deltaTime;
                else
                        body.move(0.0f, 5.0f);
        }
               
        if (Keyboard::isKeyPressed(Keyboard::S))
        {
                if (canMove == true)
                        movement.y += velocity * deltaTime;
                else
                        body.move(0.0f, -5.0f);
        }
               
        if (Keyboard::isKeyPressed(Keyboard::A))
        {
                if (canMove == true)
                        movement.x -= velocity * deltaTime;
                else
                        body.move(5.0f, 0.0f);
        }
               
        if (Keyboard::isKeyPressed(Keyboard::D))
        {
                if (canMove == true)
                        movement.x += velocity * deltaTime;
                else
                        body.move(-5.0f, 0.0f);

        }

This is my movement code.

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
There are a couple of articles that might be worth read. This one covers basic collision detection and resolution with SFML. The second one might be useful to apply to your player, so that you can change state when jumping/walking and only apply gravity when in the air. HTH

 

anything