You can turn any TCastleTransform instance into a rigid body, which means that it will be affected by gravity and collisions with other rigid bodies. TCastleScene, which you use to render 3D objects, is a descendant of TCastleTransform, so this implies that
TCastleScene can be a rigid body too. Or you can use TCastleTransform as a container for other transformations and scenes inside, and thus a group of objects can be treated as a single rigid body.
To make a rigid body you need to:
Create and configure an instance of TRigidBody.
Create and configure an instance of some TCollider descendant, like TPlaneCollider, TBoxCollider, TSphereCollider, TMeshCollider. It will be connected to the parent rigid body at construction (the TRigidBody.Collider property will be automatically set when creating the collider).
Link to your rigid body from the TCastleTransform.RigidBody property.
This is a sample code:
var Scene: TCastleScene; RigidBody: TRigidBody; Collider: TBoxCollider; begin // Create TCastleScene Scene := TCastleScene.Create(Application); Scene.Load(URL); Scene.Spatial := [ssRendering, ssDynamicCollisions]; // Scene.Translation := Vector3(1, 2, 3); // set initial position // Create TRigidBody RigidBody := TRigidBody.Create(Application); // RigidBody.Dynamic := ?; // boolean, default true // RigidBody.Gravity := ?; // boolean, default true // RigidBody.InitialLinearVelocity := Vector3(10, 0, 0); // Create TCollider (a TBoxCollider, to be more precise, in this case). // Note that TBoxCollider assumes that box is centered around (0,0,0) for now Collider := TBoxCollider.Create(RigidBody); Collider.Size := Scene.BoundingBox.Size; // Collider.Restitution := 0.3; // Collider.Density := 100.0; // Connect rigid body Scene.RigidBody := RigidBody; // Add Scene to the Viewport, to make it visible Viewport.Items.Add(Scene); end;
Right now the instruction to assign TCastleTransform.RigidBody should be at the very end, when you configured all the rigid body and collider parameters. Right now, this is when the rigid body is actually created on the physics engine side. Right now changing the properties of rigid body or collider later has no effect (if you need it, the temporary workaround is to set
nil and then again to your
The collider shape is not synchronized with the scene shape in any way. This also applies to the TMeshCollider that has a
Scene property: the mesh is created once from the scene geometry, it is not synchronized with scene changes later. (If you need it, the workaround is the same as above: set
nil and then again to your
TRigidBody instance. But creating a mesh collider is a costly operation, so think twice before doing this during the game!)
Although internally Castle Game Engine and Kraft work in 3D, the physics can work just fine for 2D games too.
Current physics engine integration is just a start. Michalis wants to dedicate a whole Castle Game Engine release in the future toward extending the physics. The plans are:
A shape within the TCastleScene should be able to act like a rigid body, independent of the rest of the scene. Ideally, this should be configurable in Blender, and exported nicely to X3D. The X3D specification has a rigid-body component to describe such things.
Currently we also have an older, simpler, internal physics/collision engine in CGE, that takes care of some tasks: the collisions of player and creatures (from CastleCreatures), a simple gravity for them, and custom collision methods for you (like RayCollision, SphereCollision etc. in CastleTransform unit). The new physics engine can probably replace them all, and there should be a flag to make it possible, and eventually it should even become the default, and the old collision implementation should be simply removed.
The current implementation doesn't expose any API for joints. Again, they could be designed in Blender and exported to X3D.
In the past, I planned integration with other physics engines, through a layer providing a common API. However, right now, I'm extremely satisfied with Kraft. As far as rigid body simulation goes, I think that Kraft may be the physics engine for us, and we may not need anything else... But just in case, here are other options I considered:
Bullet. Very full-featured, e.g. there's soft body, not only rigid body.
Full integration with Bullet will require proper translation of Bullet API to C and then to Pascal (as Bullet is in C++, it's not readily usable from anything other than C++). There is a C header for Bullet, see this old Google Code issue and this GitHub issue, but it's rather minimalistic (only rigid body), although it may be a good start.
Copyright Michalis Kamburelis and other Castle Game Engine developers.
Thank you to Paweł Wojciechowicz from Cat-astrophe Games for various graphics.
This documentation is also open-source and you can even redistribute it on open-source terms.