Collision between boxes in separated classes

Hi there guys

I’m creating a 3d topdown game (A RTS) and right now i’m still learning how to play with jmonkey.

I’ve created 2 classes, one named Minions and another named Obstacle, which represent minions and obstacles.

I also have a TerrainQuad with height that is my terrain.

I got the 2 Boxes deployed, I got one moving, but cant get collision working between em. Tried so many different ways but no one solved my problem:

Here’s my class Minion

    public Node MinionNode;
    private RigidBodyControl MinionObstacle;
    
    Vector3f startPos;
    Material material;
    Boolean active = false;
    Geometry geometria;
    TerrainQuad terrain;

        public Minion(AssetManager assetManager, TerrainQuad terrain) {
        Box box = new Box(10, 10, 10);
        geometria = new Geometry("Box", box);
        material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        material.setColor("Color", ColorRGBA.Blue);
        geometria.setMaterial(material);
        geometria.setName("Caixinha");
        this.terrain = terrain;
        startPos = new Vector3f(-50f, terrain.getHeight(new Vector2f(-50.0f, -50.0f)), -50.0f);
        MinionNode = new Node("MinionNode");
        MinionNode.attachChild(geometria);
        MinionNode.setLocalTranslation(startPos);

        BoxCollisionShape MinionShape = new BoxCollisionShape(new Vector3f(10,10,10));
        MinionObstacle = new RigidBodyControl(MinionShape, 0);
        MinionNode.addControl(MinionObstacle);
    }

My Obstacle class is exactly like this, I just change the names of variables.

On my main, I have:

 bulletAppState = new BulletAppState();
        bulletAppState.setThreadingType(BulletAppState.ThreadingType.PARALLEL);
        stateManager.attach(bulletAppState);
        bulletAppState.setDebugEnabled(true);

        
        // Boneco e Obstáculo

        boneco = new Minion(assetManager, terrain);
        rootNode.attachChild(boneco.MinionNode);
        bulletAppState.getPhysicsSpace().add(boneco.getMinionObstacle());

                
        obstaculo = new Obstacle(assetManager, terrain);
        rootNode.attachChild(obstaculo.ObstacleNode);
        bulletAppState.getPhysicsSpace().add(obstaculo.getMinionObstacle());

What I’m missing? Is there any simple way to fix this? Later one I would import some models and replace boxes with models.

Thanks a lot for help!

If i see correctly you set the mass to 0, wich means its a static collision object (and moving it is invalid)

I got it at 0, but I can object moving (not by gravity)

I’m moving objects by this fuction:

public void movimentarGeometry(float tpf) {
        interp += (mySpeed / distance * 40) * tpf;
        if (interp < 1.0) {
            MinionObstacle.setPhysicsLocation(new Vector3f().interpolate(startPos, endPos, interp));
            MinionObstacle.getPhysicsLocation().setY(terrain.getHeight(new Vector2f(geometria.getLocalTranslation().x, geometria.getLocalTranslation().z)));
        } else {
            this.setMoving(false);
            startPos = endPos;
        }
    }

What should I change? I’m a little bit lost.

Well if you set the position, you have no physic at all, cause you set it.#

If you only want detections, using ghostobjects might work fine enough.

If you want actual physical interactions (aka rolling barrels pushing stuff)
You need mass>0 or kinematic rigidbodies.

1 Like

Worked smoothly! Thanks a lot :wink: