2
\$\begingroup\$

I want a method, which takes two "ObjectBox" objects (A "ObjectBox" has .getX() .getY() .getZ() .getSizeX() .getSizeY() .getSizeZ() methods) as a parameter and return true if the two Boxes are colliding and false if they aren't.

So it should be something like this:

public static boolean checkCollision(ObjectBox box1, ObjectBox box2){
return //TRUE IF COLLIDING - FALSE IF NOT COLLIDING
}

I tried figuring it out but it seemed pretty hard to me.

\$\endgroup\$
  • \$\begingroup\$ You don't mention rotations. Are the cubes axis aligned? \$\endgroup\$ – MichaelHouse Aug 9 '13 at 13:15
  • \$\begingroup\$ There will be no rotation. \$\endgroup\$ – Sierox Aug 9 '13 at 14:02
10
\$\begingroup\$

This is called an AABB (Axis Aligned Bounding Box). The collision checks for these are pretty fast and simple. Basically you just check their relative positions and see if all three axes overlap:

public static boolean checkCollision(ObjectBox a, ObjectBox b){
{
   //check the X axis
   if(Math.abs(a.getX() - b.getX()) < a.getSizeX() + b.getSizeX())
   {
      //check the Y axis
      if(Math.abs(a.getY() - b.getY()) < a.getSizeY() + b.getSizeY())
      {
          //check the Z axis
          if(Math.abs(a.getZ() - b.getZ()) < a.getSizeZ() + b.getSizeZ())
          {
             return true;
          }
      }
   }

   return false;
} 

Only if all three axes overlap there is a collision. For example, if the cubes were overlapping on the X and Z axes and not the Y, it means that the cubes are above or below each other and not colliding. You can optimize the code above slightly, if there's an axis you're more likely to be colliding on, put that check first so you don't need to check the others.

Keep in mind, the above code assumes that the position is the center of the cube and the size is getting the half extents of the cube. If your getSize_ functions are returning the full dimension, you'll need to half them for the above to work properly.

\$\endgroup\$
  • \$\begingroup\$ Please can you tell me what getSize_ function for , generally how the above code works ? \$\endgroup\$ – Abdulrahman Apr 17 at 18:10
  • 1
    \$\begingroup\$ @Abdulrahman getSize_ returns a float value that represents the size of a box on whatever axis is being asked for. The description of what the code is doing is in the answer. Essentially, if there is any single 3D point which is shared between the two bounding boxes, they are considered to be overlapping. If you're still unclear, try removing dimensions. 1 dimension: two sections on a line are colliding if they touch or overlap. 2 dimensions: two boxes drawn on a piece of paper are colliding if they touch/overlap. Same goes for 3D. There needs to be overlap on all axis for a collision. \$\endgroup\$ – MichaelHouse Apr 18 at 14:57
-2
\$\begingroup\$

You could also consider doing the checks as a method in one of the objects. It might end up making your program less complex. For example instead of

public static boolean checkCollision(ObjectBox box1, ObjectBox box2){
    return //TRUE IF COLLIDING - FALSE IF NOT COLLIDING
}

You would have something like

public boolean checkCollision(ObjectBox other){
    return //TRUE IF COLLIDING - FALSE IF NOT COLLIDING
}

Which would be a public method inside of your ObjectBox class. Then you just have to do some basic Math inside of the box assuming that the boxes are 2D ( a little more Math if they are 3D) and invoke the new method as follows:

ObjectBox obj1 = new ObjectBox(/*Assume some parameters*/);
ObjectBox obj2 = new ObjectBox(/*Assume some parameters*/);
//Now we can compare them without needing the static method.
if(obj1.checkCollision(obj2))
    //Do amazing things.

Edit: Byte56 has you covered on the collision checking.

\$\endgroup\$
  • \$\begingroup\$ Welcome to GD.SE (if this comment actually gets posted)! This isn't actually an answer to the question, which was how to check for collisions with AABBs - please try to answer the question next time! \$\endgroup\$ – Polar Aug 9 '13 at 16:29
  • \$\begingroup\$ I still think using static methods for things like Physics and Drawing is better. Thanks for your reccommendation. \$\endgroup\$ – Sierox Aug 9 '13 at 16:51
  • \$\begingroup\$ Hi, I wouldn't have answered the question if I could have done it in a comment. Unfortunately a lack of reputation here stops me from doing that. \$\endgroup\$ – Yulfy Aug 9 '13 at 23:11
  • \$\begingroup\$ Answers aren't supposed to be used in place of comments. This is really just an extended comment, since it's just suggesting an alternative for using the function, and not suggesting an implementation for the function. \$\endgroup\$ – MichaelHouse Aug 10 '13 at 1:12

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.