Debian Codegraph @

src/transform/castleinternalphysicsvisualization.pas

raw
1 {
2 Copyright 2022-2022 Andrzej Kilijański, Michalis Kamburelis.
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 { Visualization of physics stuff. }
17 unit CastleInternalPhysicsVisualization;
18
19 {$I castleconf.inc}
20
21 interface
22
23 uses
24 Classes, SysUtils, Math,
25 CastleTransform, CastleColors, CastleRenderOptions,
26 CastleVectors, CastleScene, CastleClassUtils, CastleUtils;
27
28 type
29 TOnGetValue = function: TVector3 of object;
30 TOnSetValue = procedure (const Value: TVector3) of object;
31
32 { Ancestor for design-time tools created by behaviors to manipulate e.g. joints. }
33 TCastleToolTransform = class(TCastleTransform);
34
35 { Ancestor for design-time tools created by physics joints. }
36 TCastleJointTool = class(TCastleToolTransform)
37 strict private
38 FColor: TCastleColor;
39 FJoint: TCastleAbstractJoint;
40 procedure SetColor(const Value: TCastleColor);
41 protected
42 FSphere: TCastleSphere;
43
44 // For now it's actually more natural to just *not* adjust sphere size
45 //function EstimateSphereRadius: Single;
46
47 function GetValue: TVector3;
48 procedure SetValue(const AValue: TVector3);
49 procedure ChangedTransform; override;
50 public
51 OnGetValue: TOnGetValue;
52 OnSetValue: TOnSetValue;
53
54 constructor Create(AOwner: TComponent); override;
55 destructor Destroy; override;
56
57 procedure Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); override;
58 function PropertySections(const PropertyName: String): TPropertySections; override;
59
60 property Value: TVector3 read GetValue write SetValue;
61 property Joint: TCastleAbstractJoint read FJoint write FJoint;
62 property Color: TCastleColor read FColor write SetColor;
63 end;
64
65 const
66 ConnectedAnchorColor: TCastleColor = (X: 0.0; Y: 1.0; Z: 0.0; W: 1.0); // = Green;
67
68 implementation
69
70 uses CastleRenderContext;
71
72 { TCastleJointTool --------------------------------------------------- }
73
74 constructor TCastleJointTool.Create(AOwner: TComponent);
75 begin
76 inherited Create(AOwner);
77
78 FColor := Red;
79 SetTransient;
80
81 FSphere := TCastleSphere.Create(nil);
82 FSphere.RenderLayer := rlFront;
83 FSphere.SetTransient;
84 FSphere.Color := FColor;
85 FSphere.Material := pmUnlit;
86 FSphere.Radius := 0.1;
87 Add(FSphere);
88 end;
89
90 destructor TCastleJointTool.Destroy;
91 begin
92 FreeAndNil(FSphere);
93 inherited Destroy;
94 end;
95
96 procedure TCastleJointTool.Update(const SecondsPassed: Single;
97 var RemoveMe: TRemoveType);
98 // var
99 // NewRadius: Single;
100 begin
101 inherited Update(SecondsPassed, RemoveMe);
102
103 (*
104 if FSphere <> nil then
105 begin
106 NewRadius := EstimateSphereRadius;
107 if not SameValue(NewRadius, FSphere.Radius) then
108 begin
109 FSphere.Radius := NewRadius;
110 end;
111 end;
112 *)
113 end;
114
115 function TCastleJointTool.PropertySections(const PropertyName: String
116 ): TPropertySections;
117 begin
118 if ArrayContainsString(PropertyName, [
119 'TranslationPersistent'
120 ]) then
121 Result := [psBasic, psLayout]
122 else
123 Result := [];
124 end;
125
126 procedure TCastleJointTool.SetColor(const Value: TCastleColor);
127 begin
128 if not TCastleColor.PerfectlyEquals(FColor, Value) then
129 begin
130 FColor := Value;
131 if FSphere <> nil then
132 FSphere.Color := FColor;
133 end;
134 end;
135
136 (*
137 function TCastleJointTool.EstimateSphereRadius: Single;
138 var
139 Viewport: TCastleViewport;
140 SphereWorldPos1, SphereWorldPos2: TVector3;
141 SphereViewportPos1, SphereViewportPos2: TVector2;
142 DistanceOnViewport: Single;
143 begin
144 if (Parent <> nil) and (Parent.World <> nil) and (Parent.World.Owner <> nil) and
145 (Parent.World.Owner is TCastleViewport) then
146 begin
147 Viewport := Parent.World.Owner as TCastleViewport;
148 SphereWorldPos1 := FSphere.LocalToWorld(TVector3.Zero);
149 SphereViewportPos1 := Viewport.PositionFromWorld(SphereWorldPos1);
150
151 SphereWorldPos2 := SphereWorldPos1 + CastleVectors.Vector3(0, 1, 0);
152 SphereViewportPos2 := Viewport.PositionFromWorld(SphereWorldPos2);
153 DistanceOnViewport := PointsDistance(SphereViewportPos1, SphereViewportPos2);
154
155 { Make size about 10 pixels }
156 Result := 10 / DistanceOnViewport;
157 end else
158 Result := 1;
159 end;
160 *)
161
162 function TCastleJointTool.GetValue: TVector3;
163 begin
164 Assert(Assigned(OnGetValue));
165 Result := OnGetValue();
166 end;
167
168 procedure TCastleJointTool.SetValue(const AValue: TVector3);
169 begin
170 Assert(Assigned(OnSetValue));
171 OnSetValue(AValue);
172 if not TVector3.PerfectlyEquals(Translation, AValue) then
173 Translation := AValue;
174 end;
175
176 procedure TCastleJointTool.ChangedTransform;
177 begin
178 inherited;
179 if not TVector3.PerfectlyEquals(Translation, Value) then
180 Value := Translation;
181 end;
182
183 end.