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

Author Topic: Problems with collision [solved]  (Read 1159 times)

0 Members and 1 Guest are viewing this topic.

Anthis

  • Newbie
  • *
  • Posts: 1
    • View Profile
Problems with collision [solved]
« on: June 17, 2019, 08:32:05 am »
Hi everyone,
I'm trying to make proper collisions in my game, but unfortunately they don't work.


void APlayerController::MoveLeft(const float& DeltaTime)
{
        sf::Vector2f PawnLocation = Pawn->GetLocation(); //Get previous location
        Pawn->SetLocation(Pawn->GetLocation().x - Velocity * DeltaTime, Pawn->GetLocation().y); //Move sprite
        dynamic_cast<APlayer*> (Pawn)->SetDirection(EDirection::ELeft);
        if (dynamic_cast<APlayer*> (Pawn)->IsColliding())
        {
                Pawn->SetLocation(PawnLocation);  // if there's collision move back sprite
        }
}

This is example movement method from my player's class.


bool APlayer::IsColliding()
{
        auto Tiles = GGame::Instantiate().GetLevel()->GetCollidableTiles();
        for (auto Tile : Tiles)
        {
                auto TempTile = dynamic_cast<ATile*> (Tile);
                if (Tile)
                {
                        if (this->GetCollider().intersects(TempTile->GetCollider()))
                        {
                                return true;
                        }
                }
        }
        return false;
}
 

And this is method to check if player is colliding with any collidable tile on my map.

Any ideas what is wrong?
« Last Edit: June 17, 2019, 06:42:50 pm by Anthis »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Problems with collision [solved]
« Reply #1 on: June 18, 2019, 08:27:21 am »
You added [solved] to the title, so what was the solution?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/