1
General / Made a collision tile map = character sometimes moves through the collision
« on: May 07, 2017, 10:17:58 pm »
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:
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.
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;
}
};
{
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.