#Rectangle collision happens where it shouldn´t

1 messages · Page 1 of 1 (latest)

pastel grove
#

I am working on a collision system for a game, i am handling it with Rectangles and the intersects() function. However the collision is also happening a little too far away from where the actual collision is supposed to happen. In the picture you can see the closest i can get to the machines.

Rectangle playerBox = new Rectangle(hitboxLeft, hitboxTop, hitboxWidth, hitboxHeight);
                    boolean canMoveUp = true;
                    
                    for(int i = 0; i < MapLoader.machineCoordinate.length; i++) {
                        Rectangle machineHitbox = new Rectangle(MapLoader.machineCoordinate[i][0] - offsetX, MapLoader.machineCoordinate[i][1] - offsetY, MapLoader.machineCoordinate[i][2], MapLoader.machineCoordinate[i][3]);
                        
                        
                        if(playerBox.intersects(machineHitbox)) {
                            canMoveUp = false;
                        }
                    }
                    if(canMoveUp) {
                        up();
                        movementDirection = "UP";
                    } else canMoveUp = true;
 static int[][] machineCoordinate = {{434, 1223, 185, 95},
                                {434, 1423, 185, 95},
                                {434, 1623, 185, 95},
                                {434, 1823, 185, 95}};

offsetX and offsetY are basically the camera, the player is always in the center. They get substraced from the map coordinates.

vapid magnetBOT
# pastel grove I am working on a collision system for a game, i am handling it with Rectangles ...

Detected code, here are some useful tools:

Formatted code
Rectangle playerBox = new Rectangle(hitboxLeft, hitboxTop, hitboxWidth, hitboxHeight);
boolean canMoveUp = true;
for (int i = 0; i < MapLoader.machineCoordinate.length; i++) {
  Rectangle machineHitbox = new Rectangle(MapLoader.machineCoordinate[i] [0]  - offsetX, MapLoader.machineCoordinate[i] [1]  - offsetY, MapLoader.machineCoordinate[i] [2] , MapLoader.machineCoordinate[i] [3] );
  if (playerBox.intersects(machineHitbox)) {
    canMoveUp = false;
  }
}
if (canMoveUp) {
  up();
  movementDirection = "UP";
}
else canMoveUp = true;
static int [] [] machineCoordinate = {
    {
    434, 1223, 185, 95}
  , {
    434, 1423, 185, 95}
  , {
    434, 1623, 185, 95}
  , {
    434, 1823, 185, 95}
};
vapid magnetBOT
#

<@&987246399047479336> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

where it shouldn't. There could be several reasons why this is happening. Here are a few possible causes and solutions:

  1. Incorrect positioning or size of rectangles: Make sure that the rectangles are positioned and sized correctly. Check if the coordinates and dimensions of the rectangles are accurate.

  2. Inconsistent frame rate: If your game has a variable frame rate, the collision detection may not be consistent. Consider using a fixed time step for collision detection to ensure accuracy.

  3. Overlapping rectangles: If the rectangles overlap even slightly, the intersects() function will return true. Ensure that there is no overlap between the rectangles by adjusting their positions or sizes.

  4. Incorrect usage of intersects() function: Double-check how you are using the intersects() function. Make sure you are passing in the correct parameters and checking for collisions in the appropriate places.

  5. Floating-point precision errors: If you are using floating-point values for rectangle positions or sizes, there may be precision errors causing unexpected collisions. Consider rounding or truncating these values to avoid such issues.

  6. Other objects interfering with collision detection: Check if there are any other objects or elements in your game that could be interfering with collision detection, such as invisible barriers or incorrectly placed obstacles.

  7. Debugging tools: Utilize debugging tools to visualize and debug your collision system. This can help identify any issues with rectangle positions, sizes, or collisions.

By addressing these potential causes, you should be able to resolve the issue of collisions occurring where they shouldn't in your game's rectangle-based collision system.

merry sable
#

Have you tried debugging the values to see whether your coordinates are correct?

pastel grove
#

I am drawing the textures using the same coordinates, so yes

dull kiln
#

andu checked that the stepsize is not to big that it can actually get closer ?

merry sable
#

have you verified your hitboxheight? And it might be more interesting to use some constants to improve readability or use store machineBoundedBox as a rectangle rather than an int array.

Also if the camera is centered, shouldn't 2 and 3 be + your offset?

pastel grove
pastel grove
dull kiln
#

code wise your approach seems solid. Your code is way to verbose and needs a rewrite, but your approach seems correct. Either debug your code to see what's going on or add print commands so u can see what's happening

#

the only thing that's a tad wonky is that your giving an up move command in a method that 's suppose to check if it collides

#

( and the if that it is in, is obsolete, because u made it true by default , just need to make the false check end the collision detection )

pastel grove
dull kiln
#

readability

pastel grove
#

I never worked on large scale projects before meaning i never had to take a deeper look into clean code

pastel grove
dull kiln
#

and try to think before u make code, if u look on your code, U have code for a move.UP collision, and your going to be forced to make it for every axis u can move on

#

if the purpose is to detect a collision, that's all it should do, It does not need to care on what happens next, that's for the method that calls the detection