Below is the whole program. It is a very small and simple program made to test collisions.
You have a player rectangle, and two block rectangles. If the player collides with either of the blocks, based on the direction he is coming from, his x or y coordinate gets pushed back.
So, the collision works fine in that sense...
However, for instance, if you are moving up and hit a block from the bottom (holding the up key), then press and hold the left key (while still holding the up key), and then let go of the up key, the player gets pushed all the way to the right, past the block.I know why this is happening, it's obvious. It is simply executing the collision routine for when you press left. But I put flags in, such as
movingUp,
movingDown, etc., to combat this, and yet it still happens.
Also, in the code below, where it checks the key and the direction you're moving in, I even tried checking false for the other three directions, and it still doesn't solve anything.
For instance:
(if (playerRect.getGlobalBounds().intersects(blockRect[i].getGlobalBounds()) && movingUp && !movingDown && !movingLeft && !movingRight)
Feel free to copy and paste this code, and compile it yourself to see what I am talking about.
The collision works, but I don't know what logic I need to stop this collision bug from happening. You'd think the flags that I have would work, but they're not.
Does anyone know WHY this is happening?
Any help would be greatly appreciated. Thank you for your time.
#include <SFML/Graphics.hpp>
using namespace sf;
int main()
{
RectangleShape playerRect;
RectangleShape blockRect[2];
float x = 128;
float y = 120;
bool movingUp = false;
bool movingDown = false;
bool movingLeft = false;
bool movingRight = false;
playerRect.setSize(Vector2f(16,16));
playerRect.setFillColor(Color::Yellow);
playerRect.setPosition(x,y);
blockRect[0].setSize(Vector2f(16,16));
blockRect[0].setFillColor(Color::Blue);
blockRect[0].setPosition(200,120);
blockRect[1].setSize(Vector2f(16,16));
blockRect[1].setFillColor(Color::Blue);
blockRect[1].setPosition(128,60);
RenderWindow window(VideoMode(256,240),"");
while (window.isOpen())
{
// Process events
Event event;
while (window.pollEvent(event))
{
// Close window: exit
if (event.type == Event::Closed)
window.close();
}
if (Keyboard::isKeyPressed(Keyboard::Up))
{
y -= 0.01;
movingUp = true;
movingDown = false;
movingLeft = false;
movingRight = false;
}
else if (Keyboard::isKeyPressed(Keyboard::Down))
{
y += 0.01;
movingUp = false;
movingDown = true;
movingLeft = false;
movingRight = false;
}
else if (Keyboard::isKeyPressed(Keyboard::Left))
{
x -= 0.01;
movingUp = false;
movingDown = false;
movingLeft = true;
movingRight = false;
}
else if (Keyboard::isKeyPressed(Keyboard::Right))
{
x += 0.01;
movingUp = false;
movingDown = false;
movingLeft = false;
movingRight = true;
}
for (int i = 0; i < 2; i++)
{
if (playerRect.getGlobalBounds().intersects(blockRect[i].getGlobalBounds()) && movingUp) y = blockRect[i].getPosition().y + 16;
if (playerRect.getGlobalBounds().intersects(blockRect[i].getGlobalBounds()) && movingDown) y = blockRect[i].getPosition().y - 16;
if (playerRect.getGlobalBounds().intersects(blockRect[i].getGlobalBounds()) && movingLeft) x = blockRect[i].getPosition().x + 16;
if (playerRect.getGlobalBounds().intersects(blockRect[i].getGlobalBounds()) && movingRight) x = blockRect[i].getPosition().x - 16;
}
playerRect.setPosition(x,y);
// Clear screen
window.clear();
// Draw the sprite
window.draw(playerRect);
window.draw(blockRect[0]);
window.draw(blockRect[1]);
// Update the window
window.display();
}
return 0;
}