Intersection of modle bounds

I’m trying to make a check if two boxes are intersecting using their bounding box. From the tutorial, one could reason that the correct way is to do it by calling method in a following way:
[java]
geometry1.collideWith(geometry2.getModelBound(), results);
[/java]

In my case when the geometries are far away from each other the “results” variable above (CollisionResults) size is 12 in case where geometry1 and 2 are simple cubes.

Here is the complete code:

[java]
/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.
    */
    package jme3test.helloworld;

import com.jme3.app.SimpleApplication;
import com.jme3.bounding.BoundingBox;
import com.jme3.collision.CollisionResults;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Sphere;
import com.jme3.util.TangentBinormalGenerator;

/**
*

  • @author lukasz
    */
    public class Collision extends SimpleApplication {

    public Geometry red;
    public Geometry blue;
    private boolean running = false;

    public static void main(String[] args) {
    Collision app = new Collision();
    app.start();

    }

    @Override
    public void simpleInitApp() {
    HudDebugger.create(guiNode, guiFont);
    HudDebugger.getInstance().display();

     setDisplayFps(false);
     setDisplayStatView(false);
    
     Box redBox = new Box(1,1,1);
     redBox.setBound(new BoundingBox());
     redBox.updateBound();
     red = new Geometry("red", redBox);
     red.setMaterial(assetManager.loadMaterial("Materials/red.j3m"));
     rootNode.attachChild(red);
    
     
     Box blueBox = new Box(1,1,1);
     blueBox.setBound(new BoundingBox());
     blueBox.updateBound();
     blue = new Geometry("blue", blueBox);
     
     blue.setLocalTranslation(10, 0, 0);
     blue.setMaterial(assetManager.loadMaterial("Materials/blue.j3m"));
     rootNode.attachChild(blue);
     
     running = true;
    

    }

    @Override
    public void simpleUpdate(float tpf) {
    blue.move(-0.8f * tpf, 0, 0);
    if (running) {
    CollisionResults results = new CollisionResults();
    red.collideWith(blue.getModelBound(), results);

// change it to System.out.print() or some for real info
HudDebugger.getInstance().debug("collision", results.size() + "");

    }
}

}
[/java]

you have a convenient BoundingVolume.intersects(BoundingVolume bv) method

so basically you’d have to do
[java]
if(geometry1.getWorldBounds().intersects(geometry2.getWorldBounds){
// do your stuffs
}
[/java]

1 Like

Thank You. This is what I needed.