0
\$\begingroup\$

I'm needing help with the collision response of my player "Mario". I would like my player to be able to stand on the tiles that i add if he jumps as well as collide wit them if he walks into them i'll show the code below. I'm using AABB collision i think that's all the information that you need, but no doubt i'll miss something if you need anything more then please just ask. Thanks for taking the time to read and help out if you can.

in the game.cpp

//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;

 }

in the mario.cpp

#include "Mario.h"

 Mario::Mario(sf::RenderWindow* hwnd, Input* input, const sf::Vector2f &size) : MySprite(size)
 {
scale = 200.f;
gravity = 3.5f * scale;

falling = true;
animated = true;

window = hwnd;
in = input;

walkRight.init(0, 0, 15, 21, 4, 0.3f);
walkLeft.init(0, 0, -15, 21, 4, 0.3f);
swimRight.init(0, 22, 15, 20, 3, 0.2f);
swimLeft.init(0, 22, -15, 20, 3, 0.2f);
duck.init(0, 41, 15, 20, 2, 0.6f);

currentAnimation = &walkRight; 
setTextureRect(currentAnimation->getCurrentFrame());


}
Mario::~Mario()
{

}
void Mario::update(float dt)
{
//collision with wall while jumping
if (getPosition().x < 0)
{
    velocity.x = 0;
    setPosition(0, getPosition().y);
}
else if (getPosition().x > window->getSize().x - getSize().x)
{
    velocity.x = 0;
    setPosition(window->getSize().x - getSize().x, getPosition().y);
}
if (getPosition().y < 0)
{
    velocity.y = 0;
}
else if (getPosition().y > window->getSize().y - getSize().x)
{
    velocity.y = 0;
}

//falling
if (falling)
{
    move(velocity*dt);
    velocity.y += (gravity)*dt;

}
if (getPosition().y >= 500)
{
    falling = false;
    setPosition(getPosition().x, 500);
}

if (animated == true)
{
    // ANIMATION //
    elapsedTime += dt;
    if (elapsedTime >= currentAnimation->getAniTime())
    {
        //next frame
        currentAnimation->nextFrame();
        setTextureRect(currentAnimation->getCurrentFrame());
        elapsedTime = 0;
    }
}


// INPUT //
if (in->isKeyDown(sf::Keyboard::RShift))
{
    // point to current animation 
    currentAnimation = &swimRight;
}
if (in->isKeyDown(sf::Keyboard::LShift))
{
    // point to current animation 
    currentAnimation = &swimLeft;
}

//jump
if (in->isKeyDown(sf::Keyboard::Space) && !falling)
{
    velocity.y = - 2.0f * scale;
    falling =  true;


}
/*if (in->isKeyDown(sf::Keyboard::Space)==false)
{
    falling = false;
}*/
if (in->isKeyDown(sf::Keyboard::Right))
{

    if (getPosition().x < window->getSize().x - getSize().x)
    {
        currentAnimation = &walkRight;
        velocity.x = 100;
        move(velocity.x * dt, 0);

    }
}

if (in->isKeyDown(sf::Keyboard::Left))
{
    //if (getPosition().x < window->getSize().x - getSize().x)
    //{
        velocity.x = 100;

        currentAnimation = &walkLeft;
        move(-velocity.x * dt, 0);
        //if (in->setKeyUp(sf::Keyboard::Left))
        //{
            //animated = !animated;
        //}

    //}

}
if (in->isKeyDown(sf::Keyboard::P))
{
    //in->setKeyUp(sf::Keyboard::BackSpace);
    animated = !animated;
}
updateAABB();
}

void Mario::collisionResponse(MySprite* sp)
{

//problems here
}
\$\endgroup\$
2
\$\begingroup\$

Usually if you have a tiled level, you can calculate from World coordinates of your character the index of your tile by dividing your coordinates by tile size.

Example:

your character is at position (123, 78) and your tilesize is 32 px. Then the tile your character is occupying is (3,3) (123/32=3.xx; 78/32=3.xx)

so on moving to right you want to position your character at 130, 78 now he is walking into tile (4,3) (130/32=4.xx) So before you make the move you need to check if tile (4,3) is occupied.

| improve this answer | |
\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.