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

Author Topic: Problem using intersects  (Read 4848 times)

0 Members and 1 Guest are viewing this topic.

tobbeman

  • Newbie
  • *
  • Posts: 12
    • View Profile
Problem using intersects
« on: September 17, 2017, 04:04:56 pm »
Hello!

Got an weird problem where my booleans are not being set.

                        sf::FloatRect blockRect = levelBlocks[i].getGlobalBounds();

                        //Handle x axis collision
                        if(blockRect.intersects(playerNextX) == 1){
                                std::cout << "Intersects!" << std::endl;
                                moveX = false;
                        }
                        //Handle y axis
                        if(blockRect.intersects(playerNextY) == 1) {
                                moveY = false;
                        }

                        if (moveX) {
                                player.setPosition(playerNextX.left, player.getPosition().y);
                        }
                        if (moveY) {
                                player.setPosition(player.getPosition().x, playerNextY.top);
                        }
 
Im trying to use the code above for collison checking. The problem is that even if the code enters the intersects statement and print, thus making move false, the character moves. When printing after the intersect statement the boolean is indead always true. This is very weird as I can set the move variables just fine when listening for keyboard events.

Hopefully someone know something!
« Last Edit: September 17, 2017, 04:11:13 pm by Laurent »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Problem using intersects
« Reply #1 on: September 17, 2017, 05:08:32 pm »
Run your application through the debugger and step through your code, that way you see when something changes.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

tobbeman

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Problem using intersects
« Reply #2 on: September 18, 2017, 08:08:28 pm »
This is superwierd, the debugger says that move bool is set correctly but player still moves.

Whole game loop:

while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }

                //Setup for calc collision
                sf::Vector2f playerPos = player.getPosition();
                playerNextX.left = playerPos.x;
                playerNextY.top = playerPos.y;
                moveX = false;
                moveY = false;

                //Handle input
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                {
                        playerNextX.left -= playerSpeed;
                        moveX = true;
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                {
                        playerNextX.left += playerSpeed;
                        moveX = true;
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                {
                        playerNextY.top -= playerSpeed;
                        moveY = true;
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                {
                        playerNextY.top += playerSpeed;
                        moveY = true;
                }

                //Start graphics process
                window.clear();
                //Handle level logic
                for (int i = 0; i < levelBlocks.size(); i++) {
                       
                        sf::FloatRect blockRect = levelBlocks[i].getGlobalBounds();

                        //Handle x axis collision
                        if(blockRect.intersects(playerNextX) == 1){
                                std::cout << "Intersects X!" << std::endl;
                                moveX = false;
                        }
                        //Handle y axis
                        if(blockRect.intersects(playerNextY) == 1) {
                                std::cout << "Intersects Y!" << std::endl;
                                moveY = false;
                        }

                        if (moveX) {
                                std::cout << "MOVING X!" << std::endl;
                                player.setPosition(playerNextX.left, player.getPosition().y);
                        }
                        if (moveY) {
                                player.setPosition(player.getPosition().x, playerNextY.top);
                        }

                        //Add to draw queue
                        window.draw(levelBlocks[i]);
                }
                window.draw(player);

                //Display level
                window.display();
                window.setFramerateLimit(30);
        }

        return 0;
}
Moving is also printed, despite debugger saying it does not enter if statement. Did I break C++?
Also, im noticing that blockrect.intersects is true as long as player is on the same "level", it does not actually intersect. Any ideas? Or should I try to compile on another computer maybe

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Problem using intersects
« Reply #3 on: September 18, 2017, 08:49:09 pm »
I don't see anything syntactically wrong with the posted code, as such I can't really believe your debugging accounts. Either you enter the if-body an execute the code or you don't, unless you're trying to debug in release mode, where the debugger might jump code section due to missing debug symbols.

You shouldn't call setFramerateLimit every frame.
Intersect returns a boolean, so you shouldn't check against 1, actually you shouldn't compare it to anything as it is by itself already a boolean.
I probably would use the move() method, that way you don't have to calculate the relative position manually.
Instead of manually keeping track of the player rect, you could simply get the bounds, unless you want to manipulate the bounds, but then I'd suggest to create your own entity class, that holds a sprite and can return the correct bounds.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

tobbeman

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Problem using intersects
« Reply #4 on: September 19, 2017, 04:53:45 pm »
Welp, I fixed it. Running the move code outside the block render worked :/. Silly mistake!

I still have problem using globalBounds though, I intersect if I every enter the same axis, meaning I can't stand besides an levelblock.

Illustrated:

player = p
block = x
can't enter = d
empty = -

xxddd
xp---
d----
d----
d----

Any idea?

tobbeman

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Problem using intersects
« Reply #5 on: September 19, 2017, 07:26:47 pm »
Fixed it! I did not move the nextX box properly and its y was always at 0, thanks for your help!  ;D

 

anything