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

Author Topic: Need help with collision and gravity  (Read 1641 times)

0 Members and 1 Guest are viewing this topic.

NullFrag

  • Newbie
  • *
  • Posts: 1
    • View Profile
Need help with collision and gravity
« on: September 30, 2017, 11:33:52 pm »
Hello to anyone reading this. I am currently having an issue implementing collision and gravity in my coding. I am assuming that it is because of what I'm trying to apply it to and/or my method of implementing it.

// 32 x 32 is the size of all rectangles

bool onground = true;
float gravity;

int main() {
    sf::RenderWindow win(sf::VideoMode(1600, 960), "Example");
    win.setVerticalSyncEnabled(true);
    win.setKeyRepeatEnabled(true);

    sf::RectangleShape Player;
    Player.setFillColor(sf::Color::White);
    Player.setSize(sf::Vector2f(32, 32));
    Player.setPosition(18 * 32, 57 * 32);

    sf::RectangleShape Boxes[60][35];

    while (win.isOpen()) {
        sf::Event event;

        for (int MapY = 0; MapY < 60; ++MapY) {
            for (int MapX = 0; MapX < 35; ++MapX) {
                switch (TheLevel[MapY][MapX]) {
                case 1:
                    Boxes[MapY][MapX].setSize(sf::Vector2f(32, 32));
                    Boxes[MapY][MapX].setFillColor(sf::Color::Yellow);
                    Boxes[MapY][MapX].setPosition(MapX * 32, MapY * 32);
                    break;
             }

             //All side collision
             if (Player.getPosition().x < Boxes[MapY][MapX].getPosition().x + 32) {
                    //Player.move(1, 0); ?
                } else if (Player.getPosition().x + 32 > Boxes[MapY][MapX].getPosition().x) {
                    //Player.move(-1, 0); ?
                } else if (Player.getPosition().y < Boxes[MapY][MapX].getPosition().y + 32) {
                    //Player.move(0, 1); ?
                } else if (Player.getPosition().y + 32 > Boxes[MapY][MapX].getPosition().y) {
                    //Player.move(0, -1); ?
                    gravity = 0;
                    onground = true;
                }
           }
        }

        while (win.pollEvent(event)) {
            switch (event.type) {
            case sf::Event::Closed :
                win.close();
                break;
            }
        }

       if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
            if (onground) {
                //Player.move(0, -64.0f); ?
                //gravity = 4.0f; ?
                onground = false;
            }
        } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
            Player.move(-movespeed, 0);
        } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
            Player.move(movespeed, 0);
        }

       Player.move(0, gravity);

        win.clear();

        for (int MapY = 0; MapY < 60; ++MapY) {
            for (int MapX = 0; MapX < 35; ++MapX) {
                win.draw(Boxes[MapY][MapX]);
            }
        }

        win.draw(Player);

        win.display();

    }
    return 0;
}
 
(Sorry if my code is a mess.)

From what I've looked into, one of the solution is to use rect.getGlobalBounds().intersects(rect2.getGlobalBounds()). But that will resolve it whenever ANY collision occurs. What I am looking for is a way to detect which side of the player is having a collision against the map and implement gravity. I would appreciate some sample coding.

Thank you to those who are willing to help me.

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: Need help with collision and gravity
« Reply #1 on: October 01, 2017, 11:35:10 am »
Have a look at this post I made a while back
http://trederia.blogspot.co.uk/2016/02/2d-physics-101-pong.html

It describes calculating the collision 'manifold' which tells you the direction of a collision. It also contains some examples at the bottom written in sfml