Debian Codegraph @

src/transform/castlebehaviors_sticktosurface.inc

raw
1 {%MainUnit castlebehaviors.pas}
2 {
3 Copyright 2022-2022 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 {$ifdef read_interface}
18
19 type
20 { Behavior to make parent TCastleTransform stick to a surface of another
21 TCastleTransform @link(Target).
22 The example use-case is to stick things to the surface of the terrain underneath. }
23 TCastleStickToSurface = class(TCastleBehavior)
24 strict private
25 FTarget: TCastleTransform;
26 FTargetObserver: TFreeNotificationObserver;
27 FDirection: TVector3;
28 FOnlyAtDesign: Boolean;
29 procedure SetTarget(const Value: TCastleTransform);
30 procedure TargetFreeNotification(const Sender: TFreeNotificationObserver);
31 protected
32 function CanAttachToParent(const NewParent: TCastleTransform;
33 out ReasonWhyCannot: String): Boolean; override;
34 public
35 constructor Create(AOwner: TComponent); override;
36 destructor Destroy; override;
37 procedure Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); override;
38 function PropertySections(const PropertyName: String): TPropertySections; override;
39
40 { Direction in which we check for collisions to stick the parent to @link(Target).
41 The default direction is -Y (that is, (0, 1, 0)) which makes the parent
42 stick to the object under it, as if the gravity was forcing it down immediately. }
43 property Direction: TVector3 read FDirection write FDirection;
44 published
45 { Target to which Parent of this behavior sticks.
46
47 TODO: Rotating the target right now doesn't make proper behavior,
48 test by rotating terrain in examples/terrain/. }
49 property Target: TCastleTransform read FTarget write SetTarget;
50
51 { Perform stickiness only at design-time.
52 This is @true by default and makes this behavior trivially optimized
53 at runtime. }
54 property OnlyAtDesign: Boolean read FOnlyAtDesign write FOnlyAtDesign default true;
55
56 {$define read_interface_class}
57 {$I auto_generated_persistent_vectors/tcastlesticktosurface_persistent_vectors.inc}
58 {$undef read_interface_class}
59 end;
60
61 {$endif read_interface}
62
63 {$ifdef read_implementation}
64
65 {$define read_implementation_methods}
66 {$I auto_generated_persistent_vectors/tcastlesticktosurface_persistent_vectors.inc}
67 {$undef read_implementation_methods}
68
69 constructor TCastleStickToSurface.Create(AOwner: TComponent);
70 begin
71 inherited;
72 FOnlyAtDesign := true;
73 FDirection := Vector3(0, -1, 0);
74
75 FTargetObserver := TFreeNotificationObserver.Create(Self);
76 FTargetObserver.OnFreeNotification := {$ifdef FPC}@{$endif} TargetFreeNotification;
77
78 {$define read_implementation_constructor}
79 {$I auto_generated_persistent_vectors/tcastlesticktosurface_persistent_vectors.inc}
80 {$undef read_implementation_constructor}
81 end;
82
83 destructor TCastleStickToSurface.Destroy;
84 begin
85 {$define read_implementation_destructor}
86 {$I auto_generated_persistent_vectors/tcastlesticktosurface_persistent_vectors.inc}
87 {$undef read_implementation_destructor}
88 inherited;
89 end;
90
91 function TCastleStickToSurface.PropertySections(const PropertyName: String): TPropertySections;
92 begin
93 if ArrayContainsString(PropertyName, [
94 'DirectionPersistent', 'Target', 'OnlyAtDesign'
95 ]) then
96 Result := [psBasic]
97 else
98 Result := inherited PropertySections(PropertyName);
99 end;
100
101 procedure TCastleStickToSurface.Update(const SecondsPassed: Single; var RemoveMe: TRemoveType);
102
103 function FindPositionInTarget(const RayCollision: TRayCollision): TVector3;
104 var
105 I: Integer;
106 begin
107 for I := 0 to RayCollision.Count - 1 do
108 if RayCollision[I].Item = Target then
109 Exit(RayCollision[I].Point);
110 raise EInternalError.Create('Collision with Target resolved, but Target not on colliders list');
111 end;
112
113 var
114 RayDirection, RayOrigin, Collision, TranslationToTarget: TVector3;
115 RayCollision: TRayCollision;
116 begin
117 inherited;
118
119 Assert(Parent <> nil);
120
121 { We update Parent in each @link(Update).
122 This is simplest: this way it works when anything changes (Parent,
123 Parent's Parent..., Target, Target's Parent...).
124 And it doesn't work when Parent does not exist,
125 i.e. we don't perform this merely when Direction/Target property changes.
126
127 Disadvantage:
128 - This is not efficient, we don't check whether anything changed,
129 we always calculate collision here, every frame.
130 - This may have a visible delay, user may see previous position
131 when the object was not stuck to Target.
132
133 These are both not a problem when using this only at design-time. }
134
135 if (CastleDesignMode or not OnlyAtDesign) and
136 (Target <> nil) and
137 Parent.HasWorldTransform and
138 (Parent.Parent <> nil) and
139 Target.HasWorldTransform then
140 begin
141 { Convert Direction to Target coordinate system }
142 RayDirection := Target.WorldToLocalDirection(
143 Parent.LocalToWorldDirection(Direction));
144
145 { Get suitable ray position, outside of Target bbox,
146 in Target coordinate system }
147 TranslationToTarget := Target.WorldToLocal(
148 Parent.LocalToWorld(TVector3.Zero));
149 RayOrigin := TranslationToTarget - RayDirection * Target.LocalBoundingBox.Diagonal;
150
151 RayCollision := Target.InternalRayCollision(RayOrigin, RayDirection);
152 if RayCollision <> nil then
153 try
154 Collision := FindPositionInTarget(RayCollision);
155 { Convert collision from Target coordinate system to Parent.Parent }
156 Collision := Parent.Parent.WorldToLocal(Target.LocalToWorld(Collision));
157 Parent.Translation := Collision;
158 finally FreeAndNil(RayCollision) end;
159 end;
160 end;
161
162 function TCastleStickToSurface.CanAttachToParent(const NewParent: TCastleTransform;
163 out ReasonWhyCannot: String): Boolean;
164 begin
165 Result := inherited;
166 if not Result then Exit;
167
168 if NewParent.FindBehavior(TCastleStickToSurface) <> nil then
169 begin
170 ReasonWhyCannot := 'Only one TCastleStickToSurface behavior can be added to a given TCastleTransform';
171 Result := false;
172 end;
173 end;
174
175 procedure TCastleStickToSurface.SetTarget(const Value: TCastleTransform);
176 begin
177 if FTarget <> Value then
178 begin
179 FTarget := Value;
180 FTargetObserver.Observed := Value;
181 end;
182 end;
183
184 procedure TCastleStickToSurface.TargetFreeNotification(const Sender: TFreeNotificationObserver);
185 begin
186 // set property to nil when the referenced component is freed
187 Target := nil;
188 end;
189
190 {$endif read_implementation}