Hello there, I have a collision problem, I had a different one previously that I fixed by adding isMoving right/left etc.
But now, if I, for example collide with the wall that's on my right, my player is above(not completely) the top of the wall, I can't move left unless I release the right(D) button.
Here's my code w/o includes.
class variables{
public:
float windowWidth{ 854 }, windowHeight{ 480 }, pixelWidth{ 1280 }, pixelHeight{ 720 }, pSize{ 32 }, pWidth{ pSize }, pHeight{ pSize }, pSpeed{ 1 }, groundSize{ 128 }, groundWidth{ 256 }, groundHeight{ groundSize };
};
class Player{
variables vars;
public:
sf::RectangleShape rect;
bool moveUp{ false }, moveDown{ false }, moveLeft{ false }, moveRight{ false };
double top, bottom, left, right;
Player(sf::Vector2f position, sf::Vector2f size, sf::Color color){
rect.setPosition(position);
rect.setSize(size);
rect.setFillColor(color);
}
void Update(){
top = rect.getPosition().y;
bottom = rect.getPosition().y + rect.getSize().y;
left = rect.getPosition().x;
right = rect.getPosition().x + rect.getSize().x;
}
void Collide(sf::RectangleShape r){
double rLeft{ r.getPosition().x }, rRight{ r.getPosition().x + r.getSize().x }, rTop{ r.getPosition().y }, rBottom{ r.getPosition().y + r.getSize().y };
if (top<rBottom&&bottom>rBottom&&left<rRight&&right>rLeft&&moveUp == true) { rect.move(0, vars.pSpeed); }
if (bottom > rTop&&top < rTop&&left<rRight&&right>rLeft&&moveDown == true){ rect.move(0, -vars.pSpeed); }
if (right>rLeft&&left < rLeft&&top<rBottom&&bottom>rTop&&moveRight == true){ rect.move(-vars.pSpeed, 0); }
if (left<rRight&&right>rRight&&top<rBottom&&bottom>rTop&&moveLeft == true){ rect.move(vars.pSpeed, 0); }
}
};
class Ground{
variables vars;
public:
sf::RectangleShape rect;
Ground(sf::Vector2f position, sf::Vector2f size, sf::Color color){
rect.setPosition(position);
rect.setSize(size);
rect.setFillColor(color);
}
};
int main(){
variables vars;
Player player(sf::Vector2f(100,100), sf::Vector2f(vars.pWidth, vars.pHeight), sf::Color::Red);
sf::RenderWindow window(sf::VideoMode(vars.windowWidth, vars.windowHeight), "SPEIS INVEIDRZ!!!1!");
while (window.isOpen()){
sf::Event Event;
while (window.pollEvent(Event)){
if (Event.type == sf::Event::Closed){
window.close();
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)){
player.moveLeft=true;
player.rect.move(-vars.pSpeed, 0);
}
else{ player.moveLeft = false; }
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
player.moveRight=true;
player.rect.move(vars.pSpeed, 0);
}
else{ player.moveRight = false; }
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
player.moveUp=true;
player.rect.move(0, -vars.pSpeed);
}
else{ player.moveUp = false; }
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)){
player.moveDown=true;
player.rect.move(0, vars.pSpeed);
}
else{ player.moveDown = false; }
window.clear();
player.Update();
for (int i = 0; i < 5; i++){
Ground g(sf::Vector2f(i*vars.groundWidth, 500), sf::Vector2f(vars.groundWidth, vars.groundHeight), sf::Color::Blue);
window.draw(g.rect);
player.Collide(g.rect);
}
sf::View view(sf::FloatRect(player.rect.getPosition().x - vars.pixelWidth/2+vars.pWidth/2, player.rect.getPosition().y - vars.pixelHeight/2+vars.pHeight/2, vars.pixelWidth, vars.pixelHeight));
std::cout << player.rect.getPosition().x << " " << player.rect.getPosition().y << std::endl;
window.setView(view);
window.draw(player.rect);
window.display();
}
return 0;
}
So, any way to fix it?