1
\$\begingroup\$

I am detecting each side of collision between two rectangles, and wen I collide them, lets say, with the left side, it also detects collision on the top side when I move to the top, and the downside when I move to the bottom. I think that is because it is detecting also the top/bottom corners.

enter image description here

How I can prevent this corner collision to allow to the player to slide?

Currently, my code is the next one (using xMove (-1 or 1) and yMove (-1 or 1) to know the player movement direction.

    if (collider.intersects(mapCollider)) {

            if (direction == Direction.WEST || direction == Direction.EAST) {
                // Right collision
                if (collider.x + collider.width >= mapCollider.x) {
                    isCollisionRight = true;
                }
                // Left collision
                else if (collider.x <= mapCollider.x + mapCollider.width) {
                    isCollisionLeft = true;
                }                   
            }

            if (direction == Direction.NORTH || direction == Direction.SOUTH) {
                // Top collision
                if (collider.y + collider.height >= mapCollider.y) {
                    isCollisionTop = true;
                }
                // Bottom collision
                else if (collider.y <= mapCollider.y + mapCollider.height) {
                    isCollisionBottom = true;
                }
            }

Best regards, Thank you in advance.

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

Your tests for top/bottom collision are only false if mapCollider.y == playerCollider.y, i.e. if their tops are exactly aligned. However, this doesn't indicate whether a collision is occurring in that direction, you need to be testing for the edges of your bounding boxes, not just a single point.

For example, for left/right collision, this should be something like:

// Right collision
if (
    (playerCollider.x + playerCollider.width) >= mapCollider.x 
    && (playerCollider.x + playerCollider.width) <= (mapCollider.x + mapCollider.width))
{
    isCollisionRight = true;
}

// Left collision
if (
    playerCollider.x <= (mapCollider.x + mapCollider.width)
    && playerCollider.x >= mapCollider.x )
{
    isCollisionLeft = true;
}

Note that only one of left/right collision should be true at a time, unless the player completely inside the object.

Also, some version of this is very likely what the intersect function you are calling is doing, is there a version that returns the intersection you can call? Or otherwise you might want to replace it with your own code.

\$\endgroup\$
  • \$\begingroup\$ Hi Jack, I edited the question for you to know where x and y coords are positioned on the rectangles, because the (0, 0) coords are not in the centers, they are in the top left corners. \$\endgroup\$ – Asier Oct 20 '18 at 11:03
  • \$\begingroup\$ @Asier: That doesn't actually affect the substance of the problem, you're still comparing a single point in the object when you need to be comparing edges. \$\endgroup\$ – Jack Aidley Oct 20 '18 at 11:12
  • \$\begingroup\$ Hi Jack, ok I undertand, I need also check with the sum of the x + width of the rectangles to check the side collisions, but i tried (playerCollider.x + playerCollider.width) > mapCollider.x for the right position, and it is detecting me all sides with right :/ \$\endgroup\$ – Asier Oct 20 '18 at 16:06
  • \$\begingroup\$ @Asier: The .intersects() call is supposed to filter out non-intersections. What is belongsToLevel.tiledMap.mergedColliders? How is it calculated? Also by "right collision" do you mean the object or the player is on the right? \$\endgroup\$ – Jack Aidley Oct 20 '18 at 16:17
  • \$\begingroup\$ Hi Jack, the mergedColliders are the map colliders (in this case the castle), and is calculated getting the rectangle that I created in Tiled, wich has a specific coordinates in the map. About the side collisions, when I say top/right/left/bottom I am refering to the player, because I want to detect the side of the player rect. \$\endgroup\$ – Asier Oct 20 '18 at 16:29

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.