Asier

Member

6

1 Neutral

• Rank
Newbie

• Role
Programmer
• Interests
Art
Design
Production
Programming

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

1. Allow to slide rectangle avoiding corner collision

Hi Zakwayda! Yeah that implementation solved my problem, I can slide, because it checks the collision in terms of collision deep. My actually code is this one, ... move method ... Direction collisionDirection = Direction.NONE; // Check collisions for (Rectangle mapCollider : belongsToLevel.tiledMap.mergedColliders) { if (collider.intersects(mapCollider)) { collisionDirection = MathUtils.getRectangleDepthSideCollision(collider, mapCollider); break; } } if (direction == Direction.NORTH && collisionDirection == Direction.NORTH || direction == Direction.SOUTH && collisionDirection == Direction.SOUTH) { pos.x += xMove; } else if (direction == Direction.WEST && collisionDirection == Direction.WEST || direction == Direction.EAST && collisionDirection == Direction.EAST) { pos.y += yMove; } else { pos.x += xMove; pos.y += yMove; } } // Move method ends // Collision method in MathUtils.java /** * Gets the side of collision in depth between two rectangles. * @param collider * @param obstacle * @return */ public static Direction getRectangleDepthSideCollision(final Rectangle collider, final Rectangle obstacle) { int dx = (collider.x + collider.width / 2) - (obstacle.x + obstacle.width / 2); int dy = (collider.y + collider.height / 2) - (obstacle.y + obstacle.height / 2); int width = (collider.width + obstacle.width) / 2; int height = (collider.height + obstacle.height) / 2; int crossWidth = width * dy; int crossHeight= height * dx; // Detect if is collision (done with intersects) //if (Math.abs(dx) <= width && Math.abs(dy) <= height) { if (crossWidth > crossHeight) { if (crossWidth > -crossHeight) { System.out.println("Top!"); return Direction.NORTH; } else { System.out.println("Right!"); return Direction.EAST; } } else { if (crossWidth > -crossHeight) { System.out.println("Left!"); return Direction.WEST; } else { System.out.println("Bottom!"); return Direction.SOUTH; } } } I am very grateful for your help
2. Allow to slide rectangle avoiding corner collision

Hi guys! In the end I ended up with the next code, using the center of the rectangles. Do you see this correct? or is it inefficient? if (playerCollider.intersects(mapCollider)) { int dx = (collider.x + collider.width / 2) - (obstacle.x + obstacle.width / 2); int dy = (collider.y + collider.height / 2) - (obstacle.y + obstacle.height / 2); int width = (collider.width + obstacle.width) / 2; int height = (collider.height + obstacle.height) / 2; int crossWidth = width * dy; int crossHeight= height * dx; if (crossWidth > crossHeight) { if (crossWidth > -crossHeight) { System.out.println("Top!"); isCollisionTop = true; } else { System.out.println("Right!"); isCollisionRight = true; } } else { if (crossWidth > -crossHeight) { System.out.println("Left!"); isCollisionLeft = true; } else { System.out.println("Bottom!"); isCollisionBottom = true; } } } Thank youu!
3. Allow to slide rectangle avoiding corner collision

Hi guys, I have been testing and I have understand much better the logic of the collision. I also separated each side with a boolean and I check the x axis collision and then, the y axis collision. My only problem now is that when the right side or the top side collides, I am checking with >= and this causes true in all cases for right, not allowing to check the next else if for the other side 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; } } } I tried to limit the sides check, but I return to my first problem, The player can't slide. if (collider.intersects(mapCollider)) { if (direction == Direction.WEST || direction == Direction.EAST) { // Right collision if (collider.x + collider.width >= mapCollider.x && collider.x <= mapCollider.x) { System.out.println("right"); isCollisionRight = true; } // Left collision else if (collider.x <= mapCollider.x + mapCollider.width && collider.x + collider.width >= mapCollider.x) { System.out.println("left"); isCollisionLeft = true; } } if (direction == Direction.NORTH || direction == Direction.SOUTH) { // Top collision if (collider.y <= mapCollider.y + mapCollider.height && collider.y >= mapCollider.y) { System.out.println("top"); isCollisionTop = true; } // Bottom collision else if (collider.y <= mapCollider.y + mapCollider.height && collider.y + collider.height >= mapCollider.y) { System.out.println("bottom"); isCollisionBottom = true; } } break; } Thank you so much.
4. Allow to slide rectangle avoiding corner collision

Hello again Zakwayda, Yeah... I am sorry, (x=0, y=0) is top left corner and (x=x + width, y=y + height) the bottom right corner as you said. About the Rectagle class, I am using the java.awt.Rectangle Java class. As you say, I can use the x, y, width and heigth to calculate the right side with x + width and the bottom side y + width. About the code, as you said, if I collide for example with the right side, I won't be able to move to the left (and vice versa), due to isCollisionX=true for the both cases. So the right/left movements are not going to be able, and that is incorrect for me. Maybe I need to create a boolean for each side collision? For the direction of the player,I also have the followind code if this helps, where I can detect the direction of the player using the xMove and the yMove, which can be 1 or -1 pixel. if (yMove < 0) { direction = Direction.NORTH; } else if (yMove > 0) { direction = Direction.SOUTH; } if (xMove < 0) { direction = Direction.WEST; } else if (xMove > 0) { direction = Direction.EAST; } I am also thinking about what you said of the max, min of the rectagles, for the collision of the right side, and the bottom side, I think that I need to use x + width and y + height to compare this collision. I tried with (playerCollider.x + playerCollider.width) > mapCollider.x (for right collision) and playerCollider.x < (mapCollider.x + mapCollider.width) for the left, but I am not able to make is work. I think that I making a mess Thank you so much.
5. Allow to slide rectangle avoiding corner collision

Hello Zakwayda, The x and y corner of the colliders represent like so, The x and y represent the top left corners of the rectangles, and the top right corners, the max width and height. Then, about the movement of the player, I am reading the keyboard like this, and moving 1 pixel (int) to the desired direction. (The method move(xMove, yMove) has the collission code that I post previously. @Override public void update() { super.update(); int xMove = 0; int yMove = 0; // Read keyboard events if (keyboard.up) { yMove--; } else if (keyboard.down) { yMove++; } else if (keyboard.left) { xMove--; } else if (keyboard.right) { xMove++; } if (xMove != 0 || yMove != 0) { move(xMove, yMove); } } Thank you so much for your response.
6. Allow to slide rectangle avoiding corner collision

Hello, 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. 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. 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. boolean isCollisionX = false; boolean isCollisionY = false; // Check collisions for (Rectangle mapCollider : belongsToLevel.tiledMap.mergedColliders) { if (playerCollider.intersects(mapCollider)) { // Right collision if (playerCollider.x - mapCollider.x < 0) { isCollisionX = true; } // Left collision if (playerCollider.x - mapCollider.x > 0) { isCollisionX = true; } // Top collision if (playerCollider.y - mapCollider.y > 0) { isCollisionY = true; } // Bottom collision if (playerCollider.y - mapCollider.y < 0) { isCollisionY = true; } break; } } if (!isCollisionX) { pos.x += xMove; } if (!isCollisionY) { pos.y += yMove; } Thank you in advance.