Class TCastleRigidBody
Unit
Declaration
type TCastleRigidBody = class(TCastleBehavior)
Description
Use this behavior to be affected by physics collisions and forces. Add a TCastleRigidBody
and some collider (TCastleCollider descendant) as behaviors of TCastleTransform to make the given TCastleTransform be affected by physics and collide with other physics bodies.
Hierarchy
- TObject
- TPersistent
- TComponent
- TCastleComponent
- TCastleBehavior
- TCastleRigidBody
Overview
Methods
procedure WorldAfterAttach; override; |
|
procedure WorldBeforeDetach; override; |
|
procedure Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); override; |
|
function CanAttachToParent(const NewParent: TCastleTransform; out ReasonWhyCannot: String): Boolean; override; |
|
constructor Create(AOwner: TComponent); override; |
|
destructor Destroy; override; |
|
procedure Setup2D; |
|
function GetCollidingTransforms: TCastleTransformList; |
|
function PhysicsRayCast(const RayOrigin, RayDirection: TVector3; const MaxDistance: Single = MaxSingle): TPhysicsRayCastResult; |
|
function PropertySections(const PropertyName: String): TPropertySections; override; |
|
procedure AddForceAtPosition(const Force, Position: TVector3); |
|
procedure AddForce(const Force: TVector3; const ForceInLocalCoordinates: Boolean); |
|
procedure AddTorque(const Torque: TVector3); |
|
procedure ApplyImpulse(const Impulse, Point: TVector3); |
|
function ExistsInRoot: Boolean; |
|
procedure WakeUp; |
Properties
property AngularVelocity: TVector3 read FAngularVelocity write SetAngularVelocity; |
|
property LinearVelocity: TVector3 read FLinearVelocity write SetLinearVelocity; |
|
property InitialAngularVelocity: TVector3 read FAngularVelocity write SetAngularVelocity; deprecated 'use AngularVelocity'; |
|
property InitialLinearVelocity: TVector3 read FLinearVelocity write SetLinearVelocity; deprecated 'use LinearVelocity'; |
|
property OnCollisionEnter: TCollisionEvent read FOnCollisionEnter write SetOnCollisionEnter; |
|
property OnCollisionExit: TCollisionEvent read FOnCollisionExit write SetOnCollisionExit; |
|
property OnCollisionStay: TCollisionEvent read FOnCollisionStay write SetOnCollisionStay; |
|
property Dynamic: Boolean read FDynamic write SetDynamic default true; |
|
property Animated: Boolean read FAnimated write SetAnimated default false; |
|
property Trigger: Boolean read FTrigger write SetTrigger default false; |
|
property CollisionDetection: TCollisionDetection read FCollisionDetection
write SetCollisionDetection default cdDiscrete; |
|
property Layer: TPhysicsLayer read FLayer write SetLayer default 0; |
|
property Gravity: Boolean read FGravity write SetGravity default true; |
|
property LockTranslation: T3DCoords read FLockTranslation write SetLockTranslation default []; |
|
property LockRotation: T3DCoords read FLockRotation write SetLockRotation default []; |
|
property Exists: Boolean read FExists write SetExists default true; |
|
property AngularVelocityDamp: Single read FAngularVelocityDamp write SetAngularVelocityDamp default 0.1; |
|
property MaxAngularVelocity: Single read FMaxAngularVelocity write SetMaxAngularVelocity; |
|
property LinearVelocityDamp: Single read FLinearVelocityDamp write SetLinearVelocityDamp default 0.1; |
|
property MaxLinearVelocity: Single read FMaxLinearVelocity write SetMaxLinearVelocity; |
|
property AngularVelocityPersistent: TCastleVector3Persistent read FAngularVelocityPersistent ; |
|
property LinearVelocityPersistent: TCastleVector3Persistent read FLinearVelocityPersistent ; |
Description
Methods
procedure WorldAfterAttach; override; |
|
Initializes physics engine objects when TCastleRigidBody is attached to world. |
procedure WorldBeforeDetach; override; |
|
Deinitializes physics engine objects when TCastleRigidBody is attached to world. |
procedure Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); override; |
|
This item has no description. Showing description inherited from TCastleBehavior.Update. Continuously occuring event, for various tasks. |
function CanAttachToParent(const NewParent: TCastleTransform; out ReasonWhyCannot: String): Boolean; override; |
|
Only one TCastleRigidBody behavior can be added to a given TCastleTransform so we check that here. |
constructor Create(AOwner: TComponent); override; |
|
This item has no description. |
destructor Destroy; override; |
|
This item has no description. |
procedure Setup2D; |
|
Utility function to set common values for physics in 2D games. Locks moving along the Z axis, locks rotating along the X and Y axes. |
function GetCollidingTransforms: TCastleTransformList; |
|
Transformations that we collide with currently. |
function PhysicsRayCast(const RayOrigin, RayDirection: TVector3; const MaxDistance: Single = MaxSingle): TPhysicsRayCastResult; |
|
First object hit by the ray, calculated using the physics engine. Returns the TPhysicsRayCastResult record. Read TPhysicsRayCastResult.Hit (boolean), TPhysicsRayCastResult.Transform (guaranteed to be The given RayOrigin, RayDirection, MaxDistance are in the parent coordinate system of associated TCastleTransform (TCastleBehavior.Parent). So for example query like this works naturally:
RBody := MyTransform.FindBehavior(TCastleRigidBody) as TCastleRigidBody;
Hit := RBody.RayCast(MyTransform.Translation, MyTransform.Direction, MaxDistance);
It returns TPhysicsRayCastResult, with information there consistently in the same coordinate system as inputs. So TPhysicsRayCastResult.Distance, TPhysicsRayCastResult.Point, TPhysicsRayCastResult.Normal are in parent coordinate system of associated TCastleTransform (TCastleBehavior.Parent). This ignores the collider of this rigid body (to not accidentally collide with your own collider), and checks collisions with the rest of the world in given max distance. Only collisions with the physics colliders (TCastleCollider) are considered. Note: The TCastleTransform.Pickable property is ignored by this method, i.e. it considers all colliders regardless of their TCastleTransform.Pickable value. The TCastleTransform.Pickable is only taken into account by the routines of the old physics system (see https://castle-engine.io/physics#_old_system_for_collisions_and_gravity ). In the new system, a similar role will be filled by layers. |
function PropertySections(const PropertyName: String): TPropertySections; override; |
|
This item has no description. Showing description inherited from TCastleComponent.PropertySections. Section where to show property in the editor. |
procedure AddForceAtPosition(const Force, Position: TVector3); |
|
Push this rigid body. The body is pushed from a Position and along the direction of Force. Longer length of Force makes a stronger push. The forces affecting each body are reset every frame (only the body velocity is preserved across frames). If you want to keep applying this force (e.g. it is a wind that continues pushing in given direction) you should call this method every frame. It automatically accounts for the delta time (because the force will actually be applied later, with delta time, to velocity) so no need to multiply the arguments with SecondsPassed. That is, adding force means By changing Position from which you push, you can change the angular velocity, i.e. different Position makes the body spin in different ways. Note that Position too distant from the body center may result in unrealistically strong push. If Position is exactly at body center, then angular velocity will not be affected. Force and Position are in the world coordinate system. |
procedure AddForce(const Force: TVector3; const ForceInLocalCoordinates: Boolean); |
|
Push this rigid body. The body is pushed along the direction of Force. Longer length of Force makes a stronger push. This is similar to AddForceAtPosition, but the Force (direction) can be in local or global coordinate system (depending on ForceInLocalCoordinates). Moreover it doesn't take as parameter any Position: the force always acts from the origin of this body. Just like with AddForceAtPosition, the forces affecting each body are reset every frame (only the body velocity is preserved across frames). If you want to keep applying this force (e.g. it is a wind that continues pushing in given direction) you should call this method every frame. It automatically accounts for the delta time (because the force will actually be applied later, with delta time, to velocity) so no need to multiply the arguments with SecondsPassed. |
procedure AddTorque(const Torque: TVector3); |
|
Rotate this rigid body with physics. Longer length of Torque makes a stronger push. Torque is in world coordinates. Just like with AddForceAtPosition, the forces affecting each body are reset every frame (only the body velocity is preserved across frames). If you want to keep applying this force (e.g. it is a whirlwind that continues rotating) you should call this method every frame. It automatically accounts for the delta time (because the force will actually be applied later, with delta time, to velocity) so no need to multiply the arguments with SecondsPassed. |
procedure ApplyImpulse(const Impulse, Point: TVector3); |
|
Apply a one-time instant increase in speed, that pushes the body (increasing velocity). Impulse is a direction along which to push, Point is the position from which you push. Longer length of Impulse makes a stronger push. Impulse and Point are both in world coordinates. Adding impulse means Same notes as for Position of AddForceAtPosition: By changing Point from which you push, you can change the angular velocity, i.e. different Point makes the body spin in different ways. Note that Point too distant from the body center may result in unrealistically strong push. If Point is exactly at body center, then angular velocity will not be affected. |
function ExistsInRoot: Boolean; |
|
Returns true when rigid body behavior exists, and is added to Castle Transform, and this transform |
Properties
property AngularVelocity: TVector3 read FAngularVelocity write SetAngularVelocity; |
|
Current rotating velocity of this body. You can query it (as it changes, affected by forces) and set it at any point. In world coordinates. See https://en.wikipedia.org/wiki/Angular_velocity about the exact meaning of angular velocity. The vector direction (AngularVelocity.Normalize) determines the axis around which we rotate. The vector length (AngularVelocity.Length) determines the speed (in radians/second) with which we rotate. |
property LinearVelocity: TVector3 read FLinearVelocity write SetLinearVelocity; |
|
Current moving velocity of this body. You can query it (as it changes, affected by forces) and set it at any point. In world coordinates. |
property InitialAngularVelocity: TVector3 read FAngularVelocity write SetAngularVelocity; deprecated 'use AngularVelocity'; |
|
Warning: this symbol is deprecated: use AngularVelocity This item has no description. |
property InitialLinearVelocity: TVector3 read FLinearVelocity write SetLinearVelocity; deprecated 'use LinearVelocity'; |
|
Warning: this symbol is deprecated: use LinearVelocity This item has no description. |
property OnCollisionEnter: TCollisionEvent read FOnCollisionEnter write SetOnCollisionEnter; |
|
Occurs when TCastleRigidBody starts colliding with another TCastleRigidBody. It can occur repeatedly for the same body (in the same time instant) if the collision has many points of contact. Warning: Do not free the related TCastleTransform or TCastleRigidBody or TCastleCollider components upon receiving this event. Right now, it would free an underlying physics engine object, and this would crash the physics engine. Instead, you can:
|
property OnCollisionExit: TCollisionEvent read FOnCollisionExit write SetOnCollisionExit; |
|
Occurs when TCastleRigidBody stops colliding with another TCastleRigidBody. Just like at OnCollisionEnter: Do not free the related components upon receiving this event. See the OnCollisionEnter documentation for details of this limitation and solutions. |
property OnCollisionStay: TCollisionEvent read FOnCollisionStay write SetOnCollisionStay; |
|
Occurs when TCastleRigidBody still collides with another TCastleRigidBody. Just like at OnCollisionEnter: Do not free the related components upon receiving this event. See the OnCollisionEnter documentation for details of this limitation and solutions. |
property Animated: Boolean read FAnimated write SetAnimated default false; |
|
Is the transformation of this object updated often (relevant only when Dynamic = This property is taken into account only if Dynamic = This property is an optimization hint to the physics engine, it says how often you will change the transformation.
|
property Trigger: Boolean read FTrigger write SetTrigger default false; |
|
Triggers report when other object collides with them, but still allow the other object to pass through. In other words, colliding with a trigger will not cause the collider to stop or "bounce off" the trigger. They are useful as sensors. E.g. a trigger may be a coin (like in "Mario") that the player can "consume" by colliding with it. Triggers report collisions through the same events as other rigid bodies: (TCastleRigidBody.OnCollisionEnter, TCastleRigidBody.OnCollisionStay, TCastleRigidBody.OnCollisionExit). |
property CollisionDetection: TCollisionDetection read FCollisionDetection
write SetCollisionDetection default cdDiscrete; |
|
Sets CollisionDetectionMode in rigid body to Discrete or ContinuousSweep. |
property Layer: TPhysicsLayer read FLayer write SetLayer default 0; |
|
Physics layer of the rigid body determines with which other bodies it may collide. The way layers interact is configured in the MyViewport.Items.PhysicsProperties.LayerCollisions. Each layer can also have name and description. You can set them using the MyViewport.Items.PhysicsProperties.LayerNames property. |
property Gravity: Boolean read FGravity write SetGravity default true; |
|
Is this object affected by gravity. |
property LockTranslation: T3DCoords read FLockTranslation write SetLockTranslation default []; |
|
Disable motion (TCastleTransform.Translation change) along the particular (world) axis. For 2D games, you will usually want to disable motion along the Z axis. Instead of directly changing this property, you can achieve this by calling Setup2D. |
property LockRotation: T3DCoords read FLockRotation write SetLockRotation default []; |
|
Disable rotation (TCastleTransform.Rotation change) along the particular (world) axis. For 2D games, you will usually want to disable rotation along the X and Y axes. Instead of directly changing this property, you can achieve this by calling Setup2D. |
property Exists: Boolean read FExists write SetExists default true; |
|
Controls whether the rigid body is actually processed by the physics engine. When this is |
property AngularVelocityDamp: Single read FAngularVelocityDamp write SetAngularVelocityDamp default 0.1; |
|
Damping means that the velocity will decrease over time. |
property MaxAngularVelocity: Single read FMaxAngularVelocity write SetMaxAngularVelocity; |
|
This item has no description. |
property LinearVelocityDamp: Single read FLinearVelocityDamp write SetLinearVelocityDamp default 0.1; |
|
Damping means that the velocity will decrease over time. Values from 0.0 up to infinity make sense. Exact interpretation: The damping is applied every "physics frame" following this equation:
Where In effect, damping = 0 means that no damping occurs. Damping values > 0 means that LinearVelocity decreases with time, larger The meaning of damping = 1.0 depends on Viewport.Items.PhysicsProperties.Frequency:
|
property MaxLinearVelocity: Single read FMaxLinearVelocity write SetMaxLinearVelocity; |
|
This item has no description. |
property AngularVelocityPersistent: TCastleVector3Persistent read FAngularVelocityPersistent ; |
|
AngularVelocity that can be visually edited in Castle Game Engine Editor, Lazarus and Delphi. Normal user code does not need to deal with this, instead read or write AngularVelocity directly. See also
|
property LinearVelocityPersistent: TCastleVector3Persistent read FLinearVelocityPersistent ; |
|
LinearVelocity that can be visually edited in Castle Game Engine Editor, Lazarus and Delphi. Normal user code does not need to deal with this, instead read or write LinearVelocity directly. See also
|
Generated by PasDoc 0.16.0-snapshot.