1
\$\begingroup\$

After having a little look around about collisions. I can't seem to find what i'm doing wrong every time i touch the bottom of the tile it send's me through it and throws me out to the right of the tiles. I maybe going about this the wrong way if someone could help i'd be very grateful i'll copy all the relevant code down below.

void Game::update(float dt)
{
//check collision with world
vector<Tile>* world = level.getLevel();
for (int i = 0; i < (int)world->size(); i++)
{
    if ((*world)[i].isAlive())
    {
        //if "alive" check collision
        //world tile that are not alive don't do checks
        if (checkCollision(&mario, &(*world)[i]))
        {
            mario.collisionResponse(&(*world)[i]);
        }
    }

}



bool Game::checkCollision(MySprite* sprite, MySprite* sprite2)
{
 if (sprite->getAABB().left + sprite->getAABB().width <sprite2->getAABB().left)
    return false;
 if (sprite->getAABB().left > sprite2->getAABB().left + sprite2->getAABB().width)
    return false;
 if (sprite->getAABB().top + sprite->getAABB().height < sprite2->getAABB().top)
    return false;
 if (sprite->getAABB().top > sprite2->getAABB().top + sprite2->getAABB().height)
    return false;


return true;

}

Mario Class

void Mario::collisionResponse(MySprite* sp)
{


    //setPosition(getPosition().x, sp->getPosition().y - getSize().y);
    //velocity.y = 0;
    sf::Vector2f collide(getPosition().x - sp->getPosition().x, getPosition().y - sp->getPosition().y);

    //check left side
    if (abs(collide.x) > abs(collide.y))
    {

        if (collide.x < -5)
        {
            //right side
            setPosition(sp->getPosition().x - getSize().x-1, getPosition().y);
            velocity.x = 0;
        }
        if (collide.x > 5)
        {
            //left side
            setPosition(sp->getPosition().x + sp->getSize().x + 1, getPosition().y);
            velocity.x = 0;
        }
    }
    else
    {
        if (collide.y < -5)
        {
            setPosition(getPosition().x, sp->getPosition().y - getSize().y);
            velocity.y = 0;

        }
        else if (collide.y > 5)
        { 
            setPosition(getPosition().x, sp->getPosition().y  + getSize().y);
            velocity.y = 0;
            falling = false;
        }
    }

}
\$\endgroup\$
  • \$\begingroup\$ Are you sure there' no collision method in the AABB class? \$\endgroup\$ – Bálint Mar 19 '17 at 11:32
  • \$\begingroup\$ I'm sure that i'm just missing or i'm adding/subtracting something in the wrong way just trying to collide with the bottom of the tile then send the player falling \$\endgroup\$ – Cerberus Mar 19 '17 at 16:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.