0
\$\begingroup\$

Right now I am trying to make a 2d collision response system for a platformer, and the algorithm I have for it works quite well. The only problem is that if the player is standing exactly on a tile, he cannot move. I think this is related to the fact that the manager is checking if he is colliding on the X axis, instead of the Y Axis.

I have thought of making one big AABB for a check, using all applicable tiles, but I feel as if there is a simpler way. I don't really understand normals, so I have not tried anything with them yet.

Right now, I am getting the centers of both objects, the distance inbetween them, and the minimum distance between them, returning the overlap. And then applying the overlap to the players position. It looks like this

sf::Vector2f centerA( 
  AABBFirst.left + (AABBFirst.width  / 2), 
  AABBFirst.top  + (AABBFirst.height / 2));
sf::Vector2f centerB( 
  AABBSecond.left + (AABBSecond.width  / 2), 
  AABBSecond.top  + (AABBSecond.height / 2));

sf::Vector2f distance(centerA.x - centerB.x, centerA.y - centerB.y);
sf::Vector2f minDistance(
  (AABBFirst.width  / 2) + (AABBSecond.width  / 2), 
  (AABBFirst.height / 2) + (AABBSecond.height / 2));

return sf::Vector2f(
  distance.x > 0 ? minDistance.x - distance.x : -minDistance.x - distance.x, 
  distance.y > 0 ? minDistance.y - distance.y : -minDistance.y - distance.y);

I apologize if this question has already been asked and answered before, I have not found the proper terms to search for.

\$\endgroup\$
0
\$\begingroup\$

Collision problems are a little bit complicated but here:

My answer to another question similar to yours: Link

\$\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.