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

Author Topic: TileMap Collision. Help please!!  (Read 1654 times)

0 Members and 2 Guests are viewing this topic.

nikiblauer

  • Newbie
  • *
  • Posts: 2
    • View Profile
TileMap Collision. Help please!!
« on: October 13, 2016, 03:35:00 pm »
Hello and I wanted to ask you, if you could help me with my tilemap-collision problem i have.
I don't know what i have done wrong, but the collision on the x axis doesn't work.Please help me !!!!

 I took this from the "SFML Game Development by example" book.

Here is the collision checking:


Checkcollision:

void EntityBase::CheckCollisions() {
   Map* gameMap = m_gamemap;
   unsigned int tileSize = gameMap->GetTileSize();
   int fromX = floor(m_AABB.left / tileSize);
   int toX = floor((m_AABB.left + m_AABB.width) / tileSize);
   int fromY = floor(m_AABB.top / tileSize);
   int toY = floor((m_AABB.top + m_AABB.height) / tileSize);

   for (int x = fromX; x <= toX; ++x) {
      for (int y = fromY; y <= toY; ++y) {
         Tile* tile = gameMap->GetTile(x, y);
         if (!tile) { continue; }

         sf::FloatRect tileBounds(x * tileSize, y * tileSize, tileSize, tileSize);
         sf::FloatRect intersection;
         m_AABB.intersects(tileBounds, intersection);

         float area = intersection.width * intersection.height;
         

         CollisionElement e(area, tile->properties, tileBounds);
         m_collisions.emplace_back(e);
         std::cout << m_collisions.size() << std::endl;
      }
   }
}



Resolving it:

void EntityBase::ResolveCollisions() {
   if (!m_collisions.empty()) {
      std::sort(m_collisions.begin(), m_collisions.end(), SortCollisions);
      Map* gameMap = m_gamemap;
      unsigned int tileSize = gameMap->GetTileSize();
      for (auto &itr : m_collisions) {
         
         
         
         float xDiff = (m_AABB.left + (m_AABB.width / 2)) - (itr.m_tileBounds.left + (itr.m_tileBounds.width / 2));
         float yDiff = (m_AABB.top + (m_AABB.height / 2)) - (itr.m_tileBounds.top + (itr.m_tileBounds.height / 2));
         float resolve = 0;
         if (abs(xDiff) > abs(yDiff)) {
            if (xDiff > 0) {
               resolve = (itr.m_tileBounds.left + tileSize) - m_AABB.left;
            }
            else {
               resolve = -((m_AABB.left + m_AABB.width) - itr.m_tileBounds.left);
            }
            Move(resolve, 0);
            m_velocity.x = 0;
            m_collidingOnX = true;
         }
         else {
            if (yDiff > 0) {
               resolve = (itr.m_tileBounds.top + tileSize) - m_AABB.top;
            }
            else {
               resolve = -((m_AABB.top + m_AABB.height) - itr.m_tileBounds.top);
            }
            Move(0, resolve);
            m_velocity.y = 0;
            if (m_collidingOnY) { continue; }
            m_referenceTile = itr.m_tile;
            m_collidingOnY = true;
         }
      }
      m_collisions.clear();
   }
   if (!m_collidingOnY) { m_referenceTile = nullptr; }
}

//m_AABB is the player's floatrect

hope you can help me thanks.

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
Re: TileMap Collision. Help please!!
« Reply #1 on: October 13, 2016, 06:11:11 pm »
Please use the code tags when posting code please.

Before colliding severals objects you need to first understand the collision theory.

Hint: Start first with two objects, both AABB, google AABB vs AABB collision, and using ::getGlobalBounds provided by the SFML api you can achieve this.

Then try with more objects.

If you will done advance physics better use Box2D.
« Last Edit: October 13, 2016, 06:15:40 pm by DarkRoku »
I would like a spanish/latin community...
Problems building for Android? Look here

nikiblauer

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: TileMap Collision. Help please!!
« Reply #2 on: October 14, 2016, 08:16:15 am »
Thanks, I'll do this

 

anything