Debian Codegraph @

examples/physics/physics_persistent_forces_components/code/explosionbehavior.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 { TExplosionBehavior }
17 unit ExplosionBehavior;
18
19 interface
20
21 uses
22 Classes, SysUtils, CastleTransform, CastleBehaviors, CastleVectors,
23 CastleComponentSerialize, CastleClassUtils, AbstractIterateRigidBodyBehavior;
24
25 type
26 { Add this behavior to CastleTransform }
27 TExplosionBehavior = 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 function PropertySections(const PropertyName: String): TPropertySections; override;
38
39 published
40 property Value: Single read FValue write FValue;
41 end;
42
43
44
45 implementation
46
47 { TExplosionBehavior --------------------------------------------------------- }
48
49 procedure TExplosionBehavior.UpdateRigidBody(const RigidBody: TCastleRigidBody;
50 const SecondsPassed: Single; var RemoveMe: TRemoveType);
51 var
52 Direction: TVector3;
53 Transform: TCastleTransform;
54 begin
55 Transform := RigidBody.Parent;
56 { TODO: Parent.LocalToWorld(Parent.Translation) is wrong -
57 Parent.Translation is in Parent.Parent coord system. }
58 Direction := Transform.LocalToWorld(Transform.Translation) - Parent.LocalToWorld(Parent.Translation);
59 Direction := Direction.Normalize;
60 RigidBody.AddForceAtPosition(Direction * Value, Parent.LocalToWorld(Parent.Translation));
61 RigidBody.WakeUp;
62 end;
63
64 function TExplosionBehavior.PropertySections(const PropertyName: String
65 ): TPropertySections;
66 begin
67 if PropertyName = 'Value' then
68 Result := [psBasic]
69 else
70 Result := inherited PropertySections(PropertyName);
71 end;
72
73 initialization
74 RegisterSerializableComponent(TExplosionBehavior, 'Explosion Behavior');
75
76
77 end.
78