Debian Codegraph @

examples/viewport_and_scenes/collisions/code/gameviewmain.pas

raw
1 {
2 Copyright 2020-2022 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 { Main view, where most of the application logic takes place. }
17 unit GameViewMain;
18
19 interface
20
21 uses Classes,
22 CastleComponentSerialize, CastleUIControls, CastleControls,
23 CastleKeysMouse, CastleScene, CastleTransform, CastleViewport;
24
25 type
26 { Main view, where most of the application logic takes place. }
27 TViewMain = class(TCastleView)
28 published
29 { Components designed using CGE editor.
30 These fields will be automatically initialized at Start. }
31 LabelFps: TCastleLabel;
32 TransformMoving: TCastleTransform;
33 SceneMovingBox: TCastleScene;
34 SceneMovingSphere: TCastleScene;
35 SceneMovingRay: TCastleScene;
36 ButtonTestMove: TCastleButton;
37 ButtonTestBox: TCastleButton;
38 ButtonTestSphere: TCastleButton;
39 ButtonTestRay: TCastleButton;
40 ButtonTestPhysicsRay: TCastleButton;
41 MainViewport: TCastleViewport;
42 private
43 type
44 TTestMode = (tmMove, tmBox, tmSphere, tmRay, tmPhysicsRay);
45 var
46 FTestMode: TTestMode;
47 procedure SetTestMode(const Value: TTestMode);
48 { Update SceneMovingXxx material color, to show whether it collides,
49 following current FTestMode. }
50 procedure UpdateCollision;
51 procedure ClickTestMove(Sender: TObject);
52 procedure ClickTestBox(Sender: TObject);
53 procedure ClickTestSphere(Sender: TObject);
54 procedure ClickTestRay(Sender: TObject);
55 procedure ClickTestPhysicsRay(Sender: TObject);
56 public
57 constructor Create(AOwner: TComponent); override;
58 procedure Start; override;
59 procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override;
60 function Press(const Event: TInputPressRelease): Boolean; override;
61 end;
62
63 var
64 ViewMain: TViewMain;
65
66 implementation
67
68 uses SysUtils,
69 X3DNodes, CastleVectors, CastleBoxes;
70
71 { TViewMain ----------------------------------------------------------------- }
72
73 constructor TViewMain.Create(AOwner: TComponent);
74 begin
75 inherited;
76 DesignUrl := 'castle-data:/gameviewmain.castle-user-interface';
77 end;
78
79 procedure TViewMain.Start;
80 begin
81 inherited;
82
83 ButtonTestMove.OnClick := {$ifdef FPC}@{$endif} ClickTestMove;
84 ButtonTestBox.OnClick := {$ifdef FPC}@{$endif} ClickTestBox;
85 ButtonTestSphere.OnClick := {$ifdef FPC}@{$endif} ClickTestSphere;
86 ButtonTestRay.OnClick := {$ifdef FPC}@{$endif} ClickTestRay;
87 ButtonTestPhysicsRay.OnClick := {$ifdef FPC}@{$endif} ClickTestPhysicsRay;
88
89 SetTestMode(tmMove);
90 end;
91
92 procedure TViewMain.SetTestMode(const Value: TTestMode);
93 begin
94 FTestMode := Value;
95
96 ButtonTestMove.Pressed := FTestMode = tmMove;
97 ButtonTestBox.Pressed := FTestMode = tmBox;
98 ButtonTestSphere.Pressed := FTestMode = tmSphere;
99 ButtonTestRay.Pressed := FTestMode = tmRay;
100 ButtonTestPhysicsRay.Pressed := FTestMode = tmPhysicsRay;
101
102 SceneMovingBox.Exists := FTestMode = tmBox;
103 SceneMovingSphere.Exists := FTestMode in [tmMove, tmSphere];
104 SceneMovingRay.Exists := FTestMode in [tmRay, tmPhysicsRay];
105
106 UpdateCollision;
107 end;
108
109 procedure TViewMain.UpdateCollision;
110
111 procedure ShowCollision(const Collides: Boolean);
112 var
113 Appearance: TAppearanceNode;
114 Mat: TPhysicalMaterialNode;
115 begin
116 case FTestMode of
117 tmMove, tmSphere:
118 Appearance := SceneMovingSphere.Node('MainMaterial') as TAppearanceNode;
119 tmBox:
120 Appearance := SceneMovingBox.Node('MainMaterial') as TAppearanceNode;
121 tmRay, tmPhysicsRay:
122 Appearance := SceneMovingRay.Node('MainMaterial') as TAppearanceNode;
123 end;
124 { Here we simply assume that material is TPhysicalMaterialNode, which means it is a PBR
125 material as e.g. designed in Blender and exported to glTF. }
126 Mat := Appearance.Material as TPhysicalMaterialNode;
127 if Collides then
128 Mat.BaseColor := Vector3(0.9, 0.1, 0.1) // reddish
129 else
130 Mat.BaseColor := Vector3(0.9, 0.9, 0.9); // almost white
131 end;
132
133 var
134 Box: TBox3D;
135 SavedExists: Boolean;
136 begin
137 { Make TransformMoving temporarily non-existing,
138 so that WorldXxxCollision call doesn't detect collisions with TransformMoving
139 (so it only detects them with SceneLevel). }
140 SavedExists := TransformMoving.Exists;
141 TransformMoving.Exists := false;
142
143 case FTestMode of
144 tmMove, tmSphere:
145 begin
146 ShowCollision(MainViewport.Items.WorldSphereCollision(
147 TransformMoving.Translation, 0.5));
148 end;
149 tmBox:
150 begin
151 Box := Box3D(
152 Vector3(-0.5, -0.5, -0.5),
153 Vector3( 0.5, 0.5, 0.5)
154 ).Translate(TransformMoving.Translation);
155 ShowCollision(MainViewport.Items.WorldBoxCollision(Box));
156 end;
157 tmRay:
158 begin
159 ShowCollision(MainViewport.Items.WorldRayCast(
160 TransformMoving.Translation, Vector3(0, 0, -1)) <> nil);
161 end;
162 tmPhysicsRay:
163 begin
164 ShowCollision(MainViewport.Items.PhysicsRayCast(
165 TransformMoving.Translation, Vector3(0, 0, -1)).Transform <> nil);
166 end;
167 end;
168
169 TransformMoving.Exists := SavedExists;
170 end;
171
172 procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean);
173 const
174 MoveSpeed = 4.0;
175 var
176 MoveVector: TVector3;
177 begin
178 inherited;
179 { This virtual method is executed every frame (many times per second). }
180 LabelFps.Caption := 'FPS: ' + Container.Fps.ToString;
181
182 MoveVector := TVector3.Zero;
183 if Container.Pressed.Keys[KeyA] then
184 MoveVector := MoveVector + Vector3(-1, 0, 0) * MoveSpeed * SecondsPassed;
185 if Container.Pressed.Keys[KeyD] then
186 MoveVector := MoveVector + Vector3( 1, 0, 0) * MoveSpeed * SecondsPassed;
187 if Container.Pressed.Keys[KeyW] then
188 MoveVector := MoveVector + Vector3(0, 0, -1) * MoveSpeed * SecondsPassed;
189 if Container.Pressed.Keys[KeyS] then
190 MoveVector := MoveVector + Vector3(0, 0, 1) * MoveSpeed * SecondsPassed;
191 if Container.Pressed.Keys[KeyE] then
192 MoveVector := MoveVector + Vector3(0, 1, 0) * MoveSpeed * SecondsPassed;
193 if Container.Pressed.Keys[KeyQ] then
194 MoveVector := MoveVector + Vector3(0, -1, 0) * MoveSpeed * SecondsPassed;
195
196 if not MoveVector.IsPerfectlyZero then
197 begin
198 if FTestMode = tmMove then
199 begin
200 // change TransformMoving.Translation only if possible
201 TransformMoving.Move(MoveVector, false)
202 end else
203 begin
204 // unconditionally change TransformMoving.Translation
205 TransformMoving.Translation := TransformMoving.Translation + MoveVector;
206 UpdateCollision;
207 end;
208 end;
209 end;
210
211 procedure TViewMain.ClickTestMove(Sender: TObject);
212 begin
213 SetTestMode(tmMove);
214 end;
215
216 procedure TViewMain.ClickTestBox(Sender: TObject);
217 begin
218 SetTestMode(tmBox);
219 end;
220
221 procedure TViewMain.ClickTestSphere(Sender: TObject);
222 begin
223 SetTestMode(tmSphere);
224 end;
225
226 procedure TViewMain.ClickTestRay(Sender: TObject);
227 begin
228 SetTestMode(tmRay);
229 end;
230
231 procedure TViewMain.ClickTestPhysicsRay(Sender: TObject);
232 begin
233 SetTestMode(tmPhysicsRay);
234 end;
235
236 function TViewMain.Press(const Event: TInputPressRelease): Boolean;
237 begin
238 Result := inherited;
239 if Result then Exit; // allow the ancestor to handle keys
240 end;
241
242 end.