Add new object to collision check at runtime?

So I have set up collision checks just like in the hello collision tutorial. Using: [java] CollisionShape terrainShape =

CollisionShapeFactory.createMeshShape((Node) terrain);

RigidBodyControl landscape = new RigidBodyControl(terrainShape, 0);




At runtime I raytest where you click on the landscape (its a terrainquad) and place a box there. What I am trying to do now is add that box to the collision check, so that you can’t walk through it. Since this is done at runtime while the other one is set up when the app starts I’m thinking I have to figure out some other way to set it up. So do you guys know how?

i believe if you add a BoxCollisionShape() to your box (or just a vertically oriented PlaneCollisionShape() to simulate 1 wall), you could add a RigidBodyControl with 0.0f mass to make sure it is static, and then once you add it to your physicsSpace, you can setPhysicsLocation() to be where you raytested. you might want to tweak the exact location you set its physics location to just because the raytest results may put the center of the collisionshape partially under the collision surface of your terrain mesh which might be a bad thing, even with static physics controls. i have not tested this, but i know it is bad to set the location of dynamic controls to be inside another collision shape, like a CharacterControl set below the floor’s RigidBodyControl.

If you don’t move the box you can create it by just adding a RigidBodyControl with mass = 0 and add it to the physics space. The collision shape will be created automatically. If you want to programmatically move it and have it still collide with other objects, give it a mass and set it to kinematic mode.

1 Like

I have mark as a global variable and I run this class on startup, [java]protected void initMark() {

Box sphere = new Box(1,1,1); //yes a box named sphere :stuck_out_tongue:

mark = new Geometry("BOOM!", sphere);

Material mark_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");

mark_mat.setColor("Color", ColorRGBA.Red);



And to add the cube I tell mark the position and attach it to userNode (which is a child of the rootnode).

It does not move, so to add it to the collision check I should do this right?

[java]CollisionShape userShape =


RigidBodyControl userControl = new RigidBodyControl(userShape, 0);



How would I get bulletAppState to add it or update it? Because right now it crashes on the last line and I think thats what im doing wrong.

:? please read the manual, you have to attach the RigidBodyControl to some model (I guess userNode in your case)… If theres no mesh theres no collision shape. It doesn’t just “crash”, it gives you an exception too, read it.

What you would do is:

[java]RigidBodyControl userControl = new RigidBodyControl(0);




[java]RigidBodyControl userControl = new RigidBodyControl(10);




1 Like

What do you mean by read the manual?

And I somewhat got it working now. At the moment I have it set when it places a cube it names it with a different number on each cube (so i can create multiple). The collision detection works on the first cube I place but not any after that. Heres the current code.

[java] userNode.attachChild(cube); //cube is created above and attached to userNode

RigidBodyControl userControl = new RigidBodyControl(0);



And I get a null pointer exception when using .add instead of .addAll (plus addAll should work even better for my purpose).

So what I am thinking is since it is adding everything from userNode to the physicsSpace it should be working. Does each cube need its own unique RidgidBodyControl though? Or would it go to the entire group?

Also thanks for sticking through my noobish questions.

@maxyme said:
What do you mean by read the manual?

I mean press F1 in the SDK or click the "Documentation" link on the top of this site.