Debian Codegraph @

examples/physics/physics_persistent_forces_components/code/gravityforcebehavior.pas

raw
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 { TGravityForceBehavior }
17 unit GravityForceBehavior;
18
19 interface
20
21 uses
22 Classes, SysUtils, CastleTransform, CastleBehaviors, CastleVectors,
23 CastleComponentSerialize, CastleClassUtils, AbstractIterateRigidBodyBehavior;
24
25 type
26 { Transform with this behavior attracts other rigid bodies in world. }
27 TGravityForceBehavior = class(TAbstractIterateRigidBodyBehavior)
28 private
29 FValue: Single;
30
31 protected
32 { Updates all found rigid bodies. }
33 procedure UpdateRigidBody(const RigidBody: TCastleRigidBody;
34 const SecondsPassed: Single; var RemoveMe: TRemoveType); override;
35
36 public
37 constructor Create(AOwner: TComponent); override;
38
39 function PropertySections(const PropertyName: String): TPropertySections; override;
40 published
41 property Value: Single read FValue write FValue;
42 end;
43
44 implementation
45
46 uses CastleLog;
47
48 { TGravityForceBehavior ------------------------------------------------------ }
49
50 procedure TGravityForceBehavior.UpdateRigidBody(
51 const RigidBody: TCastleRigidBody; const SecondsPassed: Single;
52 var RemoveMe: TRemoveType);
53 var
54 Direction: TVector3;
55 Transform: TCastleTransform;
56 begin
57 Transform := RigidBody.Parent;
58 Direction := Parent.LocalToWorld(Parent.Translation) - Transform.LocalToWorld(Transform.Translation);
59 Direction := Direction.Normalize;
60 RigidBody.AddForceAtPosition(Direction * Value, Parent.LocalToWorld(Parent.Translation));
61 RigidBody.WakeUp;
62 end;
63
64 constructor TGravityForceBehavior.Create(AOwner: TComponent);
65 begin
66 inherited Create(AOwner);
67 end;
68
69 function TGravityForceBehavior.PropertySections(const PropertyName: String
70 ): TPropertySections;
71 begin
72 if PropertyName = 'Value' then
73 Result := [psBasic]
74 else
75 Result := inherited PropertySections(PropertyName);
76 end;
77
78 initialization
79 RegisterSerializableComponent(TGravityForceBehavior, 'Gravity Force Behavior');
80
81 end.
82