How does one update a RigidBodyControl?

Hey everyone,



So I’m working on a Minecraft-genre game, and have run into a problem. This is the code I’m using

[java]CollisionShape worldShape = CollisionShapeFactory.createMeshShape(world);

landscape = new RigidBodyControl(worldShape);

world.addControl(landscape);

rootNode.attachChild(world);

bulletAppState.getPhysicsSpace().add(landscape);[/java]



world is an instance of World, which extends Node. To it I attach Chunks, which extend Node. To Chunks I attach Blocks which extend Geometry. Okay, so the above code is how I apply physics to the Blocks in order that the player walk on them. However, using this code to remove a Block.



[java]CollisionResults results = new CollisionResults();

Ray ray = new Ray(cam.getLocation(), cam.getDirection());

world.collideWith(ray, results);

if (results.size() > 0) {

CollisionResult closest = results.getClosestCollision();

if (closest.getDistance() <= 7)

closest.getGeometry().removeFromParent();[/java]

However, it’s obvious that it only removes it from the world node. How on earth can I update the physical mesh? I’ve thought about removing the control from world, then remaking the landscape control, etc. However, it seems extremely inefficient to do that.



Thanks for any advice, I’m stumped,

mtheoryninja

You have to create a new collision shape and replace the current one. For a minecraft type game you might want to have some basic “building blocks” hand in the form of collision shapes. Collision shapes can be reused and used for multiple objects at the same time.

Okay, so I’m experimenting with what you’ve said. Since a CollisionShapeFactory.createBoxShape() takes a Spatial as a parameter, doesn’t that Spatial need to be created with a position? And since each Block will have a different position, doesn’t that mean each CollisionShape will be different since it has a different location?



Now, since you said I could do it above, I’m guessing I’m not understanding something. Could you clarify what you meant?



Thanks :slight_smile:

mtn

No, a collision shape moves to the location of a rigidbody. In a compound collision shape it has a location and rotation. So for a boxworld you’d maybe have 10 basic rigidbodys and move the collisionshapes in the compound shapes. Note however that no collisions should be happening with the world mesh while you move it.

I feel dense, but what do you mean I’d have 10 basic rigidbodies (Is that the same as a RigidBodyControl?) and move the collisionshapes in the compound shapes?



Sorry if this is a simple issue, but I’m not understanding what you’re saying. Thanks for helping me though.

A RigidBody is the basic bullet physics object. A RigidBodyControl extends that object but can be attached to a Spatial to move it along with the physics object. A RigidBody has a CollisionShape that is attached to it and defines its shape. You can see that visually in some of the physics tests, the shapes are displayed in blue. Now theres a special type of CollisionShape that is called CompoundCollisionShape and it can contain multiple other CollisionShapes, each with location and rotation. So you have one RigidBody with mass=0 (means its static) and it has a CompoundCollisionShape that you add and remove other CollisionShapes to. Don’t use the CollisionShapeFactory to create these but use the basic CollisionShape objects like BoxCollisionShape etc. and create them directly. The CollisionShapeFactory is there to create CollisionShapes based on Geometry, you don’t need it when you know about your box locations. Be aware that the optimization will be tedious and you cant expect a CompoundCollisionShape with 10.000 BoxCollisionShapes to perform well. Create reusable shapes and move them around in a smart way around the user to create the illusion of a physical world. Now if you understood this don’t forget to consider the other things I have said before. If you didn’t you should probably do the tutorials again and first engage in smaller projects than completely modifiable worlds that professionals fail to implement sometimes.

Cheers,

Normen

2 Likes

Okay, that cleared a lot of things up. Thanks for writing that!



Now that I know a bit more about how the whole physics thing works, I’ll give it a shot.



Thanks again, all the best,

mtheoryninja