What is the best way to do 3D Mesh Collision with imported .obj files?


I’m asking this question because I don’t know exactly where to begin. This kind of collision is new to me, and I’m trying to create a system of collision detection for imported .obj PShapes.

In short, what is the best way to detect when two imported .obj files collide with each other?

Here is a picture of what the imported .obj files look like surrounded by their meshes (The player is in the center and the setting is the hallway with chairs):

I’ve seen a few posts relating to 3D collision, but none with colliding .obj pShapes. If anyone could point me in the right direction, I would greatly appreciate it. Thanks!

1 Like

I guess it kinda depends what result you are looking for. trying to achieve collisions between two meshes is very daunting and there are many complex checks that need to be performed (Which I’m afraid I do not know how to do). An easier route would be to use Axis Aligned Bounding Boxes (or just AABB) and spheres to define complex object files. this example is written in another programming language but pretty clearly describes what needs to be done. This is all assuming that you know the dimensions and positions of your .obj shapes. Here’s code for a simple AABB test where object1 is movable and object2 is immovable.

void update(){
    float left = object1.position.x - object1.dimensions.x/2;
    float right = object1.position.x + object1.dimensions.x/2;
    float top = object1.position.y - object1.dimensions.y/2;
    float bottom = object1.position.y + object1.dimensions.y/2;
    float front = object1.position.z - object1.dimensions.z/2;
    float back = object1.position.z + object1.dimensions.z/2;
    float boxLeft = object2.position.x - object2.dimensions.x/2;
    float boxRight = object2.position.x + object2.dimensions.x/2;
    float boxTop = object2.position.y - object2.dimensions.y/2;
    float boxBottom = object2.position.y + object2.dimensions.y/2;
    float boxFront = object2.position.z - object2.dimensions.z/2;
    float boxBack = object2.position.z + object2.dimensions.z/2;
    float boxLeftOverlap = right - boxLeft;
    float boxRightOverlap = boxRight - left;
    float boxTopOverlap = bottom - boxTop;
    float boxBottomOverlap = boxBottom - top;
    float boxFrontOverlap = back - boxFront;
    float boxBackOverlap = boxBack - front;
    if (((left > boxLeft && left < boxRight || (right > boxLeft && right < boxRight)) && ((top > boxTop &&top < boxBottom) || (bottom > boxTop && bottom < boxBottom)) && ((front > boxFront && front < boxBack) || (back > boxFront && back < boxBack)))){
//this is where collision = true
      float xOverlap = max(min(boxLeftOverlap, boxRightOverlap), 0);
      float yOverlap = max(min(boxTopOverlap, boxBottomOverlap), 0);
      float zOverlap = max(min(boxFrontOverlap, boxBackOverlap), 0);
      if (xOverlap < yOverlap && xOverlap < zOverlap){
        if (boxLeftOverlap < boxRightOverlap){
          object1.position.x = boxLeft - object1.dimensions.x/2;
        } else {
          object1.position.x = boxRight + object1.dimensions.x/2;
      else if (yOverlap < xOverlap && yOverlap < zOverlap){
        if (boxTopOverlap < boxBottomOverlap){
          object1.position.y = boxTop - object1.dimensions.y/2;
        } else {
          object1.position.y = boxBottom + object1.dimensions.y/2;
      else if (zOverlap < xOverlap && zOverlap < yOverlap){
        if (boxFrontOverlap < boxBackOverlap){
          object1.position.z = boxFront - object1.dimensions.x/2;
        } else {
          object1.position.z = boxBack + object1.dimensions.x/2;

I hope at least some of this helps and points you in the right direction! :smiley:

1 Like

Yes, this definitely points me in the right direction! :slight_smile: Thank you for your help!

1 Like