| 1 |
{ |
| 2 |
Copyright 2022-2022 Michalis Kamburelis, Andrzej Kilijański. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ TWindForceBehavior } |
| 17 |
unit WindForceBehavior; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses |
| 22 |
Classes, SysUtils, CastleTransform, CastleBehaviors, CastleVectors, |
| 23 |
CastleComponentSerialize, CastleClassUtils, AbstractTimeDurationBehavior, |
| 24 |
AbstractIterateRigidBodyBehavior; |
| 25 |
|
| 26 |
type |
| 27 |
{ Add this behavior to CastleTransform } |
| 28 |
TWindForceBehavior = class(TAbstractIterateRigidBodyBehavior) |
| 29 |
private |
| 30 |
FValue: Single; |
| 31 |
|
| 32 |
protected |
| 33 |
{ Updates all found rigid bodies. } |
| 34 |
procedure UpdateRigidBody(const RigidBody: TCastleRigidBody; |
| 35 |
const SecondsPassed: Single; var RemoveMe: TRemoveType); override; |
| 36 |
public |
| 37 |
function PropertySections(const PropertyName: String): TPropertySections; override; |
| 38 |
published |
| 39 |
property Value: Single read FValue write FValue; |
| 40 |
end; |
| 41 |
|
| 42 |
implementation |
| 43 |
|
| 44 |
{ TWindForceBehavior --------------------------------------------------------- } |
| 45 |
|
| 46 |
procedure TWindForceBehavior.UpdateRigidBody(const RigidBody: TCastleRigidBody; |
| 47 |
const SecondsPassed: Single; var RemoveMe: TRemoveType); |
| 48 |
var |
| 49 |
Direction: TVector3; |
| 50 |
begin |
| 51 |
{ TODO: Parent.LocalToWorld(Parent.Translation) is wrong - |
| 52 |
Parent.Translation is in Parent.Parent coord system. } |
| 53 |
Direction := Vector3(0,0,0) - Parent.LocalToWorld(Parent.Translation); |
| 54 |
Direction := Direction.Normalize; |
| 55 |
RigidBody.AddForceAtPosition(Direction * Value, Parent.LocalToWorld(Parent.Translation)); |
| 56 |
RigidBody.WakeUp; |
| 57 |
end; |
| 58 |
|
| 59 |
function TWindForceBehavior.PropertySections(const PropertyName: String |
| 60 |
): TPropertySections; |
| 61 |
begin |
| 62 |
if (PropertyName = 'Value') then |
| 63 |
Result := [psBasic] |
| 64 |
else |
| 65 |
Result := inherited PropertySections(PropertyName); |
| 66 |
end; |
| 67 |
|
| 68 |
initialization |
| 69 |
RegisterSerializableComponent(TWindForceBehavior, 'Wind Force Behavior'); |
| 70 |
|
| 71 |
|
| 72 |
end. |
| 73 |
|