Use these components (available in both editor and code) to control the physics.
In the editor, adding any collider automatically also adds the TCastleRigidBody
, so you only need to do one step: add a collider. The usual way to do this is by right-clicking on a TCastleTransform
instance in your hierarchy, and choosing from context menu "Add Behavior → Physics → Collider → …".
Then run "Physics → Play Simulation" (use the menu item or button on the header) and observe that body is affected by
There are lots of options to tweak how things behave. See API docs for documentation.
You can visualize physics colliders (esp. useful if you disable the TCastleCollider.AutoSize
on them and want to see the effect) using the menu item "Physics → Show Colliders".
The colliders in general should not collide at start. You should set up the colliders to avoid it. To make the collider smaller than automatically calculated you can:
Note
|
A good test to enjoy the physics is to make a "bullet" that will hit some rigid body with significant speed, so that you can see how it behaves on collision. To make a bullet, just
|
3.1. Adding physics components using code
As with all CGE components, while you can add them by clicking in CGE editor (at design-time), you can also add them using Pascal code at run-time. Like this:
var
NewBody: TCastleRigidBody;
NewCollider: TCastleSphereCollider;
begin;
NewBody := TCastleRigidBody.Create(FreeAtStop);
MyTransform.AddBehavior(NewBody);
NewCollider := TCastleSphereCollider.Create(FreeAtStop);
NewCollider.Restitution := 0.6;
NewCollider.Mass := 1;
MyTransform.AddBehavior(NewCollider);
end;