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

Author Topic: Need help with move function  (Read 3605 times)

0 Members and 1 Guest are viewing this topic.

CowNation

  • Newbie
  • *
  • Posts: 20
    • View Profile
Need help with move function
« 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);

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Re: Need help with move function
« Reply #1 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.
Failing to succeed does not mean failing to progress!

CowNation

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Need help with move function
« Reply #2 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?

CowNation

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Need help with move function
« Reply #3 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
« Last Edit: May 03, 2018, 05:15:24 pm by CowNation »

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Re: Need help with move function
« Reply #4 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:
  • entity.move(xMovement, 0);
  • checkCollision(entity, collider);
  • // here you can check from which side entity collided (e.g. based on which key is pressed)
  • entity.move(0, yMovement);
  • checkCollision(entity, collider);
  • // here you can check again

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, so you can safely use && here.

Also depending on the implementation of PixelPerfect, it might already check for bounding box collision?
Failing to succeed does not mean failing to progress!

 

anything