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

Author Topic: Collision problems  (Read 1097 times)

0 Members and 1 Guest are viewing this topic.

DogFromHome

  • Newbie
  • *
  • Posts: 2
    • View Profile
Collision problems
« on: February 18, 2014, 07:47:37 pm »
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?

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Collision problems
« Reply #1 on: February 18, 2014, 07:54:43 pm »
Quote
I can't move left unless I release the right(D) button.

Well if both keys are pressed then they are going to offset each other - meaning you won't move, you can handle events instead of realtime key states and then when an event fires set the state of your movement. That way when left is pressed after right then left will take priority.

Or maybe I am way off the mark and you better explain better what the issue is.  ;)
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

DogFromHome

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Collision problems
« Reply #2 on: February 20, 2014, 07:40:44 am »
That's the full code, you could test it yourself and see.
I can't really explain.
The best explanation is:
If I collide with right/left while holding D/A and the player is 1-99% higher/lower then wall, I can't move up/down.
You'd understand better if you'd test it yourself.

If you got a fix, could you just drop the code here?
I'm pretty good at reading not huge amounts of code.

 

anything