Multiple RigidBodyControls or CollisionShapes? CompoundCollisionShape? How to?

Hi all,

I’m struggling with making a staircase collidable. What I’m currently trying to accomplish is a CompoundCollisionShape made of 4 small BoxCollisionShape but I can’t for the life of me figure out why it simply doesn’t do anything on the scene. Here’s what I tried so far:

TRY #1 – 4 controls ARE created but on the scene I only see 1 or they’re all perfectly aligned so that I only see one, not sure but by going like this.node.getControl(0…3) != null I assume they’re all set because 4 returns null and 0…3 returns a control…
[java]
BoxCollisionShape cs1 = new BoxCollisionShape(new Vector3f(0.3f, 0.2f, 1.2f));
RigidBodyControl rbc1 = new RigidBodyControl(cs1, 0);
rbc1.setPhysicsLocation(location.subtract(0, 0.3f, 0.3f));
rbc1.setPhysicsRotation(rotation);
this.node.addControl(rbc1);

        BoxCollisionShape cs2 = new BoxCollisionShape(new Vector3f(0.3f, 0.2f, 1.2f));
        RigidBodyControl rbc2 = new RigidBodyControl(cs2, 0);
        rbc2.setPhysicsLocation(location);
        rbc2.setPhysicsRotation(rotation);
        this.node.addControl(rbc2);
        
        BoxCollisionShape cs3 = new BoxCollisionShape(new Vector3f(0.3f, 0.2f, 1.2f));
        RigidBodyControl rbc3 = new RigidBodyControl(cs3, 0);
        rbc3.setPhysicsLocation(location.add(0, 0.3f, 0.3f));
        rbc3.setPhysicsRotation(rotation);
        this.node.addControl(rbc3);
        
        BoxCollisionShape cs4 = new BoxCollisionShape(new Vector3f(0.3f, 0.2f, 1.2f));
        RigidBodyControl rbc4 = new RigidBodyControl(cs4, 0);
        rbc4.setPhysicsLocation(location.add(0, 0.6f, 0.6f));
        rbc4.setPhysicsRotation(rotation);
        this.node.addControl(rbc4);
        
        this.app.get_bullet_app_state().getPhysicsSpace().add(this.node);

[/java]

TRY #2 – Then I thought of several odd things like maybe the BoxCollisionShape has to be slightly different for it to work (I had this issue with the bounding volume box in my app and I fixed it by adding 0.00001f to an axis so that it’s slightly different and then collisions would happen as expected) and also I thought that maybe I had to addControl() before setPhysicsLocation() but none of those 2 things made it work properly.
[java]
BoxCollisionShape cs1 = new BoxCollisionShape(new Vector3f(0.31f, 0.2f, 1.11f));
RigidBodyControl rbc1 = new RigidBodyControl(cs1, 0);
this.node.addControl(rbc1);
rbc1.setPhysicsLocation(location.subtract(0, 0.3f, 0.3f));
rbc1.setPhysicsRotation(rotation);

        BoxCollisionShape cs2 = new BoxCollisionShape(new Vector3f(0.32f, 0.2f, 1.11f));
        RigidBodyControl rbc2 = new RigidBodyControl(cs2, 0);
        this.node.addControl(rbc2);
        rbc2.setPhysicsLocation(location);
        rbc2.setPhysicsRotation(rotation);
        
        BoxCollisionShape cs3 = new BoxCollisionShape(new Vector3f(0.33f, 0.2f, 1.11f));
        RigidBodyControl rbc3 = new RigidBodyControl(cs3, 0);
        this.node.addControl(rbc3);
        rbc3.setPhysicsLocation(location.add(0, 0.3f, 0.3f));
        rbc3.setPhysicsRotation(rotation);
        
        BoxCollisionShape cs4 = new BoxCollisionShape(new Vector3f(0.33f, 0.2f, 1.11f));
        RigidBodyControl rbc4 = new RigidBodyControl(cs4, 0);
        this.node.addControl(rbc4);
        rbc4.setPhysicsLocation(location.add(0, 0.6f, 0.6f));
        rbc4.setPhysicsRotation(rotation);
        
        this.app.get_bullet_app_state().getPhysicsSpace().add(this.node);

[/java]

TRY #3 – That’s when I realized it should probably have tried to go the CompoundCollisionShape route. So that’s what I did and I tried to copy one of the rare posts on this forum that talks about CompoundCollisionShape, I tried like this with a GhostControl first:
[java]
CompoundCollisionShape compoundShape = new CompoundCollisionShape();

        BoxCollisionShape cs1 = new BoxCollisionShape(new Vector3f(0.3f, 0.2f, 1.11f));
        compoundShape.addChildShape(cs1, location.subtract(0, 0.3f, 0.3f));
        
        BoxCollisionShape cs2 = new BoxCollisionShape(new Vector3f(0.3f, 0.2f, 1.11f));
        compoundShape.addChildShape(cs2, location);
        
        BoxCollisionShape cs3 = new BoxCollisionShape(new Vector3f(0.3f, 0.2f, 1.11f));
        compoundShape.addChildShape(cs3, location.add(0, 0.3f, 0.3f));
        
        BoxCollisionShape cs4 = new BoxCollisionShape(new Vector3f(0.3f, 0.2f, 1.11f));
        compoundShape.addChildShape(cs4, location.add(0, 0.6f, 0.6f));
        
        GhostControl gc = new GhostControl(compoundShape);
        this.node.addControl(gc);
        
        this.app.get_bullet_app_state().getPhysicsSpace().add(this.node);

[/java]

TRY #4 – Then I tried with a RigidBodyControl (exact same code as above but instead of GhostControl it’s RigidBodyControl) instead. Didn’t work, In both cases, absolutely no control is shown on the scene when I wireframe it. Then I thought maybe instead of adding the NODE to the PhysicsSpace, maybe I should add the actual control directly like this:
[java]
CompoundCollisionShape compoundShape = new CompoundCollisionShape();

        BoxCollisionShape cs1 = new BoxCollisionShape(new Vector3f(0.3f, 0.2f, 1.11f));
        compoundShape.addChildShape(cs1, location.subtract(0, 0.3f, 0.3f));
        
        BoxCollisionShape cs2 = new BoxCollisionShape(new Vector3f(0.3f, 0.2f, 1.11f));
        compoundShape.addChildShape(cs2, location);
        
        BoxCollisionShape cs3 = new BoxCollisionShape(new Vector3f(0.3f, 0.2f, 1.11f));
        compoundShape.addChildShape(cs3, location.add(0, 0.3f, 0.3f));
        
        BoxCollisionShape cs4 = new BoxCollisionShape(new Vector3f(0.3f, 0.2f, 1.11f));
        compoundShape.addChildShape(cs4, location.add(0, 0.3f, 0.3f));
        
        RigidBodyControl rbc = new RigidBodyControl(compoundShape, 0);
        this.node.addControl(rbc);
        this.app.get_bullet_app_state().getPhysicsSpace().add(rbc);

[/java]

…didn’t work. No control shows up on the scene :frowning:

I’m hoping somebody can enlighten me on the matter :stuck_out_tongue: Thx for reading through! :smiley:

Actually, it just occurs to me that I could also only use 1 BoxCollisionShape like diagonally rotated so that it very grossly fits the staircase and it would work quite well, but I’m still interested in knowing how to actually make the CompoundCollisionShape work.

EDIT: Actually, I’m stuck also using this approach because when I rotate the RigidBodyControl, the model rotates… SIGH :frowning:

EDIT #2: Finally got it working with this single diagonally rotated BoxCollisionShape. For anybody who might find this piece of information useful, I had to rotate my model in the opposite direction of that diagonal angle I was looking for and then once the model is attached to my node and that node is added to the PhysicsSpace, I setPhysicsRotation() in the diagonal angle. That shifts the model back to its original rotation but with the BoxCollisionShape in a desired diagonal angle.

Why do you need the compound shape at all?

For static geometry the meshollision shape is usually faster than compounds.

ORLY? You mean like a complex shape that perfectly mimics the mesh? but it’s like 37000 triangles tough.

Simply like this?
[java]
RigidBodyControl rbc = new RigidBodyControl(0);
this.node.addControl(rbc);
[/java]

EDIT: If that’s what you meant, I don’t think it will work lol… I just tried and it goes down from 300fps to 4fps when I touch the staircase hahahah… so… I think we can safely assume it’s better if it’s a less complex collision shape now can’t we? :stuck_out_tongue:

Are you on android or something? Whats the shape that collides? What you say sounds unlikely.

Wait, why does your stairs have several thausand polygons?

Anyway, a common approach is to make a very simplified model for this (aka collisionmodel).
The green model in this example

1 Like
@Empire Phoenix said: Wait, why does your stairs have several thausand polygons?

Because it’s made of broken, unequal planks. I could most certainly try to merge some vertices, but i’m not a 3D artist nor a Blender expert, so having succeeded in actually make one already feels like an accomplishment to me hahaha! :stuck_out_tongue:

Yep OK I get you for the simplified model, that’s kind of what I did by luck, because it could be done with only 1 diagonal box. But your green collision shape is a compound one isn’t it? I see like 10 components on it? Could you be so kind to post the code that made this work? I can’t figure it out! It does work for 1 shape, but not for the compound or multiple collision shapes!.. :frowning:

I +1’ed your answer. It looks exactly as what I want to do.

@Normen I’m on desktop using a high end graphics card and a 4ghz 8-core CPU. It’s just obvious to me that the model is too complex for the collision detection but at the same time it doesn’t matter because if I would be able to make a compound shape I could do EXACTLY like @Empire Phoenix did (the simpler collision shape) and I’m sure it would be 100% optimal.

Thx

For the simplified collision shape just go into Blender or whatever modeller you use and use the decimate modifier or create a new mesh that just follows the form of the stairs mesh… really simple, you don’t even have to add materials or anything, 'cause then you go into jMonkey and create a new mesh collision shape out of the simplified mesh and ba-da-bing-ba-da-boom simplified collision shape of awesome. Super easy and nice; far easier than spending hours trying to create a compound collision shape :wink:

1 Like

There is no code, thats an exmaple image from the unreal editor tutorials.

WELL… I like this method of doing it! So thank you @nomnom I think that’s very easy and accessible since Blender is free to use (not EASY to use, but it’s free so anybody can use it). Thx all! :smiley: +1’ed you @nomnom :wink:

No problem! (Yeahhh, it’s got a bit of a learning curve… But once you learn the keybindings it’s awesome)
But it was what @EmpirePhoenix was talking about the whole time, so it was really his solution :wink: Good luck!

ORLY @Empire Phoenix ?