Debian Codegraph @

src/transform/castletransform_joints.inc

raw
1 {%MainUnit castletransform.pas}
2 {
3 Copyright 2017-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 { Physics joints. }
18
19 {$ifdef read_interface}
20 { Base class for physics joints, that connects two physics bodies
21 and constraints their relative movement.
22
23 This class provides useful virtual methods to create joints
24 (observing the existence of rigid body and parent) and some common
25 properties to all joints (allowing all joints to be breakable). }
26 TCastleAbstractJoint = class(TCastleBehavior)
27 protected
28 { When joint is added to world we need to find parent rigid body and set
29 callbacks for its initialize/deinitialize events. If this pointer is not nil
30 that means that we've done that. }
31 FParentRigidBody: TCastleRigidBody;
32 FBreakThresholdForce: Single;
33 FBreakThresholdTorque: Single;
34 FBreakable: Boolean;
35
36 { Create physics engine (Kraft) joint and set its specialized properties. }
37 procedure CreateKraftJoint; virtual; abstract;
38
39 { Get physics engine (Kraft) joint instance. }
40 function GetKraftJoint: TKraftConstraintJoint; virtual; abstract;
41
42 procedure RecreateKraftJoint;
43 procedure InitializeKraftJoint; virtual;
44 procedure DeinitializeKraftJoint; virtual; abstract;
45
46 { Can we create joint object. }
47 function AllNeededKraftObjectsInitialized: Boolean; virtual;
48
49 { Event callback when TCastleRigidBody is added to the parent transform. }
50 procedure OnParentTransformAddBehavior(const Component: TComponent); virtual;
51
52 { Event callback when TCastleRigidBody is removed from the parent transform. }
53 procedure OnBeforeParentTransfromRemoveBehavior(const Component: TComponent); virtual;
54
55 { Event callback when TCastleRigidBody behavior is initialized. }
56 procedure OnParentRigidBodyInitialized(const Component: TComponent); virtual;
57
58 { Event callback when TCastleRigidBody behavior is deinitialized. }
59 procedure OnBeforeParentRigidBodyDeinitialized(const Component: TComponent); virtual;
60
61 { Sets TCastleRigidBody behavior initialize/deinitialize callbacks. }
62 procedure SetParentRigidBodyEvents;
63
64 { Removes TCastleRigidBody behavior initialize/deinitialize callbacks. }
65 procedure RemoveParentRigidBodyEvents;
66
67 procedure UpdateBreakThresholdForceInKraft;
68 procedure UpdateBreakThresholdTorqueInKraft;
69 procedure UpdateBreakableInKraft;
70
71 procedure SetBreakThresholdForce(const AValue: Single);
72 procedure SetBreakThresholdTorque(const AValue: Single);
73 procedure SetBreakable(const AValue: Boolean);
74 public
75 destructor Destroy; override;
76 procedure WorldAfterAttach; override;
77 procedure WorldBeforeDetach; override;
78 function PropertySections(const PropertyName: String): TPropertySections; override;
79
80 { Create design-time transformations that are displayed in the hierarchy
81 to visualize physics joints 3D points.
82 If you want to visualize the behavior using a visible TCastleTransform,
83 then create and add to Parent a descendant of TCastleJointTool,
84 and set InternalCastleDesignInvalidate to @true. }
85 procedure InternalCreateGizmos; virtual;
86
87 { Destroy design-time stuff.
88 You can destroy any component, just be sure
89 to set InternalCastleDesignInvalidate to @true. }
90 procedure InternalDestroyGizmos; virtual;
91 published
92 { Makes the joint breakable when force is greater than BreakThresholdForce
93 or torque is greater than BreakThresholdTorque. }
94 property Breakable: Boolean read FBreakable write SetBreakable default false;
95
96 { Force threshold to break joint. }
97 property BreakThresholdForce: Single read FBreakThresholdForce write SetBreakThresholdForce {$ifdef FPC}default 0.0{$endif};
98
99 { Torque threshold to break joint. }
100 property BreakThresholdTorque: Single read FBreakThresholdTorque write SetBreakThresholdTorque {$ifdef FPC}default 0.0{$endif};
101 end;
102
103 { Abstract class for joints that use only one TCastleTransform. }
104 TCastleAbstractOneBodyJoint = class(TCastleAbstractJoint)
105 end;
106
107 { Abstract class for joints that connect two TCastleTransform instances. }
108 TCastleAbstractTwoBodiesJoint = class(TCastleAbstractJoint)
109 private
110 { TCastleAbstractTwoBodiesJoint means that joint connects two transforms so this is
111 the second one transform pointer }
112 FConnected: TCastleTransform;
113 { When connected transform is assigned we need found second rigid body and set
114 callbacks for its initialize/deinitialize events. If this pointer is not nil
115 that means that we done that }
116 FConnectedRigidBody: TCastleRigidBody;
117 { Stores valu should connected rigid bodies collide or not }
118 FConnectedCollide: Boolean;
119 { We need observe second connected transform and remove it form
120 FConnected when second transform is freed }
121 FConnectedTransfromFreeObserver: TFreeNotificationObserver;
122 protected
123 procedure InitializeKraftJoint; override;
124 procedure SetConnected(const AConnected: TCastleTransform); virtual;
125 procedure SetConnectedCollide(const AValue: Boolean); virtual;
126
127 function AllNeededKraftObjectsInitialized: Boolean; override;
128
129 procedure OnConnectedFree(const Sender: TFreeNotificationObserver); virtual;
130
131 { Event callback when TCastleRigidBody is added to the connected transform. }
132 procedure OnConnectedAddBehavior(const Component: TComponent); virtual;
133
134 { Event callback when TCastleRigidBody is removed from the connected transform. }
135 procedure OnConnectedTransfromRemoveBehavior(const Component: TComponent); virtual;
136
137 { Event callback when connected transform TCastleRigidBody behavior is initialized. }
138 procedure OnConnectedRigidBodyInitialized(const Component: TComponent); virtual;
139
140 { Event callback when connected transform TCastleRigidBody behavior is deinitialized. }
141 procedure OnBeforeConnectedRigidBodyDeinitialized(const Component: TComponent); virtual;
142
143 { Sets connected transform add/remove behavior callbacks and free
144 notification observer. }
145 procedure SetConnectedEvents;
146
147 { Removes connected transform add/remmove behavior callbacks and free
148 notification observer. }
149 procedure RemoveConnectedEvents;
150
151 { Sets connected TCastleRigidBody behavior initialize/deinitialize callbacks. }
152 procedure SetConnectedRigidBodyEvents;
153
154 { Removes connected TCastleRigidBody behavior initialize/deinitialize
155 callbacks. }
156 procedure RemoveConnectedRigidBodyEvents;
157 public
158 function PropertySections(const PropertyName: String): TPropertySections; override;
159
160 { We need to check if the connected castle transform is not
161 the parent node and we need to change the connected node to nil then. }
162 procedure ParentAfterAttach; override;
163 published
164 { Second transform of the physics joint.
165 Note that it can't be the Parent of this joint. }
166 property Connected: TCastleTransform read FConnected
167 write SetConnected;
168
169 { Should connected transforms (Connected and Parent) collide. }
170 property ConnectedCollide: Boolean read FConnectedCollide
171 write SetConnectedCollide default false;
172 end;
173
174 EConnectedSameAsParent = class (Exception);
175
176 { Hinge joint allows to rotate the transformation around a given axis,
177 like a door attached using hinges to the frame.
178 @link(Anchor) determines the rotation point and @link(Axis) the axis direction. }
179 TCastleHingeJoint = class(TCastleAbstractTwoBodiesJoint)
180 private
181 FKraftJointHinge: TKraftConstraintJointHinge;
182 FAxis: TVector3;
183 FAnchor: TVector3;
184 FAnchorTransform: TCastleTransform;
185 FMinAngle: Single;
186 FMaxAngle: Single;
187 FAngleLimits: Boolean;
188 function GetAnchorForTool: TVector3;
189 procedure SetAnchor(const AValue: TVector3);
190 procedure SetAxis(const AValue: TVector3);
191 procedure SetAngleLimits(const AValue: Boolean);
192 procedure SetMinAngle(const AValue: Single);
193 procedure SetMaxAngle(const AValue: Single);
194 procedure InternalUpdateGizmosName;
195 protected
196 function GetKraftJoint: TKraftConstraintJoint; override;
197 procedure CreateKraftJoint; override;
198 procedure DeinitializeKraftJoint; override;
199 procedure SetName(const Value: TComponentName); override;
200 public
201 constructor Create(AOwner: TComponent); override;
202 destructor Destroy; override;
203
204 procedure WorldBeforeDetach; override;
205 procedure InternalCreateGizmos; override;
206 procedure InternalDestroyGizmos; override;
207 function PropertySections(const PropertyName: String): TPropertySections; override;
208
209 { Position of the hinge in the parent transformation coordinates. }
210 property Anchor: TVector3 read FAnchor write SetAnchor;
211
212 { Rotation axis around which the @link(Connected) rotates.
213 By default +Y. }
214 property Axis: TVector3 read FAxis write SetAxis;
215 published
216 { Turns on minimum and maximum rotation angle limit. }
217 property AngleLimits: Boolean read FAngleLimits write SetAngleLimits default false;
218
219 { Minimal angle, in radians, must be a value between -2pi + epsilon to 0.
220 Honored only if @link(AngleLimits). }
221 property MinAngle: Single read FMinAngle write SetMinAngle {$ifdef FPC}default 0.0{$endif};
222
223 { Maximum angle, in radians, must be a value between 0 to 2pi - epsilon.
224 Honored only if @link(AngleLimits). }
225 property MaxAngle: Single read FMaxAngle write SetMaxAngle {$ifdef FPC}default 0.0{$endif};
226
227 {$define read_interface_class}
228 {$I auto_generated_persistent_vectors/tcastlehingejoint_persistent_vectors.inc}
229 {$undef read_interface_class}
230 end;
231
232 { Rope joint connects two bodies by an invisible rope that prevents
233 the distance between them to grow beyond specfied @link(Distance).
234 @link(Anchor) and @link(ConnectedAnchor) define the hook points of the rope. }
235 TCastleRopeJoint = class(TCastleAbstractTwoBodiesJoint)
236 private
237 FKraftJointRope: TKraftConstraintJointRope;
238 FAnchor: TVector3;
239 FConnectedAnchor: TVector3;
240 FDistance: Single;
241 FAnchorTransform: TCastleTransform;
242 FConnectedAnchorTransform: TCastleTransform;
243 function GetAnchorForTool: TVector3;
244 procedure SetAnchor(const AValue: TVector3);
245 function GetConnectedAnchorForTool: TVector3;
246 procedure SetConnectedAnchor(const AValue: TVector3);
247 procedure SetDistance(const AValue: Single);
248 procedure InternalUpdateGizmosName;
249 protected
250 function GetKraftJoint: TKraftConstraintJoint; override;
251 procedure CreateKraftJoint; override;
252 procedure DeinitializeKraftJoint; override;
253
254 procedure OnConnectedFree(const Sender: TFreeNotificationObserver); override;
255 protected
256 procedure SetName(const Value: TComponentName); override;
257 public
258 constructor Create(AOwner: TComponent); override;
259 destructor Destroy; override;
260
261 procedure WorldBeforeDetach; override;
262 procedure InternalCreateGizmos; override;
263 procedure InternalDestroyGizmos; override;
264 function PropertySections(const PropertyName: String): TPropertySections; override;
265
266 { First hook point that is located in parent transform local coordinates. }
267 property Anchor: TVector3 read FAnchor write SetAnchor;
268
269 { Second hook point that is located in Connected local coordinates. }
270 property ConnectedAnchor: TVector3 read FConnectedAnchor
271 write SetConnectedAnchor;
272 published
273 { Maximum rope length (between Anchor and ConnectedAnchor) }
274 property Distance: Single read FDistance write SetDistance {$ifdef FPC}default 0.0{$endif};
275
276 {$define read_interface_class}
277 {$I auto_generated_persistent_vectors/tcastleropejoint_persistent_vectors.inc}
278 {$undef read_interface_class}
279 end;
280
281 { Ball joint allows to freely rotate the transformation of one object relative to another
282 (rotate in any axis) but does not allow any movement.
283 @link(Anchor) determines the rotation point. }
284 TCastleBallJoint = class(TCastleAbstractTwoBodiesJoint)
285 private
286 FKraftJointBall: TKraftConstraintJointBallSocket;
287 FAnchor: TVector3;
288 FAnchorTransform: TCastleTransform;
289 function GetAnchorForTool: TVector3;
290 procedure SetAnchor(const AValue: TVector3);
291 procedure InternalUpdateGizmosName;
292 protected
293 function GetKraftJoint: TKraftConstraintJoint; override;
294 procedure CreateKraftJoint; override;
295 procedure DeinitializeKraftJoint; override;
296 procedure SetName(const Value: TComponentName); override;
297 public
298 constructor Create(AOwner: TComponent); override;
299 destructor Destroy; override;
300
301 procedure WorldBeforeDetach; override;
302 procedure InternalCreateGizmos; override;
303 procedure InternalDestroyGizmos; override;
304 function PropertySections(const PropertyName: String): TPropertySections; override;
305
306 { The rotation point of the ball joint in parent transform coordinates. }
307 property Anchor: TVector3 read FAnchor write SetAnchor;
308
309 {$define read_interface_class}
310 {$I auto_generated_persistent_vectors/tcastleballjoint_persistent_vectors.inc}
311 {$undef read_interface_class}
312 end;
313
314 { Distance joint tries to maintain a certain distance between two rigid bodies,
315 with certain flexibility (like a spring).
316 @link(Anchor) and @link(ConnectedAnchor) actually determine the points between
317 which the distance is observed. }
318 TCastleDistanceJoint = class(TCastleAbstractTwoBodiesJoint)
319 private
320 FKraftJointDistance: TKraftConstraintJointDistance;
321 FAnchor: TVector3;
322 FConnectedAnchor: TVector3;
323 FFrequencyHz: Single;
324 FDampingRatio: Single;
325 FAnchorTransform: TCastleTransform;
326 FConnectedAnchorTransform: TCastleTransform;
327
328 function GetAnchorForTool: TVector3;
329 procedure SetAnchor(const AValue: TVector3);
330 function GetConnectedAnchorForTool: TVector3;
331 procedure SetConnectedAnchor(const AValue: TVector3);
332 procedure SetFrequencyHz(const AValue: Single);
333 procedure SetDampingRatio(const AValue: Single);
334 procedure InternalUpdateGizmosName;
335 protected
336 function GetKraftJoint: TKraftConstraintJoint; override;
337 procedure CreateKraftJoint; override;
338 procedure DeinitializeKraftJoint; override;
339 procedure SetName(const Value: TComponentName); override;
340 public
341 constructor Create(AOwner: TComponent); override;
342 destructor Destroy; override;
343
344 procedure WorldBeforeDetach; override;
345 procedure InternalCreateGizmos; override;
346 procedure InternalDestroyGizmos; override;
347 function PropertySections(const PropertyName: String): TPropertySections; override;
348
349 { First point from which the distance is measured. }
350 property Anchor: TVector3 read FAnchor write SetAnchor;
351
352 { Second point from which the distance is measured. }
353 property ConnectedAnchor: TVector3 read FConnectedAnchor
354 write SetConnectedAnchor;
355 published
356 { Frequency controls how fast the oscillations occur. The smaller the value,
357 the longer the oscillation movement. This may not seem intuitive,
358 but a higher value causes the oscillation in specified time to be faster
359 and therefore the movement is shorter.
360
361 So if you want longer movement make this value smaller.
362
363 See live example: https://gamedev.stackexchange.com/questions/98772/b2distancejoint-with-frequency-equal-to-0-vs-b2weldjoint }
364 property FrequencyHz: Single read FFrequencyHz write SetFrequencyHz {$ifdef FPC}default 0.0{$endif};
365
366 { How fast the oscillations are damped, large values can cause the body
367 to not do even one oscillation. }
368 property DampingRatio: Single read FDampingRatio write SetDampingRatio {$ifdef FPC}default 0.0{$endif};
369
370 {$define read_interface_class}
371 {$I auto_generated_persistent_vectors/tcastledistancejoint_persistent_vectors.inc}
372 {$undef read_interface_class}
373 end;
374
375 { Grab joint pulls the rigid body @link(Anchor) towards a @link(TargetWorld) defined
376 in the world coordinates.
377 The pull is not instant, rather it works like a flexible spring / elastic rope. }
378 TCastleGrabJoint = class(TCastleAbstractOneBodyJoint)
379 private
380 FDampingRatio: Single;
381 FFrequencyHz: Single;
382 FKraftJointGrab: TKraftConstraintJointGrab;
383 FMaxForce: Single;
384 FAnchor, FTargetWorld: TVector3;
385 FAnchorTransform: TCastleTransform;
386 FTargetWorldTransform: TCastleTransform;
387
388 function GetAnchorForTool: TVector3;
389 procedure SetAnchor(const AValue: TVector3);
390 function GetTargetWorldForTool: TVector3;
391 procedure SetTargetWorld(const AValue: TVector3);
392 procedure SetFrequencyHz(const AValue: Single);
393 procedure SetDampingRatio(const AValue: Single);
394 procedure SetMaxForce(const AValue: Single);
395 procedure InternalUpdateGizmosName;
396 { Returns MaxSingle when MaxForce is zero. }
397 function MaxForceNonZero: Single;
398 protected
399 function GetKraftJoint: TKraftConstraintJoint; override;
400 procedure CreateKraftJoint; override;
401 procedure DeinitializeKraftJoint; override;
402 procedure SetName(const Value: TComponentName); override;
403 public
404 const
405 DefaultFrequencyHz = 5.0;
406 DefaultDampingRatio = 0.7;
407
408 constructor Create(AOwner: TComponent); override;
409 destructor Destroy; override;
410
411 procedure WorldBeforeDetach; override;
412 procedure InternalCreateGizmos; override;
413 procedure InternalDestroyGizmos; override;
414 function PropertySections(const PropertyName: String): TPropertySections; override;
415
416 { Point, in local coordinates, that will be pulled to @link(TargetWorld). }
417 property Anchor: TVector3 read FAnchor write SetAnchor;
418
419 { Point, in world coordinates, that pulls @link(Anchor).
420 With time, @link(Anchor) (in local coords) will match @link(TargetWorld) (in world coords). }
421 property TargetWorld: TVector3 read FTargetWorld write SetTargetWorld;
422 published
423 { Frequency controls how fast the oscillations occur. The smaller the value,
424 the longer the oscillation movement. This may not seem intuitive,
425 but a higher value causes the oscillation in specified time to be faster
426 and therefore the movement is shorter.
427
428 So if you want longer movement make this value smaller.
429
430 See live example: https://gamedev.stackexchange.com/questions/98772/b2distancejoint-with-frequency-equal-to-0-vs-b2weldjoint }
431 property FrequencyHz: Single read FFrequencyHz write SetFrequencyHz {$ifdef FPC}default DefaultFrequencyHz{$endif};
432
433 { How fast the oscillations are damped, large values can cause the body
434 will not do even one oscillation. }
435 property DampingRatio: Single read FDampingRatio write SetDampingRatio {$ifdef FPC}default DefaultDampingRatio{$endif};
436
437 { Maximal force that will be aplied to the body to move it.
438 Zero (default) means "unlimited". }
439 property MaxForce: Single read FMaxForce write SetMaxForce {$ifdef FPC}default 0.0{$endif};
440
441 {$define read_interface_class}
442 {$I auto_generated_persistent_vectors/tcastlegrabjoint_persistent_vectors.inc}
443 {$undef read_interface_class}
444 end;
445
446 {$endif read_interface}
447
448 {$ifdef read_implementation}
449
450 { TCastleAbstractTwoBodiesJoint ---------------------------------------------------- }
451
452 procedure TCastleAbstractTwoBodiesJoint.SetConnectedCollide(const AValue: Boolean);
453 begin
454 if FConnectedCollide = AValue then
455 Exit;
456 FConnectedCollide := AValue;
457 RecreateKraftJoint;
458 end;
459
460 function TCastleAbstractTwoBodiesJoint.AllNeededKraftObjectsInitialized: Boolean;
461 begin
462 Result := (inherited AllNeededKraftObjectsInitialized) and
463 (FConnectedRigidBody <> nil) and (FConnectedRigidBody.IsInitialized);
464 end;
465
466 procedure TCastleAbstractTwoBodiesJoint.OnConnectedFree(
467 const Sender: TFreeNotificationObserver);
468 begin
469 DeinitializeKraftJoint;
470 RemoveConnectedRigidBodyEvents;
471 RemoveConnectedEvents;
472 FConnectedRigidBody := nil;
473 FConnected := nil;
474 end;
475
476 procedure TCastleAbstractTwoBodiesJoint.OnConnectedAddBehavior(
477 const Component: TComponent);
478 begin
479 FConnectedRigidBody := Component as TCastleRigidBody;
480 SetConnectedRigidBodyEvents;
481 end;
482
483 procedure TCastleAbstractTwoBodiesJoint.OnConnectedTransfromRemoveBehavior(
484 const Component: TComponent);
485 begin
486 DeinitializeKraftJoint;
487 RemoveConnectedRigidBodyEvents;
488 FConnectedRigidBody := nil;
489 end;
490
491 procedure TCastleAbstractTwoBodiesJoint.OnConnectedRigidBodyInitialized(
492 const Component: TComponent);
493 begin
494 RecreateKraftJoint;
495 end;
496
497 procedure TCastleAbstractTwoBodiesJoint.OnBeforeConnectedRigidBodyDeinitialized(
498 const Component: TComponent);
499 begin
500 DeinitializeKraftJoint;
501 end;
502
503 procedure TCastleAbstractTwoBodiesJoint.SetConnectedEvents;
504 begin
505 if FConnected = nil then
506 Exit;
507
508 FConnected.AddNotificationOnBehaviorAdded(
509 {$ifdef FPC}@{$endif} OnConnectedAddBehavior, TCastleRigidBody);
510
511 FConnected.AddNotificationBeforeBehaviorRemove(
512 {$ifdef FPC}@{$endif} OnConnectedTransfromRemoveBehavior, TCastleRigidBody);
513
514 if FConnectedTransfromFreeObserver = nil then
515 begin
516 FConnectedTransfromFreeObserver := TFreeNotificationObserver.Create(Self);
517 FConnectedTransfromFreeObserver.OnFreeNotification := {$ifdef FPC}@{$endif}OnConnectedFree;
518 end;
519
520 FConnectedTransfromFreeObserver.Observed := FConnected;
521 end;
522
523 procedure TCastleAbstractTwoBodiesJoint.RemoveConnectedEvents;
524 begin
525 if FConnected <> nil then
526 begin
527 { Remove events from previous connected castle transform }
528 FConnected.AddNotificationOnBehaviorAdded(
529 {$ifdef FPC}@{$endif} OnConnectedAddBehavior, TCastleRigidBody);
530
531 FConnected.AddNotificationBeforeBehaviorRemove(
532 {$ifdef FPC}@{$endif} OnConnectedTransfromRemoveBehavior, TCastleRigidBody);
533 end;
534
535 { Stop watching this transform }
536 if FConnectedTransfromFreeObserver <> nil then
537 FConnectedTransfromFreeObserver.Observed := nil;
538 end;
539
540 procedure TCastleAbstractTwoBodiesJoint.SetConnectedRigidBodyEvents;
541 begin
542 if FConnectedRigidBody <> nil then
543 begin
544 FConnectedRigidBody.AddNotificationOnInitialized({$ifdef FPC}@{$endif} OnConnectedRigidBodyInitialized);
545 FConnectedRigidBody.AddNotificationBeforeDeinitialized({$ifdef FPC}@{$endif} OnBeforeConnectedRigidBodyDeinitialized);
546 end;
547 end;
548
549 procedure TCastleAbstractTwoBodiesJoint.RemoveConnectedRigidBodyEvents;
550 begin
551 if FConnectedRigidBody <> nil then
552 begin
553 FConnectedRigidBody.RemoveNotificationOnInitialized({$ifdef FPC}@{$endif} OnConnectedRigidBodyInitialized);
554 FConnectedRigidBody.RemoveNotificationBeforeDeinitialized({$ifdef FPC}@{$endif} OnBeforeConnectedRigidBodyDeinitialized);
555 end;
556 end;
557
558 function TCastleAbstractTwoBodiesJoint.PropertySections(const PropertyName: String
559 ): TPropertySections;
560 begin
561 if ArrayContainsString(PropertyName, [
562 'Connected', 'ConnectedCollide'
563 ]) then
564 Result := [psBasic]
565 else
566 Result := inherited PropertySections(PropertyName);
567 end;
568
569 procedure TCastleAbstractTwoBodiesJoint.ParentAfterAttach;
570 begin
571 { Remove connected transform if new parent is the same transform }
572 if Parent = FConnected then
573 begin
574 RemoveConnectedRigidBodyEvents;
575 FConnectedRigidBody := nil;
576 RemoveConnectedEvents;
577 FConnected := nil;
578
579 raise Exception.Create('Connected is the same as Parent of TCastleAbstractTwoBodiesJoint, this is not allowed');
580 // WritelnWarning...
581 end;
582 end;
583
584 procedure TCastleAbstractTwoBodiesJoint.InitializeKraftJoint;
585 begin
586 inherited InitializeKraftJoint;
587
588 // make sure connected rigid body is active after joint creation
589 FConnectedRigidBody.WakeUp;
590 end;
591
592 procedure TCastleAbstractTwoBodiesJoint.SetConnected(
593 const AConnected: TCastleTransform);
594 begin
595 if (AConnected <> nil) and (AConnected = Parent) then
596 raise EConnectedSameAsParent.Create(
597 'Can''t set Connected to behavior Parent.');
598
599 if FConnected <> AConnected then
600 begin
601 if FConnected <> nil then
602 begin
603 { Remove events from previous connected castle transform }
604 RemoveConnectedEvents;
605
606 { Remove events from Rigidbody of old connected transform }
607 RemoveConnectedRigidBodyEvents;
608 end;
609
610 FConnected := AConnected;
611 if FConnected <> nil then
612 begin
613 { If FConnected is not nil set all events and try
614 recreate joint }
615 SetConnectedEvents;
616
617 FConnectedRigidBody := FConnected.FindBehavior(TCastleRigidBody) as TCastleRigidBody;
618 if FConnectedRigidBody <> nil then
619 begin
620 SetConnectedRigidBodyEvents;
621 RecreateKraftJoint;
622 end;
623 end;
624 end;
625 end;
626
627 { TCastleAbstractJoint ------------------------------------------------------------- }
628
629 destructor TCastleAbstractJoint.Destroy;
630 begin
631 DeinitializeKraftJoint;
632 inherited Destroy;
633 end;
634
635 procedure TCastleAbstractJoint.RecreateKraftJoint;
636 begin
637 DeinitializeKraftJoint;
638 if AllNeededKraftObjectsInitialized then
639 InitializeKraftJoint;
640 end;
641
642 procedure TCastleAbstractJoint.InitializeKraftJoint;
643 begin
644 CreateKraftJoint;
645 // make sure rigid body is active after joint creation
646 FParentRigidBody.WakeUp;
647 UpdateBreakableInKraft;
648 UpdateBreakThresholdForceInKraft;
649 UpdateBreakThresholdTorqueInKraft;
650 end;
651
652 function TCastleAbstractJoint.AllNeededKraftObjectsInitialized: Boolean;
653 begin
654 Result := (FParentRigidBody <> nil) and FParentRigidBody.IsInitialized;
655 end;
656
657 procedure TCastleAbstractJoint.OnParentTransformAddBehavior(
658 const Component: TComponent);
659 begin
660 FParentRigidBody := Component as TCastleRigidBody;
661 SetParentRigidBodyEvents;
662 end;
663
664 procedure TCastleAbstractJoint.OnBeforeParentTransfromRemoveBehavior(
665 const Component: TComponent);
666 begin
667 DeinitializeKraftJoint;
668 RemoveParentRigidBodyEvents;
669 FParentRigidBody := nil;
670 end;
671
672 procedure TCastleAbstractJoint.OnParentRigidBodyInitialized(
673 const Component: TComponent);
674 begin
675 RecreateKraftJoint;
676 end;
677
678 procedure TCastleAbstractJoint.OnBeforeParentRigidBodyDeinitialized(
679 const Component: TComponent);
680 begin
681 DeinitializeKraftJoint;
682 end;
683
684 procedure TCastleAbstractJoint.SetParentRigidBodyEvents;
685 begin
686 if FParentRigidBody <> nil then
687 begin
688 FParentRigidBody.AddNotificationOnInitialized({$ifdef FPC}@{$endif} OnParentRigidBodyInitialized);
689 FParentRigidBody.AddNotificationBeforeDeinitialized({$ifdef FPC}@{$endif} OnBeforeParentRigidBodyDeinitialized);
690 end;
691 end;
692
693 procedure TCastleAbstractJoint.RemoveParentRigidBodyEvents;
694 begin
695 if FParentRigidBody <> nil then
696 begin
697 FParentRigidBody.RemoveNotificationOnInitialized({$ifdef FPC}@{$endif} OnParentRigidBodyInitialized);
698 FParentRigidBody.RemoveNotificationBeforeDeinitialized({$ifdef FPC}@{$endif} OnBeforeParentRigidBodyDeinitialized);
699 end;
700 end;
701
702 procedure TCastleAbstractJoint.UpdateBreakThresholdForceInKraft;
703 begin
704 if GetKraftJoint <> nil then
705 GetKraftJoint.BreakThresholdForce := FBreakThresholdForce;
706 end;
707
708 procedure TCastleAbstractJoint.UpdateBreakThresholdTorqueInKraft;
709 begin
710 if GetKraftJoint <> nil then
711 GetKraftJoint.BreakThresholdTorque := FBreakThresholdTorque;
712 end;
713
714 procedure TCastleAbstractJoint.UpdateBreakableInKraft;
715 begin
716 if GetKraftJoint <> nil then
717 begin
718 if FBreakable then
719 GetKraftJoint.Flags := GetKraftJoint.Flags + [kcfBreakable]
720 else
721 GetKraftJoint.Flags := GetKraftJoint.Flags - [kcfBreakable];
722 end;
723 end;
724
725 procedure TCastleAbstractJoint.SetBreakThresholdForce(const AValue: Single);
726 begin
727 if FBreakThresholdForce <> AValue then
728 begin
729 FBreakThresholdForce := AValue;
730 UpdateBreakThresholdForceInKraft;
731 end;
732 end;
733
734 procedure TCastleAbstractJoint.SetBreakThresholdTorque(const AValue: Single);
735 begin
736 if FBreakThresholdTorque <> AValue then
737 begin
738 FBreakThresholdTorque := AValue;
739 UpdateBreakThresholdTorqueInKraft;
740 end;
741 end;
742
743 procedure TCastleAbstractJoint.SetBreakable(const AValue: Boolean);
744 begin
745 if FBreakable <> AValue then
746 begin
747 FBreakable := AValue;
748 UpdateBreakableInKraft;
749 end;
750 end;
751
752 procedure TCastleAbstractJoint.WorldAfterAttach;
753 begin
754 inherited WorldAfterAttach;
755
756 { This means we are added to world so we need to get parent and check
757 Parent has rigid body if has we should try recreate kraft joint. }
758
759 Parent.AddNotificationOnBehaviorAdded(
760 {$ifdef FPC}@{$endif} OnParentTransformAddBehavior, TCastleRigidBody);
761
762 Parent.AddNotificationBeforeBehaviorRemove(
763 {$ifdef FPC}@{$endif} OnBeforeParentTransfromRemoveBehavior, TCastleRigidBody);
764
765 FParentRigidBody := Parent.FindBehavior(TCastleRigidBody) as TCastleRigidBody;
766 if FParentRigidBody <> nil then
767 begin
768 SetParentRigidBodyEvents;
769 RecreateKraftJoint;
770 end;
771 end;
772
773 procedure TCastleAbstractJoint.WorldBeforeDetach;
774 begin
775 { This means we are before detach from world so we need to get parent
776 remove all callbacks also rigid body callbacks }
777
778 Parent.RemoveNotificationOnBehaviorAdded(
779 {$ifdef FPC}@{$endif} OnParentTransformAddBehavior, TCastleRigidBody);
780
781 Parent.RemoveNotificationBeforeBehaviorRemove(
782 {$ifdef FPC}@{$endif} OnBeforeParentTransfromRemoveBehavior, TCastleRigidBody);
783
784 RemoveParentRigidBodyEvents;
785 inherited ParentBeforeDetach;
786 end;
787
788 function TCastleAbstractJoint.PropertySections(const PropertyName: String
789 ): TPropertySections;
790 begin
791 if ArrayContainsString(PropertyName, [
792 'Breakable', 'BreakThresholdForce', 'BreakThresholdTorque'
793 ]) then
794 Result := [psBasic]
795 else
796 Result := inherited PropertySections(PropertyName);
797 end;
798
799 procedure TCastleAbstractJoint.InternalCreateGizmos;
800 begin
801 // nothing in this class
802 end;
803
804 procedure TCastleAbstractJoint.InternalDestroyGizmos;
805 begin
806 // nothing in this class
807 end;
808
809 { TCastleHingeJoint ---------------------------------------------------------------- }
810
811 constructor TCastleHingeJoint.Create(AOwner: TComponent);
812 begin
813 inherited Create(AOwner);
814 ListenWorldChange := true;
815 FAxis:= TVector3.One[1];
816 FAnchor := TVector3.Zero;
817
818 {$define read_implementation_constructor}
819 {$I auto_generated_persistent_vectors/tcastlehingejoint_persistent_vectors.inc}
820 {$undef read_implementation_constructor}
821 end;
822
823 destructor TCastleHingeJoint.Destroy;
824 begin
825 {$define read_implementation_destructor}
826 {$I auto_generated_persistent_vectors/tcastlehingejoint_persistent_vectors.inc}
827 {$undef read_implementation_destructor}
828
829 inherited Destroy;
830 end;
831
832 procedure TCastleHingeJoint.WorldBeforeDetach;
833 begin
834 FreeAndNil(FAnchorTransform);
835 inherited WorldBeforeDetach;
836 end;
837
838 procedure TCastleHingeJoint.InternalCreateGizmos;
839 begin
840 inherited;
841 if FAnchorTransform = nil then
842 begin
843 FAnchorTransform := TCastleJointTool.Create(nil);
844 TCastleJointTool(FAnchorTransform).Joint := Self;
845 TCastleJointTool(FAnchorTransform).OnGetValue := {$ifdef FPC}@{$endif} GetAnchorForTool;
846 TCastleJointTool(FAnchorTransform).OnSetValue := {$ifdef FPC}@{$endif} SetAnchor;
847 InternalUpdateGizmosName;
848 Parent.Add(FAnchorTransform);
849 TCastleJointTool(FAnchorTransform).Value := Anchor;
850 InternalCastleDesignInvalidate := true;
851 end;
852 end;
853
854 procedure TCastleHingeJoint.InternalDestroyGizmos;
855 begin
856 FreeAndNil(FAnchorTransform);
857 InternalCastleDesignInvalidate := true;
858 inherited;
859 end;
860
861 function TCastleHingeJoint.PropertySections(const PropertyName: String
862 ): TPropertySections;
863 begin
864 if ArrayContainsString(PropertyName, [
865 'AnchorPersistent', 'AngleLimits', 'MinAngle', 'MaxAngle',
866 'AxisPersistent'
867 ]) then
868 Result := [psBasic]
869 else
870 Result := inherited PropertySections(PropertyName);
871 end;
872
873 function TCastleHingeJoint.GetAnchorForTool: TVector3;
874 begin
875 Result := Anchor;
876 end;
877
878 procedure TCastleHingeJoint.SetAnchor(const AValue: TVector3);
879 begin
880 if not TVector3.PerfectlyEquals(FAnchor, AValue) then
881 begin
882 FAnchor := AValue;
883 if FAnchorTransform <> nil then
884 TCastleJointTool(FAnchorTransform).Value := FAnchor;
885 RecreateKraftJoint;
886 end;
887 end;
888
889 procedure TCastleHingeJoint.SetAxis(const AValue: TVector3);
890 begin
891 if not TVector3.PerfectlyEquals(FAxis, AValue) then
892 begin
893 FAxis := AValue;
894 if FKraftJointHinge <> nil then
895 FKraftJointHinge.SetWorldRotationAxis(
896 VectorToKraft(Parent.LocalToWorldDirection(FAxis)));
897 end;
898 end;
899
900 procedure TCastleHingeJoint.SetAngleLimits(const AValue: Boolean);
901 begin
902 if FAngleLimits <> AValue then
903 begin
904 FAngleLimits := AValue;
905 if FKraftJointHinge <> nil then
906 FKraftJointHinge.EnableLimit(AValue);
907 end;
908 end;
909
910 procedure TCastleHingeJoint.SetMinAngle(const AValue: Single);
911 begin
912 if FMinAngle <> AValue then
913 begin
914 FMinAngle := AValue;
915 if FKraftJointHinge <> nil then
916 FKraftJointHinge.SetMinimumAngleLimit(FMinAngle);
917 end;
918 end;
919
920 procedure TCastleHingeJoint.SetMaxAngle(const AValue: Single);
921 begin
922 if FMaxAngle <> AValue then
923 begin
924 FMaxAngle := AValue;
925 if FKraftJointHinge <> nil then
926 FKraftJointHinge.SetMaximumAngleLimit(FMaxAngle);
927 end;
928 end;
929
930 procedure TCastleHingeJoint.InternalUpdateGizmosName;
931 begin
932 if (FAnchorTransform <> nil) and (FAnchorTransform.Name <> Name + '_Anchor') then
933 begin
934 FAnchorTransform.Name := Name + '_Anchor';
935 InternalCastleDesignInvalidate := true;
936 end;
937 end;
938
939 function TCastleHingeJoint.GetKraftJoint: TKraftConstraintJoint;
940 begin
941 Result := FKraftJointHinge;
942 end;
943
944 procedure TCastleHingeJoint.CreateKraftJoint;
945 begin
946 FKraftJointHinge := TKraftConstraintJointHinge.Create(
947 World.FKraftEngine,
948 FParentRigidBody.FKraftBody,
949 FConnectedRigidBody.FKraftBody,
950 VectorToKraft(Parent.LocalToWorld(FAnchor)),
951 VectorToKraft(Parent.LocalToWorldDirection(FAxis)),
952 FAngleLimits,
953 false,
954 FMinAngle,
955 FMaxAngle,
956 0,
957 0,
958 ConnectedCollide
959 );
960 end;
961
962 procedure TCastleHingeJoint.DeinitializeKraftJoint;
963 begin
964 FreeAndNil(FKraftJointHinge);
965 end;
966
967 procedure TCastleHingeJoint.SetName(const Value: TComponentName);
968 begin
969 inherited SetName(Value);
970 InternalUpdateGizmosName;
971 end;
972
973 {$define read_implementation_methods}
974 {$I auto_generated_persistent_vectors/tcastlehingejoint_persistent_vectors.inc}
975 {$undef read_implementation_methods}
976
977 { TCastleRopeJoint --------------------------------------------------------------- }
978
979 constructor TCastleRopeJoint.Create(AOwner: TComponent);
980 begin
981 inherited Create(AOwner);
982 ListenWorldChange := true;
983 FAnchor := TVector3.Zero;
984 FConnectedAnchor := TVector3.Zero;
985
986 {$define read_implementation_constructor}
987 {$I auto_generated_persistent_vectors/tcastleropejoint_persistent_vectors.inc}
988 {$undef read_implementation_constructor}
989 end;
990
991 destructor TCastleRopeJoint.Destroy;
992 begin
993 {$define read_implementation_destructor}
994 {$I auto_generated_persistent_vectors/tcastleropejoint_persistent_vectors.inc}
995 {$undef read_implementation_destructor}
996
997 inherited Destroy;
998 end;
999
1000 procedure TCastleRopeJoint.WorldBeforeDetach;
1001 begin
1002 FreeAndNil(FAnchorTransform);
1003 FreeAndNil(FConnectedAnchorTransform);
1004 inherited WorldBeforeDetach;
1005 end;
1006
1007 function TCastleRopeJoint.PropertySections(const PropertyName: String
1008 ): TPropertySections;
1009 begin
1010 if ArrayContainsString(PropertyName, [
1011 'AnchorPersistent', 'ConnectedAnchorPersistent', 'Distance'
1012 ]) then
1013 Result := [psBasic]
1014 else
1015 Result := inherited PropertySections(PropertyName);
1016 end;
1017
1018 procedure TCastleRopeJoint.InternalCreateGizmos;
1019 begin
1020 inherited;
1021 if FAnchorTransform = nil then
1022 begin
1023 FAnchorTransform := TCastleJointTool.Create(nil);
1024 TCastleJointTool(FAnchorTransform).Joint := Self;
1025 TCastleJointTool(FAnchorTransform).OnGetValue := {$ifdef FPC}@{$endif} GetAnchorForTool;
1026 TCastleJointTool(FAnchorTransform).OnSetValue := {$ifdef FPC}@{$endif} SetAnchor;
1027 InternalUpdateGizmosName;
1028 Parent.Add(FAnchorTransform);
1029 FAnchorTransform.Translation := Anchor;
1030 InternalCastleDesignInvalidate := true;
1031 end;
1032
1033 if (FConnectedAnchorTransform = nil) and (FConnectedRigidBody <> nil) and (FConnectedRigidBody.Parent <> nil) then
1034 begin
1035 FConnectedAnchorTransform := TCastleJointTool.Create(nil);
1036 TCastleJointTool(FConnectedAnchorTransform).Joint := Self;
1037 TCastleJointTool(FConnectedAnchorTransform).OnGetValue := {$ifdef FPC}@{$endif} GetConnectedAnchorForTool;
1038 TCastleJointTool(FConnectedAnchorTransform).OnSetValue := {$ifdef FPC}@{$endif} SetConnectedAnchor;
1039 TCastleJointTool(FConnectedAnchorTransform).Color := ConnectedAnchorColor;
1040 InternalUpdateGizmosName;
1041 FConnectedAnchorTransform.Translation := ConnectedAnchor;
1042 FConnectedRigidBody.Parent.Add(FConnectedAnchorTransform);
1043 InternalCastleDesignInvalidate := true;
1044 end;
1045 end;
1046
1047 procedure TCastleRopeJoint.InternalDestroyGizmos;
1048 begin
1049 FreeAndNil(FAnchorTransform);
1050 FreeAndNil(FConnectedAnchorTransform);
1051 InternalCastleDesignInvalidate := true;
1052 inherited;
1053 end;
1054
1055 function TCastleRopeJoint.GetAnchorForTool: TVector3;
1056 begin
1057 Result := Anchor;
1058 end;
1059
1060 procedure TCastleRopeJoint.SetAnchor(const AValue: TVector3);
1061 begin
1062 if not TVector3.PerfectlyEquals(FAnchor, AValue) then
1063 begin
1064 FAnchor := AValue;
1065 if FAnchorTransform <> nil then
1066 TCastleJointTool(FAnchorTransform).Value := FAnchor;
1067 RecreateKraftJoint;
1068 end;
1069 end;
1070
1071 function TCastleRopeJoint.GetConnectedAnchorForTool: TVector3;
1072 begin
1073 Result := ConnectedAnchor;
1074 end;
1075
1076 procedure TCastleRopeJoint.SetConnectedAnchor(const AValue: TVector3);
1077 begin
1078 if not TVector3.PerfectlyEquals(FConnectedAnchor, AValue) then
1079 begin
1080 FConnectedAnchor := AValue;
1081 if FConnectedAnchorTransform <> nil then
1082 TCastleJointTool(FConnectedAnchorTransform).Value := FConnectedAnchor;
1083 RecreateKraftJoint;
1084 end;
1085 end;
1086
1087 procedure TCastleRopeJoint.SetDistance(const AValue: Single);
1088 begin
1089 if not SameValue(FDistance, AValue) then
1090 begin
1091 FDistance := AValue;
1092 RecreateKraftJoint;
1093 end;
1094 end;
1095
1096 procedure TCastleRopeJoint.InternalUpdateGizmosName;
1097 begin
1098 if (FAnchorTransform <> nil) and (FAnchorTransform.Name <> Name + '_Anchor') then
1099 begin
1100 FAnchorTransform.Name := Name + '_Anchor';
1101 InternalCastleDesignInvalidate := true;
1102 end;
1103
1104 if (FConnectedAnchorTransform <> nil) and
1105 (FConnectedAnchorTransform.Name <> Name + '_ConnectedAnchor') then
1106 begin
1107 FConnectedAnchorTransform.Name := Name + '_ConnectedAnchor';
1108 InternalCastleDesignInvalidate := true;
1109 end;
1110 end;
1111
1112 function TCastleRopeJoint.GetKraftJoint: TKraftConstraintJoint;
1113 begin
1114 Result := FKraftJointRope;
1115 end;
1116
1117 procedure TCastleRopeJoint.CreateKraftJoint;
1118 begin
1119 FKraftJointRope := TKraftConstraintJointRope.Create(
1120 World.FKraftEngine,
1121 FParentRigidBody.FKraftBody,
1122 FConnectedRigidBody.FKraftBody,
1123 VectorToKraft(FAnchor),
1124 VectorToKraft(FConnectedAnchor),
1125 FDistance,
1126 ConnectedCollide
1127 );
1128 end;
1129
1130 procedure TCastleRopeJoint.DeinitializeKraftJoint;
1131 begin
1132 FreeAndNil(FKraftJointRope);
1133 end;
1134
1135 procedure TCastleRopeJoint.OnConnectedFree(const Sender: TFreeNotificationObserver);
1136 begin
1137 { Remove connected transform anchor visualisation because
1138 its parent is freed }
1139 FreeAndNil(FConnectedAnchorTransform);
1140
1141 inherited OnConnectedFree(Sender);
1142 end;
1143
1144 procedure TCastleRopeJoint.SetName(const Value: TComponentName);
1145 begin
1146 inherited SetName(Value);
1147 InternalUpdateGizmosName;
1148 end;
1149
1150 {$define read_implementation_methods}
1151 {$I auto_generated_persistent_vectors/tcastleropejoint_persistent_vectors.inc}
1152 {$undef read_implementation_methods}
1153
1154 { TCastleBallJoint --------------------------------------------------------------- }
1155
1156 constructor TCastleBallJoint.Create(AOwner: TComponent);
1157 begin
1158 inherited Create(AOwner);
1159 ListenWorldChange := true;
1160 FAnchor := TVector3.Zero;
1161 {$define read_implementation_constructor}
1162 {$I auto_generated_persistent_vectors/tcastleballjoint_persistent_vectors.inc}
1163 {$undef read_implementation_constructor}
1164 end;
1165
1166 destructor TCastleBallJoint.Destroy;
1167 begin
1168 {$define read_implementation_destructor}
1169 {$I auto_generated_persistent_vectors/tcastleballjoint_persistent_vectors.inc}
1170 {$undef read_implementation_destructor}
1171
1172 inherited Destroy;
1173 end;
1174
1175 procedure TCastleBallJoint.WorldBeforeDetach;
1176 begin
1177 FreeAndNil(FAnchorTransform);
1178 inherited WorldBeforeDetach;
1179 end;
1180
1181 procedure TCastleBallJoint.InternalCreateGizmos;
1182 begin
1183 inherited;
1184 if FAnchorTransform = nil then
1185 begin
1186 FAnchorTransform := TCastleJointTool.Create(nil);
1187 TCastleJointTool(FAnchorTransform).Joint := Self;
1188 TCastleJointTool(FAnchorTransform).OnGetValue := {$ifdef FPC}@{$endif} GetAnchorForTool;
1189 TCastleJointTool(FAnchorTransform).OnSetValue := {$ifdef FPC}@{$endif} SetAnchor;
1190 InternalUpdateGizmosName;
1191 Parent.Add(FAnchorTransform);
1192 TCastleJointTool(FAnchorTransform).Value := Anchor;
1193 InternalCastleDesignInvalidate := true;
1194 end;
1195 end;
1196
1197 procedure TCastleBallJoint.InternalDestroyGizmos;
1198 begin
1199 FreeAndNil(FAnchorTransform);
1200 InternalCastleDesignInvalidate := true;
1201 inherited;
1202 end;
1203
1204 function TCastleBallJoint.PropertySections(const PropertyName: String): TPropertySections;
1205 begin
1206 if (PropertyName = 'AnchorPersistent') then
1207 Result := [psBasic]
1208 else
1209 Result := inherited PropertySections(PropertyName);
1210 end;
1211
1212 function TCastleBallJoint.GetAnchorForTool: TVector3;
1213 begin
1214 Result := Anchor;
1215 end;
1216
1217 procedure TCastleBallJoint.SetAnchor(const AValue: TVector3);
1218 begin
1219 if not TVector3.PerfectlyEquals(FAnchor, AValue) then
1220 begin
1221 FAnchor := AValue;
1222 if FAnchorTransform <> nil then
1223 TCastleJointTool(FAnchorTransform).Value := FAnchor;
1224 RecreateKraftJoint;
1225 end;
1226 end;
1227
1228 procedure TCastleBallJoint.InternalUpdateGizmosName;
1229 begin
1230 if (FAnchorTransform <> nil) and (FAnchorTransform.Name <> Name + '_Anchor') then
1231 begin
1232 FAnchorTransform.Name := Name + '_Anchor';
1233 InternalCastleDesignInvalidate := true;
1234 end;
1235 end;
1236
1237 function TCastleBallJoint.GetKraftJoint: TKraftConstraintJoint;
1238 begin
1239 Result := FKraftJointBall;
1240 end;
1241
1242 procedure TCastleBallJoint.CreateKraftJoint;
1243 begin
1244 FKraftJointBall := TKraftConstraintJointBallSocket.Create(
1245 World.FKraftEngine,
1246 FParentRigidBody.FKraftBody,
1247 FConnectedRigidBody.FKraftBody,
1248 VectorToKraft(Parent.LocalToWorld(FAnchor)),
1249 ConnectedCollide
1250 );
1251 end;
1252
1253 procedure TCastleBallJoint.DeinitializeKraftJoint;
1254 begin
1255 FreeAndNil(FKraftJointBall);
1256 end;
1257
1258 procedure TCastleBallJoint.SetName(const Value: TComponentName);
1259 begin
1260 inherited SetName(Value);
1261 InternalUpdateGizmosName;
1262 end;
1263
1264 {$define read_implementation_methods}
1265 {$I auto_generated_persistent_vectors/tcastleballjoint_persistent_vectors.inc}
1266 {$undef read_implementation_methods}
1267
1268 { TCastleDistanceJoint ------------------------------------------------------------- }
1269
1270 constructor TCastleDistanceJoint.Create(AOwner: TComponent);
1271 begin
1272 inherited Create(AOwner);
1273 ListenWorldChange := true;
1274 FAnchor := TVector3.Zero;
1275 FConnectedAnchor := TVector3.Zero;
1276 {$define read_implementation_constructor}
1277 {$I auto_generated_persistent_vectors/tcastledistancejoint_persistent_vectors.inc}
1278 {$undef read_implementation_constructor}
1279 end;
1280
1281 destructor TCastleDistanceJoint.Destroy;
1282 begin
1283 {$define read_implementation_destructor}
1284 {$I auto_generated_persistent_vectors/tcastledistancejoint_persistent_vectors.inc}
1285 {$undef read_implementation_destructor}
1286 inherited Destroy;
1287 end;
1288
1289 procedure TCastleDistanceJoint.WorldBeforeDetach;
1290 begin
1291 FreeAndNil(FAnchorTransform);
1292 FreeAndNil(FConnectedAnchorTransform);
1293 inherited WorldBeforeDetach;
1294 end;
1295
1296 procedure TCastleDistanceJoint.InternalCreateGizmos;
1297 begin
1298 inherited;
1299 if FAnchorTransform = nil then
1300 begin
1301 FAnchorTransform := TCastleJointTool.Create(nil);
1302 TCastleJointTool(FAnchorTransform).Joint := Self;
1303 TCastleJointTool(FAnchorTransform).OnGetValue := {$ifdef FPC}@{$endif} GetAnchorForTool;
1304 TCastleJointTool(FAnchorTransform).OnSetValue := {$ifdef FPC}@{$endif} SetAnchor;
1305 InternalUpdateGizmosName;
1306 Parent.Add(FAnchorTransform);
1307 FAnchorTransform.Translation := Anchor;
1308 InternalCastleDesignInvalidate := true;
1309 end;
1310
1311 if (FConnectedAnchorTransform = nil) and (FConnectedRigidBody <> nil) and (FConnectedRigidBody.Parent <> nil) then
1312 begin
1313 FConnectedAnchorTransform := TCastleJointTool.Create(nil);
1314 TCastleJointTool(FConnectedAnchorTransform).Joint := Self;
1315 TCastleJointTool(FConnectedAnchorTransform).OnGetValue := {$ifdef FPC}@{$endif} GetConnectedAnchorForTool;
1316 TCastleJointTool(FConnectedAnchorTransform).OnSetValue := {$ifdef FPC}@{$endif} SetConnectedAnchor;
1317 TCastleJointTool(FConnectedAnchorTransform).Color := ConnectedAnchorColor;
1318 InternalUpdateGizmosName;
1319 FConnectedAnchorTransform.Translation := ConnectedAnchor;
1320 FConnectedRigidBody.Parent.Add(FConnectedAnchorTransform);
1321 InternalCastleDesignInvalidate := true;
1322 end;
1323 end;
1324
1325 procedure TCastleDistanceJoint.InternalDestroyGizmos;
1326 begin
1327 FreeAndNil(FAnchorTransform);
1328 FreeAndNil(FConnectedAnchorTransform);
1329 InternalCastleDesignInvalidate := true;
1330 inherited;
1331 end;
1332
1333 function TCastleDistanceJoint.PropertySections(const PropertyName: String
1334 ): TPropertySections;
1335 begin
1336 if ArrayContainsString(PropertyName, [
1337 'AnchorPersistent', 'ConnectedAnchorPersistent', 'DampingRatio',
1338 'FrequencyHz'
1339 ]) then
1340 Result := [psBasic]
1341 else
1342 Result := inherited PropertySections(PropertyName);
1343 end;
1344
1345 function TCastleDistanceJoint.GetAnchorForTool: TVector3;
1346 begin
1347 Result := Anchor;
1348 end;
1349
1350 procedure TCastleDistanceJoint.SetAnchor(const AValue: TVector3);
1351 begin
1352 if not TVector3.PerfectlyEquals(FAnchor, AValue) then
1353 begin
1354 FAnchor := AValue;
1355 if FAnchorTransform <> nil then
1356 TCastleJointTool(FAnchorTransform).Value := AValue;
1357 RecreateKraftJoint;
1358 end;
1359 end;
1360
1361 function TCastleDistanceJoint.GetConnectedAnchorForTool: TVector3;
1362 begin
1363 Result := ConnectedAnchor;
1364 end;
1365
1366 procedure TCastleDistanceJoint.SetConnectedAnchor(const AValue: TVector3);
1367 begin
1368 if not TVector3.PerfectlyEquals(FConnectedAnchor, AValue) then
1369 begin
1370 FConnectedAnchor := AValue;
1371 if FConnectedAnchorTransform <> nil then
1372 TCastleJointTool(FConnectedAnchorTransform).Value := AValue;
1373 RecreateKraftJoint;
1374 end;
1375 end;
1376
1377 procedure TCastleDistanceJoint.SetFrequencyHz(const AValue: Single);
1378 begin
1379 if not SameValue(FFrequencyHz, AValue) then
1380 begin
1381 FFrequencyHz := AValue;
1382 RecreateKraftJoint;
1383 end;
1384 end;
1385
1386 procedure TCastleDistanceJoint.SetDampingRatio(const AValue: Single);
1387 begin
1388 if not SameValue(FDampingRatio, AValue) then
1389 begin
1390 FDampingRatio := AValue;
1391 RecreateKraftJoint;
1392 end;
1393 end;
1394
1395 procedure TCastleDistanceJoint.InternalUpdateGizmosName;
1396 begin
1397 if (FAnchorTransform <> nil) and (FAnchorTransform.Name <> Name + '_Anchor') then
1398 begin
1399 FAnchorTransform.Name := Name + '_Anchor';
1400 InternalCastleDesignInvalidate := true;
1401 end;
1402
1403 if (FConnectedAnchorTransform <> nil) and
1404 (FConnectedAnchorTransform.Name <> Name + '_ConnectedAnchor') then
1405 begin
1406 FConnectedAnchorTransform.Name := Name + '_ConnectedAnchor';
1407 InternalCastleDesignInvalidate := true;
1408 end;
1409 end;
1410
1411 function TCastleDistanceJoint.GetKraftJoint: TKraftConstraintJoint;
1412 begin
1413 Result := FKraftJointDistance;
1414 end;
1415
1416 procedure TCastleDistanceJoint.CreateKraftJoint;
1417 begin
1418 Assert(FKraftJointDistance = nil, 'Second joint initialization');
1419 FKraftJointDistance := TKraftConstraintJointDistance.Create(
1420 World.FKraftEngine,
1421 FParentRigidBody.FKraftBody,
1422 FConnectedRigidBody.FKraftBody,
1423 VectorToKraft(FAnchor),
1424 VectorToKraft(FConnectedAnchor),
1425 FFrequencyHz,
1426 FDampingRatio,
1427 ConnectedCollide
1428 );
1429 end;
1430
1431 procedure TCastleDistanceJoint.DeinitializeKraftJoint;
1432 begin
1433 FreeAndNil(FKraftJointDistance);
1434 end;
1435
1436 procedure TCastleDistanceJoint.SetName(const Value: TComponentName);
1437 begin
1438 inherited SetName(Value);
1439 InternalUpdateGizmosName;
1440 end;
1441
1442 {$define read_implementation_methods}
1443 {$I auto_generated_persistent_vectors/tcastledistancejoint_persistent_vectors.inc}
1444 {$undef read_implementation_methods}
1445
1446 { TCastleGrabJoint ----------------------------------------------------------------- }
1447
1448 constructor TCastleGrabJoint.Create(AOwner: TComponent);
1449 begin
1450 inherited Create(AOwner);
1451 ListenWorldChange := true;
1452 FTargetWorld := TVector3.Zero;
1453 FMaxForce := 0.0;
1454 FFrequencyHz := DefaultFrequencyHz;
1455 FDampingRatio := DefaultDampingRatio;
1456 {$define read_implementation_constructor}
1457 {$I auto_generated_persistent_vectors/tcastlegrabjoint_persistent_vectors.inc}
1458 {$undef read_implementation_constructor}
1459 end;
1460
1461 destructor TCastleGrabJoint.Destroy;
1462 begin
1463 {$define read_implementation_destructor}
1464 {$I auto_generated_persistent_vectors/tcastlegrabjoint_persistent_vectors.inc}
1465 {$undef read_implementation_destructor}
1466
1467 inherited Destroy;
1468 end;
1469
1470 procedure TCastleGrabJoint.WorldBeforeDetach;
1471 begin
1472 FreeAndNil(FAnchorTransform);
1473 FreeAndNil(FTargetWorldTransform);
1474 inherited WorldBeforeDetach;
1475 end;
1476
1477 procedure TCastleGrabJoint.InternalCreateGizmos;
1478 begin
1479 inherited;
1480 if FAnchorTransform = nil then
1481 begin
1482 FAnchorTransform := TCastleJointTool.Create(nil);
1483 TCastleJointTool(FAnchorTransform).Joint := Self;
1484 TCastleJointTool(FAnchorTransform).OnGetValue := {$ifdef FPC}@{$endif} GetAnchorForTool;
1485 TCastleJointTool(FAnchorTransform).OnSetValue := {$ifdef FPC}@{$endif} SetAnchor;
1486 InternalUpdateGizmosName;
1487 Parent.Add(FAnchorTransform);
1488 FAnchorTransform.Translation := Anchor;
1489 InternalCastleDesignInvalidate := true;
1490 end;
1491
1492 if FTargetWorldTransform = nil then
1493 begin
1494 FTargetWorldTransform := TCastleJointTool.Create(nil);
1495 TCastleJointTool(FTargetWorldTransform).Joint := Self;
1496 TCastleJointTool(FTargetWorldTransform).OnGetValue := {$ifdef FPC}@{$endif} GetTargetWorldForTool;
1497 TCastleJointTool(FTargetWorldTransform).OnSetValue := {$ifdef FPC}@{$endif} SetTargetWorld;
1498 TCastleJointTool(FTargetWorldTransform).Color := Blue;
1499 InternalUpdateGizmosName;
1500 Parent.World.Add(FTargetWorldTransform);
1501 FTargetWorldTransform.Translation := TargetWorld;
1502 InternalCastleDesignInvalidate := true;
1503 end;
1504 end;
1505
1506 procedure TCastleGrabJoint.InternalDestroyGizmos;
1507 begin
1508 FreeAndNil(FAnchorTransform);
1509 FreeAndNil(FTargetWorldTransform);
1510 InternalCastleDesignInvalidate := true;
1511 inherited;
1512 end;
1513
1514 function TCastleGrabJoint.PropertySections(const PropertyName: String
1515 ): TPropertySections;
1516 begin
1517 if ArrayContainsString(PropertyName, [
1518 'TargetWorldPersistent', 'AnchorPersistent',
1519 'DampingRatio', 'FrequencyHz', 'MaxForce'
1520 ]) then
1521 Result := [psBasic]
1522 else
1523 Result := inherited PropertySections(PropertyName);
1524 end;
1525
1526 function TCastleGrabJoint.GetTargetWorldForTool: TVector3;
1527 begin
1528 Result := TargetWorld;
1529 end;
1530
1531 procedure TCastleGrabJoint.SetTargetWorld(const AValue: TVector3);
1532 begin
1533 if not TVector3.PerfectlyEquals(FTargetWorld, AValue) then
1534 begin
1535 FTargetWorld := AValue;
1536
1537 if FTargetWorldTransform <> nil then
1538 TCastleJointTool(FTargetWorldTransform).Value := FTargetWorld;
1539
1540 if FKraftJointGrab <> nil then
1541 begin
1542 FKraftJointGrab.SetWorldPoint(VectorToKraft(FTargetWorld));
1543 // Kraft needs us to wake rigid body to make body start moving
1544 // https://github.com/BeRo1985/kraft/issues/26
1545 FParentRigidBody.WakeUp;
1546 end;
1547 end;
1548 end;
1549
1550 function TCastleGrabJoint.GetAnchorForTool: TVector3;
1551 begin
1552 Result := Anchor;
1553 end;
1554
1555 procedure TCastleGrabJoint.SetAnchor(const AValue: TVector3);
1556 begin
1557 if not TVector3.PerfectlyEquals(FAnchor, AValue) then
1558 begin
1559 FAnchor := AValue;
1560 if FAnchorTransform <> nil then
1561 TCastleJointTool(FAnchorTransform).Value := AValue;
1562 RecreateKraftJoint;
1563 end;
1564 end;
1565
1566 procedure TCastleGrabJoint.SetFrequencyHz(const AValue: Single);
1567 begin
1568 if FFrequencyHz <> AValue then
1569 begin
1570 FFrequencyHz := AValue;
1571 RecreateKraftJoint;
1572 end;
1573 end;
1574
1575 procedure TCastleGrabJoint.SetDampingRatio(const AValue: Single);
1576 begin
1577 if FDampingRatio <> AValue then
1578 begin
1579 FDampingRatio := AValue;
1580 RecreateKraftJoint;
1581 end;
1582 end;
1583
1584 function TCastleGrabJoint.MaxForceNonZero: Single;
1585 begin
1586 if MaxForce = 0 then
1587 Result := MaxSingle
1588 else
1589 Result := MaxForce;
1590 end;
1591
1592 procedure TCastleGrabJoint.SetMaxForce(const AValue: Single);
1593 begin
1594 if FMaxForce <> AValue then
1595 begin
1596 FMaxForce := AValue;
1597 if FKraftJointGrab <> nil then
1598 FKraftJointGrab.SetMaximalForce(MaxForceNonZero);
1599 end;
1600 end;
1601
1602 procedure TCastleGrabJoint.InternalUpdateGizmosName;
1603 begin
1604 if (FAnchorTransform <> nil) and (FAnchorTransform.Name <> Name + '_Anchor') then
1605 begin
1606 FAnchorTransform.Name := Name + '_Anchor';
1607 InternalCastleDesignInvalidate := true;
1608 end;
1609
1610 if (FTargetWorldTransform <> nil) and
1611 (FTargetWorldTransform.Name <> Name + '_TargetWorld') then
1612 begin
1613 FTargetWorldTransform.Name := Name + '_TargetWorld';
1614 InternalCastleDesignInvalidate := true;
1615 end;
1616 end;
1617
1618 function TCastleGrabJoint.GetKraftJoint: TKraftConstraintJoint;
1619 begin
1620 Result := FKraftJointGrab;
1621 end;
1622
1623 procedure TCastleGrabJoint.CreateKraftJoint;
1624 var
1625 AnchorInWorldCoords: TKraftVector3;
1626 begin
1627 Assert(FKraftJointGrab = nil, 'Second joint initialization');
1628
1629 { TKraftConstraintJointGrab.Create takes AWorldPoint as argument,
1630 and implicitly sets fLocalAnchor to
1631
1632 fLocalAnchor:=Vector3TermMatrixMulInverted(AWorldPoint,ARigidBody.fWorldTransform);
1633
1634 IOW, it converts AWorldPoint into local coordinate space,
1635 so that initial state means that TKraftConstraintJointGrab doesn't have to do anything
1636 -- target is at anchor.
1637
1638 We want a different behavior,
1639 - Anchor (in local coords) to be explicitly configurable, like other joints
1640 - TargetWorld configurable, and not necessarily "nothing to do" initially
1641
1642 So we kind of "trick" Kraft into it:
1643 - as AWorldPoint argument, we pass AnchorInWorldCoords
1644 - later we call SetWorldPoint to set real desired TargetWorld
1645 }
1646
1647 AnchorInWorldCoords := Vector3TermMatrixMul(
1648 VectorToKraft(FAnchor),
1649 FParentRigidBody.FKraftBody.WorldTransform);
1650
1651 FKraftJointGrab := TKraftConstraintJointGrab.Create(
1652 World.FKraftEngine,
1653 FParentRigidBody.FKraftBody,
1654 AnchorInWorldCoords,
1655 FFrequencyHz,
1656 FDampingRatio,
1657 MaxForceNonZero
1658 );
1659 FKraftJointGrab.SetWorldPoint(VectorToKraft(FTargetWorld));
1660 end;
1661
1662 procedure TCastleGrabJoint.DeinitializeKraftJoint;
1663 begin
1664 FreeAndNil(FKraftJointGrab);
1665 end;
1666
1667 procedure TCastleGrabJoint.SetName(const Value: TComponentName);
1668 begin
1669 inherited SetName(Value);
1670 InternalUpdateGizmosName;
1671 end;
1672
1673 {$define read_implementation_methods}
1674 {$I auto_generated_persistent_vectors/tcastlegrabjoint_persistent_vectors.inc}
1675 {$undef read_implementation_methods}
1676
1677 {$endif read_implementation}