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

Author Topic: Problemm with the move function  (Read 986 times)

0 Members and 1 Guest are viewing this topic.

Watcharakiete

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Problemm with the move function
« on: October 20, 2017, 07:15:51 pm »
Hi everyone I'me new with SFML and have a ploblem with the function called move()  :-\

I've set the class and function in it is

void Collider::Move(float dx, float dy)
{
        body.move(dx, dy);
}

in this class (Collider), it has boolean called CheckCollision

bool Collider::CheckCollision(Collider & other,float push)
{
        sf::Vector2f otherPosition = other.GetPosition();
        sf::Vector2f otherHalfsize = other.GetHalfsize();
        sf::Vector2f thisPosition = GetPosition();
        sf::Vector2f thisHalfsize = GetHalfsize();

        float DeltaX = otherPosition.x - thisPosition.x;
        float DeltaY = otherPosition.y - thisPosition.y;

        float intersectX = (abs(DeltaX)) - (otherHalfsize.x+ thisHalfsize.x);
        float intersectY = (abs(DeltaY)) - (otherHalfsize.y + thisHalfsize.y);

       
       
        Move(50.0f, 0.0f);
       
       
        if (intersectX < 0.0f && intersectY < 0.0f)
        {
                push = std::min(std::max(push, 0.0f), 1.0f);
               
                if (intersectX > intersectY)
                {      
                       
                        if (DeltaX > 0.0f)
                        {
                        a = 1;
                                Move(intersectX*(1.0f - push), 0.0f);
                                other.Move(-intersectX*push, 0.0f);
                        }

                        else
                        {
                                a = 2;
                                Move(-intersectX*(1.0f - push), 0.0f);
                                other.Move(intersectX*push, 0.0f);
                       
                        }
                }
                else
                {
                       
                        if (DeltaY > 0.0f)
                        {
                                a = 3;
                                Move(0.0f,intersectY*(1.0f - push));
                                other.Move(0.0f,-intersectY*push );
                        }

                        else
                        {
                                a = 4;
                                Move(0.0f,-intersectY*(1.0f - push));
                                other.Move(0.0f,intersectY*push);

                        }
               
                }
                return true;
        }




        return false;
}
 

And then in the main.cpp I called the 2 rectangleShapes to check if they collided with each other.
by using

box1.GetCollider().CheckCollision(player.GetCollider(),0.2f);

In the code you'll see that I try to call move  to the body which I've checked that a has changed from 1-4 (which means it works but not move anyof the rectangleShape)

Im still confusing this and can not solve. please help me.
« Last Edit: October 20, 2017, 09:29:50 pm by Laurent »

 

anything