| 1 |
{ |
| 2 |
Copyright 2022-2023 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 |
CastleVectors, CastleComponentSerialize, |
| 23 |
CastleUIControls, CastleControls, CastleKeysMouse, CastleScene, CastleTransform, |
| 24 |
X3DNodes; |
| 25 |
|
| 26 |
type |
| 27 |
{ Main view, where most of the application logic takes place. } |
| 28 |
TViewMain = class(TCastleView) |
| 29 |
published |
| 30 |
{ Components designed using CGE editor. |
| 31 |
These fields will be automatically initialized at Start. } |
| 32 |
LabelFps, |
| 33 |
LabelAddForceAtPosition, |
| 34 |
LabelAddForce, |
| 35 |
LabelAddTorque, |
| 36 |
LabelApplyImpulse: TCastleLabel; |
| 37 |
SceneArrow: TCastleScene; |
| 38 |
DynamicBodies: TCastleTransform; |
| 39 |
SceneVisualizeVelocities: TCastleScene; |
| 40 |
CheckboxVisualizeVelocities: TCastleCheckbox; |
| 41 |
private |
| 42 |
RigidBodies: TCastleRigidBodyList; |
| 43 |
VisualizeVelocitiesAngularLines: TCoordinateNode; |
| 44 |
VisualizeVelocitiesLinearLines: TCoordinateNode; |
| 45 |
procedure AddForceAtPosition; |
| 46 |
procedure AddForce; |
| 47 |
procedure AddTorque; |
| 48 |
procedure ApplyImpulse; |
| 49 |
function ForceScale: Single; |
| 50 |
procedure CreateVisualizeVelocitiesNodes; |
| 51 |
procedure UpdateVisualizeVelocities; |
| 52 |
procedure ChangeVisualizeVelocities(Sender: TObject); |
| 53 |
public |
| 54 |
constructor Create(AOwner: TComponent); override; |
| 55 |
procedure Start; override; |
| 56 |
procedure Stop; override; |
| 57 |
procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override; |
| 58 |
function Press(const Event: TInputPressRelease): Boolean; override; |
| 59 |
end; |
| 60 |
|
| 61 |
var |
| 62 |
ViewMain: TViewMain; |
| 63 |
|
| 64 |
implementation |
| 65 |
|
| 66 |
uses SysUtils, Math, |
| 67 |
CastleColors, CastleUtils; |
| 68 |
|
| 69 |
{ TViewMain ----------------------------------------------------------------- } |
| 70 |
|
| 71 |
constructor TViewMain.Create(AOwner: TComponent); |
| 72 |
begin |
| 73 |
inherited; |
| 74 |
DesignUrl := 'castle-data:/gameviewmain.castle-user-interface'; |
| 75 |
end; |
| 76 |
|
| 77 |
procedure TViewMain.Start; |
| 78 |
var |
| 79 |
T: TCastleTransform; |
| 80 |
RBody: TCastleRigidBody; |
| 81 |
begin |
| 82 |
inherited; |
| 83 |
|
| 84 |
RigidBodies := TCastleRigidBodyList.Create; |
| 85 |
for T in DynamicBodies do |
| 86 |
begin |
| 87 |
RBody := T.FindBehavior(TCastleRigidBody) as TCastleRigidBody; |
| 88 |
if RBody <> nil then |
| 89 |
RigidBodies.Add(RBody); |
| 90 |
end; |
| 91 |
|
| 92 |
CreateVisualizeVelocitiesNodes; |
| 93 |
CheckboxVisualizeVelocities.OnChange := {$ifdef FPC}@{$endif} ChangeVisualizeVelocities; |
| 94 |
end; |
| 95 |
|
| 96 |
procedure TViewMain.Stop; |
| 97 |
begin |
| 98 |
FreeAndNil(RigidBodies); |
| 99 |
inherited; |
| 100 |
end; |
| 101 |
|
| 102 |
procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean); |
| 103 |
|
| 104 |
procedure ColorLabel(const Lab: TCastleLabel; const Active: Boolean); |
| 105 |
begin |
| 106 |
if Active then |
| 107 |
Lab.Color := Blue |
| 108 |
else |
| 109 |
Lab.Color := White; |
| 110 |
end; |
| 111 |
|
| 112 |
const |
| 113 |
MoveSpeed = 10; |
| 114 |
ScaleIncrease = 2; |
| 115 |
RotationSpeed = 10; |
| 116 |
begin |
| 117 |
inherited; |
| 118 |
{ This virtual method is executed every frame (many times per second). } |
| 119 |
LabelFps.Caption := 'FPS: ' + Container.Fps.ToString; |
| 120 |
|
| 121 |
{ Transform SceneArrow by keys } |
| 122 |
|
| 123 |
if Container.Pressed[keyW] then |
| 124 |
SceneArrow.Translation := SceneArrow.Translation + Vector3(0, 0, -1) * SecondsPassed * MoveSpeed; |
| 125 |
if Container.Pressed[keyS] then |
| 126 |
SceneArrow.Translation := SceneArrow.Translation + Vector3(0, 0, 1) * SecondsPassed * MoveSpeed; |
| 127 |
if Container.Pressed[keyA] then |
| 128 |
SceneArrow.Translation := SceneArrow.Translation + Vector3(-1, 0, 0) * SecondsPassed * MoveSpeed; |
| 129 |
if Container.Pressed[keyD] then |
| 130 |
SceneArrow.Translation := SceneArrow.Translation + Vector3( 1, 0, 0) * SecondsPassed * MoveSpeed; |
| 131 |
|
| 132 |
if Container.Pressed[keyQ] then |
| 133 |
SceneArrow.Scale := SceneArrow.Scale * Vector3(1, 1, Power(ScaleIncrease, SecondsPassed)); |
| 134 |
if Container.Pressed[keyE] then |
| 135 |
SceneArrow.Scale := SceneArrow.Scale * Vector3(1, 1, Power(1 / ScaleIncrease, SecondsPassed)); |
| 136 |
|
| 137 |
if Container.Pressed[keyZ] then |
| 138 |
SceneArrow.Rotation := Vector4(0, 1, 0, SceneArrow.Rotation.W + SecondsPassed * RotationSpeed); |
| 139 |
if Container.Pressed[keyC] then |
| 140 |
SceneArrow.Rotation := Vector4(0, 1, 0, SceneArrow.Rotation.W - SecondsPassed * RotationSpeed); |
| 141 |
|
| 142 |
if Container.Pressed[key7] then |
| 143 |
AddForceAtPosition; |
| 144 |
if Container.Pressed[key8] then |
| 145 |
AddForce; |
| 146 |
if Container.Pressed[key9] then |
| 147 |
AddTorque; |
| 148 |
|
| 149 |
ColorLabel(LabelAddForceAtPosition, Container.Pressed[key7]); |
| 150 |
ColorLabel(LabelAddForce, Container.Pressed[key8]); |
| 151 |
ColorLabel(LabelAddTorque, Container.Pressed[key9]); |
| 152 |
ColorLabel(LabelApplyImpulse, Container.Pressed[key0]); |
| 153 |
|
| 154 |
if SceneVisualizeVelocities.Exists then |
| 155 |
UpdateVisualizeVelocities; |
| 156 |
end; |
| 157 |
|
| 158 |
function TViewMain.Press(const Event: TInputPressRelease): Boolean; |
| 159 |
begin |
| 160 |
Result := inherited; |
| 161 |
if Result then Exit; // allow the ancestor to handle keys |
| 162 |
|
| 163 |
if Event.IsKey(key0) then |
| 164 |
begin |
| 165 |
ApplyImpulse; |
| 166 |
Exit(true); |
| 167 |
end; |
| 168 |
end; |
| 169 |
|
| 170 |
function TViewMain.ForceScale: Single; |
| 171 |
begin |
| 172 |
Result := SceneArrow.Scale.Z * 1; |
| 173 |
end; |
| 174 |
|
| 175 |
procedure TViewMain.AddForceAtPosition; |
| 176 |
var |
| 177 |
RBody: TCastleRigidBody; |
| 178 |
begin |
| 179 |
for RBody in RigidBodies do |
| 180 |
RBody.AddForceAtPosition(SceneArrow.Direction * ForceScale, SceneArrow.Translation); |
| 181 |
end; |
| 182 |
|
| 183 |
procedure TViewMain.AddForce; |
| 184 |
var |
| 185 |
RBody: TCastleRigidBody; |
| 186 |
begin |
| 187 |
for RBody in RigidBodies do |
| 188 |
RBody.AddForce(SceneArrow.Direction * ForceScale, false); |
| 189 |
end; |
| 190 |
|
| 191 |
procedure TViewMain.AddTorque; |
| 192 |
var |
| 193 |
RBody: TCastleRigidBody; |
| 194 |
begin |
| 195 |
for RBody in RigidBodies do |
| 196 |
RBody.AddTorque(SceneArrow.Direction * ForceScale); |
| 197 |
end; |
| 198 |
|
| 199 |
procedure TViewMain.ApplyImpulse; |
| 200 |
var |
| 201 |
RBody: TCastleRigidBody; |
| 202 |
begin |
| 203 |
for RBody in RigidBodies do |
| 204 |
RBody.ApplyImpulse(SceneArrow.Direction * ForceScale, SceneArrow.Translation); |
| 205 |
end; |
| 206 |
|
| 207 |
procedure TViewMain.ChangeVisualizeVelocities(Sender: TObject); |
| 208 |
begin |
| 209 |
SceneVisualizeVelocities.Exists := CheckboxVisualizeVelocities.Checked; |
| 210 |
end; |
| 211 |
|
| 212 |
procedure TViewMain.CreateVisualizeVelocitiesNodes; |
| 213 |
var |
| 214 |
LineSet: TLineSetNode; |
| 215 |
Appearance: TAppearanceNode; |
| 216 |
Material: TUnlitMaterialNode; |
| 217 |
Shape: TShapeNode; |
| 218 |
RootNode: TX3DRootNode; |
| 219 |
begin |
| 220 |
RootNode := TX3DRootNode.Create; |
| 221 |
|
| 222 |
{ create visualization for angular velocities } |
| 223 |
|
| 224 |
Material := TUnlitMaterialNode.Create; |
| 225 |
Material.EmissiveColor := YellowRGB; |
| 226 |
|
| 227 |
Appearance := TAppearanceNode.Create; |
| 228 |
Appearance.Material := Material; |
| 229 |
|
| 230 |
LineSet := TLineSetNode.CreateWithShape(Shape); |
| 231 |
LineSet.Mode := lmPair; // each pair of vertexes on LineSet.Coord forms a line segment |
| 232 |
Shape.Appearance := Appearance; |
| 233 |
RootNode.AddChildren(Shape); |
| 234 |
|
| 235 |
VisualizeVelocitiesAngularLines := TCoordinateNode.Create; |
| 236 |
LineSet.Coord := VisualizeVelocitiesAngularLines; |
| 237 |
|
| 238 |
{ create visualization for linear velocities } |
| 239 |
|
| 240 |
Material := TUnlitMaterialNode.Create; |
| 241 |
Material.EmissiveColor := RedRGB; |
| 242 |
|
| 243 |
Appearance := TAppearanceNode.Create; |
| 244 |
Appearance.Material := Material; |
| 245 |
|
| 246 |
LineSet := TLineSetNode.CreateWithShape(Shape); |
| 247 |
LineSet.Mode := lmPair; // each pair of vertexes on LineSet.Coord forms a line segment |
| 248 |
Shape.Appearance := Appearance; |
| 249 |
RootNode.AddChildren(Shape); |
| 250 |
|
| 251 |
VisualizeVelocitiesLinearLines := TCoordinateNode.Create; |
| 252 |
LineSet.Coord := VisualizeVelocitiesLinearLines; |
| 253 |
|
| 254 |
UpdateVisualizeVelocities; |
| 255 |
|
| 256 |
SceneVisualizeVelocities.Load(RootNode, true); |
| 257 |
SceneVisualizeVelocities.Exists := CheckboxVisualizeVelocities.Checked; |
| 258 |
end; |
| 259 |
|
| 260 |
procedure TViewMain.UpdateVisualizeVelocities; |
| 261 |
|
| 262 |
{ Make the direction have some minimal and maximum length, to be nicely visible. } |
| 263 |
function VelocityScale(const Dir: TVector3): TVector3; |
| 264 |
var |
| 265 |
L: Single; |
| 266 |
begin |
| 267 |
L := Dir.Length; |
| 268 |
if IsZero(L) then |
| 269 |
Exit(Dir); |
| 270 |
|
| 271 |
L := MapRangeClamped(L, 0, 10, 2, 5); |
| 272 |
Result := Dir.AdjustToLength(L); |
| 273 |
end; |
| 274 |
|
| 275 |
var |
| 276 |
VertexesLinearVelocity, VertexesAngularVelocity: TVector3List; |
| 277 |
I: Integer; |
| 278 |
RBody: TCastleRigidBody; |
| 279 |
Origin: TVector3; |
| 280 |
begin |
| 281 |
VertexesLinearVelocity := VisualizeVelocitiesLinearLines.FdPoint.Items; |
| 282 |
VertexesAngularVelocity := VisualizeVelocitiesAngularLines.FdPoint.Items; |
| 283 |
|
| 284 |
VertexesLinearVelocity.Count := 2 * RigidBodies.Count; |
| 285 |
VertexesAngularVelocity.Count := 2 * RigidBodies.Count; |
| 286 |
|
| 287 |
for I := 0 to RigidBodies.Count - 1 do |
| 288 |
begin |
| 289 |
RBody := RigidBodies[I]; |
| 290 |
Origin := RBody.Parent.LocalToWorld(TVector3.Zero); |
| 291 |
VertexesLinearVelocity.L[I * 2 ] := Origin; |
| 292 |
VertexesLinearVelocity.L[I * 2 + 1] := Origin + VelocityScale(RBody.LinearVelocity); |
| 293 |
VertexesAngularVelocity.L[I * 2 ] := Origin; |
| 294 |
VertexesAngularVelocity.L[I * 2 + 1] := Origin + VelocityScale(RBody.AngularVelocity); |
| 295 |
end; |
| 296 |
|
| 297 |
VisualizeVelocitiesLinearLines.FdPoint.Changed; |
| 298 |
VisualizeVelocitiesAngularLines.FdPoint.Changed; |
| 299 |
end; |
| 300 |
|
| 301 |
end. |