[ANSWERED] Controlling multiple Geometries with 1 RigidBodyControl

Is it possible to add the same RigidBodyControl to multiple Geometries?

In this code, methods buildTop, buildBottom, buildLeft, and builtRight are creating four walls to a room. All of them are using phys as the control. I am adding all the Geometries to rootNode, and I’m adding phys to the same physicsSpace as my player. I can currently run through walls.

buildFloor builds the floor for my room. It uses floorPhys as the control. Everything is being attached to rootNode correctly, and floorPhys is being added to the same physicsSpace as the walls. It is working just fine!

Edit: I’m certain it’s because RigidBodyControl doesn’t like controlling multiple things, because the floor used to be controlled by phys and it didn’t work then.

private RigidBodyControl phys = new RigidBodyControl(0);
private RigidBodyControl floorPhys = new RigidBodyControl(0);
...
...
protected void buildRoom() {
	float wallLength = (size-doorWidth)*0.5f;
	Material wallMesh = new Material(manager, "Common/MatDefs/Misc/Unshaded.j3md");
	wallMesh.setTexture("ColorMap", manager.loadTexture("Textures/metal01.jpg"));
	Box topsShape = new Box(wallLength, height, thickness);
	Box sideShape = new Box(thickness, height, wallLength);
	
	buildTop(wallMesh, topsShape, wallLength);
	buildBottom(wallMesh, topsShape, wallLength);
	buildLeft(wallMesh, sideShape, wallLength);
	buildRight(wallMesh, sideShape, wallLength);
	buildFloor();
		
	room.setLocalTranslation(pos);
}
...
private void buildRight(Material mesh, Box shape, float length) {
	// typical of all the other wall-building methods
	Geometry rightTop = new Geometry("right-top wall", shape);
	rightTop.setMaterial(mesh);
	rightTop.setLocalTranslation(size, height*0.5f, -size+length);
	rightTop.addControl(phys);
	room.attachChild(rightTop);
	
	Geometry rightBottom = new Geometry("right-bottom wall", shape);
	rightBottom.setMaterial(mesh);
	rightBottom.setLocalTranslation(size, height*0.5f, size-length);
	rightBottom.addControl(phys);
	room.attachChild(rightBottom);
}
private void buildFloor() {
	Box b = new Box(size, 0.5f, size);
	Geometry g = new Geometry("floor", b);
	g.setLocalTranslation(pos.x, -height*0.5f, pos.z);
	Material mat = new Material(manager, "Common/MatDefs/Misc/Unshaded.j3md");
	mat.setTexture("ColorMap", manager.loadTexture("Textures/light005.jpg"));
	g.setMaterial(mat);
	g.addControl(floorPhys);
	room.attachChild(g);
}
1 Like

No. 1:1 relationship. A control can only be attached to one spatial at a time.

It will be attached to whatever spatial you added it to last.

Edit: note that this is not specific to RigidBodyControl… it’s the way all controls work. They are meant to extend the functionality of a single spatial.

1 Like

That’s kinda disappointing…
I guess I’ll have to have 9 RigidBodyControls! :rofl:

Or one node and one control.

I don’t even want to imagine what an ugly pig of code Control would be if it somehow had to keep track of multiple spatials, etc… ugh. Not worth it. The controls only job is to implement parts of a Spatial. Composition-over-inheritance style.

@codex

you can always use CompoundCollisionShape on Geoms Node (just add one node for them all)

This way you only need add CompoundCollisionShape into physics space.

Thats how i do with my Voxel buildings. Chunks(Geoms) are put into CompoundCollisionShape so i only add this one into physics space / control.

CompoundCollisionShape collisionShape = new CompoundCollisionShape();
Vector3f chunkNodeOffset = getChunksNode().getLocalTranslation();
for (VoxelChunk chunk : chunks) {
    if (chunk != null) {
        CollisionShape chunkCollisionShape = chunk.getPhysicsShape();
        if (chunkCollisionShape != null) {
            collisionShape.addChildShape(chunkCollisionShape, new Vector3f(chunkNodeOffset.x, chunkNodeOffset.y, chunkNodeOffset.z));
        }
    }
}
2 Likes

For static (non-moving) rigid bodies (mass=0) I don’t see any need for physics controls.

If this were my project, I would create a single static PhysicsRigidBody for all the non-moving parts of the world, including the floor and all the walls.

1 Like