SFML community forums

Help => Graphics => Topic started by: CowNation on May 03, 2018, 02:50:23 am

Title: Need help with move function
Post by: CowNation on May 03, 2018, 02:50:23 am
Can someone help me with my move function for my main sprite?
My code:
    void Move(RenderWindow* window, Vector2i UPDOWN, Vector2i LEFTRIGHT, Vector2f UPDOWNSPEED, Vector2f LEFTRIGHTSPEED, Sprite collider) {
        if (window->hasFocus()) {
            if (GetAsyncKeyState(UPDOWN.x) && collisionDir != Up) {
                if (collisionDir == None && Collision::PixelPerfectTest(sprite, collider)) { collisionDir = Up; }
                else {
                    sprite.move(0.0f, UPDOWNSPEED.x);
                    //Game::World::tileMap.setCamera(Game::World::tileMap.getCamera() + sf::Vector2f(0.0f, UPDOWNSPEED.x));
                }
            }
            if (GetAsyncKeyState(UPDOWN.y) && collisionDir != Down) {
                if (collisionDir == None && Collision::PixelPerfectTest(sprite, collider)) { collisionDir = Down; }
                else{
                    sprite.move(0.0f, UPDOWNSPEED.y);
                    //Game::World::tileMap.setCamera(Game::World::tileMap.getCamera() + sf::Vector2f(0.0f, UPDOWNSPEED.y));
                }
            }
            if (GetAsyncKeyState(LEFTRIGHT.x) && collisionDir != Left) {
                if (collisionDir == None && Collision::PixelPerfectTest(sprite, collider)) { collisionDir = Left; }
                else {
                    sprite.move(LEFTRIGHTSPEED.x, 0.0f);
                    //Game::World::tileMap.setCamera(Game::World::tileMap.getCamera() + sf::Vector2f(LEFTRIGHTSPEED.x, 0.0f));
                }
            }
            if (GetAsyncKeyState(LEFTRIGHT.y) && collisionDir != Right) {
                if (collisionDir == None && Collision::PixelPerfectTest(sprite, collider)) { collisionDir = Right; }
                else {
                    sprite.move(LEFTRIGHTSPEED.y, 0.0f);
                    //Game::World::tileMap.setCamera(Game::World::tileMap.getCamera() + sf::Vector2f(LEFTRIGHTSPEED.y, 0.0f));
                }
            }
            if (!Collision::PixelPerfectTest(sprite, collider)) { collisionDir = None; }
        }
    }
all of the collisionDir is for detecting which side the collider is at
if anyone has a better way i'd love to do it
if you switch positions quick enough, you can travel through a collider
and if you walk diagnally you can travel with them
any suggestions for better way to detect which side the collider is at?
or just a better way to do movement in general, i guess
here is proper use for calling Move func btw
Game::Entity::g_eLocal.Move(window, Vector2i(VK_UP, VK_DOWN), Vector2i(VK_LEFT, VK_RIGHT), Vector2f(-5.0f, 5.0f), Vector2f(-5.0f, 5.0f), Wall);
Title: Re: Need help with move function
Post by: Geheim on May 03, 2018, 08:02:49 am
One way is to seperate your movement:
You first move only left and right, check for collision, then you only move up and down and check for collision again.
This way you can easily detect from which side you collided. Keep in mind that you will now spend double the processing power for your collision which in your case is quite expensive (PixelPerfect).

You might consider first testing for normal BoundingBox collision and only go PixelPerfect if needed.
Title: Re: Need help with move function
Post by: CowNation on May 03, 2018, 05:00:48 pm
Thanks for the response!
Question, what do you mean exactly by testing left & right then testing up & down?
Title: Re: Need help with move function
Post by: CowNation on May 03, 2018, 05:13:49 pm
One way is to seperate your movement:
You first move only left and right, check for collision, then you only move up and down and check for collision again.
This way you can easily detect from which side you collided. Keep in mind that you will now spend double the processing power for your collision which in your case is quite expensive (PixelPerfect).

You might consider first testing for normal BoundingBox collision and only go PixelPerfect if needed.

And as for pixel perfect collision, would it be better if i nested it into another if statement so it didnt execute constantly?

if (sprite.getGlobalBounds().intsects(collider.getGlobalBounds()){ if PixelPerfectCollision(sprite, collider){ //* sick codes*/ } }
soz if any typos, am away from my pc
Title: Re: Need help with move function
Post by: Geheim on May 03, 2018, 08:21:16 pm
Question, what do you mean exactly by testing left & right then testing up & down?
I mean something like this:

And as for pixel perfect collision, would it be better if i nested it into another if statement so it didnt execute constantly?
If-statements in C++ follow the lazy evaluation strategy (https://en.wikipedia.org/wiki/Lazy_evaluation), so you can safely use && here.

Also depending on the implementation of PixelPerfect, it might already check for bounding box collision?