-
Notifications
You must be signed in to change notification settings - Fork 0
/
Billiards.cpp
459 lines (370 loc) · 17.2 KB
/
Billiards.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//
// Copyright (c) 2008-2017 the Urho3D project.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#include <Urho3D/Core/CoreEvents.h>
#include <Urho3D/Engine/Engine.h>
#include <Urho3D/Graphics/Camera.h>
#include <Urho3D/Graphics/DebugRenderer.h>
#include <Urho3D/Graphics/Graphics.h>
#include <Urho3D/Graphics/Material.h>
#include <Urho3D/Graphics/Model.h>
#include <Urho3D/Graphics/Octree.h>
#include <Urho3D/Graphics/Renderer.h>
#include <Urho3D/Graphics/StaticModel.h>
#include <Urho3D/Input/Input.h>
#include <Urho3D/Resource/ResourceCache.h>
#include <Urho3D/Scene/Scene.h>
#include <Urho3D/Physics/CollisionShape.h>
#include <Urho3D/Physics/PhysicsWorld.h>
#include <Urho3D/Physics/RigidBody.h>
#include <Urho3D/IO/Log.h>
#include "Billiards.h"
#include "Table.h"
#include "Ball.h"
#include "WhiteBall.h"
#include "Interface.h"
#include <Urho3D/DebugNew.h>
#include "iostream"
URHO3D_DEFINE_APPLICATION_MAIN(Billiards)
const String BLACK_BALL_NAME = "BlackBall";
const float CAMERA_DISTANCE = 20.0f;
// Mouse sensitivity as degrees per pixel
const float MOUSE_SENSITIVITY = 0.1f;
const Vector3 INITIAL_WHITE_BALL_POSITION = Vector3(-10.0f, 7.5f, -1.0f);
const Vector3 CAMERA_INITIAL_POSITION = Vector3(-29.14f, 13.23f, -1.0f);
Billiards::Billiards(Context *context) :
Sample(context) {
Table::RegisterObject(context);
Ball::RegisterObject(context);
WhiteBall::RegisterObject(context);
Interface::RegisterObject(context);
}
// http://billiards.colostate.edu/threads/physics.html
void Billiards::Start() {
// Execute base class startup
Sample::Start();
// Create the scene content
CreateScene();
CreateTable();
// Create the white ball
CreateWhiteBall();
CreateBalls();
// Create the UI content
CreateInterface();
// Setup the viewport for displaying the scene
SetupViewport();
// Hook up to the frame update events
SubscribeToEvents();
// Set the mouse mode to use in the sample
Sample::InitMouseMode(MM_RELATIVE);
}
void Billiards::CreateScene() {
ResourceCache *cache = GetSubsystem<ResourceCache>();
// Create camera and define viewport. We will be doing load / save, so it's convenient to create the camera outside the scene,
// so that it won't be destroyed and recreated, and we don't have to redefine the viewport on load
cameraNode_ = new Node(context_);
cameraNode_->SetPosition(Vector3(-30.0f, 17.0f, -17.0f));
Camera *camera = cameraNode_->CreateComponent<Camera>();
camera->SetFarClip(500.0f);
GetSubsystem<Renderer>()->SetViewport(0, new Viewport(context_, scene_, camera));
scene_ = new Scene(context_);
// Create the Octree component to the scene. This is required before adding any drawable components, or else nothing will
// show up. The default octree volume will be from (-1000, -1000, -1000) to (1000, 1000, 1000) in world coordinates; it
// is also legal to place objects outside the volume but their visibility can then not be checked in a hierarchically
// optimizing manner
scene_->CreateComponent<Octree>();
scene_->CreateComponent<PhysicsWorld>();
scene_->CreateComponent<DebugRenderer>();
// Create a child scene node (at world origin) and a StaticModel component into it. Set the StaticModel to show a simple
// plane mesh with a "stone" material. Note that naming the scene nodes is optional. Scale the scene node larger
// (100 x 100 world units)
Node *planeNode = scene_->CreateChild("Plane");
planeNode->SetScale(Vector3(100.0f, 1.0f, 100.0f));
StaticModel *planeObject = planeNode->CreateComponent<StaticModel>();
planeObject->SetModel(cache->GetResource<Model>("Models/Plane.mdl"));
planeObject->SetMaterial(cache->GetResource<Material>("Materials/StoneTiled.xml"));
// Create a directional light to the world so that we can see something. The light scene node's orientation controls the
// light direction; we will use the SetDirection() function which calculates the orientation from a forward direction vector.
// The light will use default settings (white light, no shadows)
Node *lightNode = scene_->CreateChild("DirectionalLight");
lightNode->SetPosition(Vector3(0.0f, 30.0f, 0.0f));
lightNode->SetDirection(Vector3::DOWN); // The direction vector does not need to be normalized
Light *light = lightNode->CreateComponent<Light>();
light->SetLightType(LIGHT_DIRECTIONAL);
}
void Billiards::CreateTable() {
Node *tableNode = scene_->CreateChild("Table");
tableNode->SetPosition(Vector3(0.0f, 7.5f, 0.0f));
tableNode->SetScale(0.1f);
table_ = tableNode->CreateComponent<Table>();
table_->Init();
}
void Billiards::CreateWhiteBall() {
Node *whiteBallNode = scene_->CreateChild("WhiteBall");
whiteBallNode->SetPosition(INITIAL_WHITE_BALL_POSITION);
// Create the vehicle logic component
whiteBall_ = whiteBallNode->CreateComponent<WhiteBall>();
// Create the rendering and physics components
whiteBall_->Init(cameraNode_);
}
void Billiards::CreateBalls() {
// Test balls:
// CreateBall("Ball1TestNoWhite", Vector2(-14.0f, -7.5f));
// CreateBall(BLACK_BALL_NAME, Vector2(-14.0f, -7.5f), "Materials/Black.xml");
// CreateBall("Ball1", Vector2(-12.0f, -3.0f));
CreateBall("Ball1", Vector2(7.0f, -1.0f));
CreateBall("Ball2", Vector2(8.0f, -1.5f));
CreateBall("Ball3", Vector2(8.0f, -0.5f));
CreateBall("Ball4", Vector2(9.0f, 0.f));
CreateBall(BLACK_BALL_NAME, Vector2(9.0f, -1.0f), "Materials/Black.xml");
CreateBall("Ball6", Vector2(9.0f, -2.0f));
CreateBall("Ball7", Vector2(10.0f, 0.5f));
CreateBall("Ball8", Vector2(10.0f, -0.5f));
CreateBall("Ball9", Vector2(10.0f, -1.5f));
CreateBall("Ball10", Vector2(10.0f, -2.5f));
CreateBall("Ball11", Vector2(11.0f, 1.f));
CreateBall("Ball12", Vector2(11.0f, 0.f));
CreateBall("Ball13", Vector2(11.0f, -1.f));
CreateBall("Ball14", Vector2(11.0f, -2.f));
CreateBall("Ball15", Vector2(11.0f, -3.f));
}
void Billiards::CreateBall(String name, const Vector2 &position, String material) {
Node *ballNode = scene_->CreateChild(name);
ballNode->SetPosition(Vector3(position.x_, 7.5f, position.y_));
Ball *ball = ballNode->CreateComponent<Ball>();
ball->Init(material);
balls_.Push(WeakPtr<Ball>(ball));
}
void Billiards::CreateInterface() {
interface_ = GetSubsystem<UI>()->GetRoot()->CreateChild<Interface>();
interface_->Init();
}
void Billiards::SetupViewport() {
Renderer *renderer = GetSubsystem<Renderer>();
// Set up a viewport to the Renderer subsystem so that the 3D scene can be seen. We need to define the scene and the camera
// at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
// use, but now we just use full screen and default render path configured in the engine command line options
SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
renderer->SetViewport(0, viewport);
}
// For debugging purposes only
void Billiards::SpawnObject() {
ResourceCache *cache = GetSubsystem<ResourceCache>();
// Create a smaller box at camera position
Node *boxNode = scene_->CreateChild("SpawnedBall");
boxNode->SetPosition(cameraNode_->GetPosition());
boxNode->SetRotation(cameraNode_->GetRotation());
boxNode->SetScale(BALL_SCALE);
StaticModel *boxObject = boxNode->CreateComponent<StaticModel>();
boxObject->SetModel(cache->GetResource<Model>("Models/Sphere.mdl"));
boxObject->SetMaterial(cache->GetResource<Material>("Materials/StoneEnvMapSmall.xml"));
boxObject->SetCastShadows(true);
// Create physics components, use a smaller mass also
RigidBody *body = boxNode->CreateComponent<RigidBody>();
body->SetMass(BALL_MASS);
body->SetFriction(BALL_FRICTION);
body->SetLinearDamping(BALL_LINEAR_DAMPING);
body->SetAngularDamping(BALL_ANGULAR_DAMPING);
// body->SetRollingFriction(1.0f);
body->SetRestitution(BALL_RESTITUTION);
CollisionShape *shape = boxNode->CreateComponent<CollisionShape>();
shape->SetSphere(1.0f);
const float OBJECT_VELOCITY = 10.0f;
// Set initial velocity for the RigidBody based on camera forward vector. Add also a slight up component
// to overcome gravity better
body->SetLinearVelocity(cameraNode_->GetRotation() * Vector3(0.0f, 0.25f, 1.0f) * OBJECT_VELOCITY);
}
void Billiards::MoveCamera(float timeStep) {
// Do not move if the UI has a focused element (the console)
if (GetSubsystem<UI>()->GetFocusElement())
return;
Input *input = GetSubsystem<Input>();
// Movement speed as world units per second
const float MOVE_SPEED = 20.0f;
// Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
IntVector2 mouseMove = input->GetMouseMove();
yaw_ += MOUSE_SENSITIVITY * mouseMove.x_;
pitch_ += MOUSE_SENSITIVITY * mouseMove.y_;
pitch_ = Clamp(pitch_, -90.0f, 90.0f);
// Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f));
// Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
// Use the Translate() function (default local space) to move relative to the node's orientation.
if (input->GetKeyDown(KEY_W))
cameraNode_->Translate(Vector3::FORWARD * MOVE_SPEED * timeStep);
if (input->GetKeyDown(KEY_S))
cameraNode_->Translate(Vector3::BACK * MOVE_SPEED * timeStep);
if (input->GetKeyDown(KEY_A))
cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
if (input->GetKeyDown(KEY_D))
cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
}
void Billiards::SubscribeToEvents() {
// Subscribe HandleUpdate() function for processing update events
SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(Billiards, HandleUpdate));
// Subscribe to PostUpdate event for updating the camera position after physics simulation
SubscribeToEvent(E_POSTUPDATE, URHO3D_HANDLER(Billiards, HandlePostUpdate));
// Subscribe HandlePostRenderUpdate() function for processing the post-render update event, during which we request
// debug geometry
SubscribeToEvent(E_POSTRENDERUPDATE, URHO3D_HANDLER(Billiards, HandlePostRenderUpdate));
SubscribeToEvent(E_BALLINPOCKET, URHO3D_HANDLER(Billiards, HandleBallInPocket));
SubscribeToEvent(E_WHITEBALLINPOCKET, URHO3D_HANDLER(Billiards, HandleWhiteBallInPocket));
}
void Billiards::HandleBallInPocket(StringHash eventType, VariantMap &eventData) {
// remove ball from balls_ vector
String ballName = eventData[BallInPocket::P_BALLNAME].GetString();
if (ballName == BLACK_BALL_NAME) {
interface_->ShowGameOverScreen();
cameraFreeMode_ = true;
UnsubscribeFromEvent(E_UPDATE);
UnsubscribeFromEvent(E_POSTUPDATE);
UnsubscribeFromEvent(E_BALLINPOCKET);
UnsubscribeFromEvent(E_WHITEBALLINPOCKET);
return;
}
for (Vector<WeakPtr<Ball>>::Iterator it = balls_.Begin(); it != balls_.End(); ++it) {
if ((*it)->GetName() == ballName) {
balls_.Remove(*it);
break;
}
}
}
void Billiards::HandleWhiteBallInPocket(StringHash eventType, VariantMap &eventData) {
// remove ball from balls_ vector
whiteBallInPocket_ = true;
yaw_ = WHITE_BALL_INITIAL_YAW;
pitch_ = WHITE_BALL_INITIAL_PITH;
cameraNode_->SetPosition(CAMERA_INITIAL_POSITION);
interface_->ShowWhiteBallInPocketInfo();
}
void Billiards::HandleUpdate(StringHash eventType, VariantMap &eventData) {
using namespace Update;
Input *input = GetSubsystem<Input>();
// Take the frame time step, which is stored as a float
float timeStep = eventData[P_TIMESTEP].GetFloat();
// Before any next calculations - update isAnyBallMoving variable
IsAnyBallMoving();
if (!isAnyBallMoving_ && balls_.Empty()) {
// no update anymore - show win screen
interface_->ShowWinScreen();
UnsubscribeFromEvent(E_UPDATE);
UnsubscribeFromEvent(E_POSTUPDATE);
UnsubscribeFromEvent(E_BALLINPOCKET);
UnsubscribeFromEvent(E_WHITEBALLINPOCKET);
return;
}
if (whiteBall_) {
// handle changing camera mode
if (input->GetKeyPress(KEY_C)) {
if (!cameraFreeMode_) {
// if camera is switch to free mode - assign whiteBall controls to camera
yaw_ = whiteBall_->controls_.yaw_;
pitch_ = whiteBall_->controls_.pitch_;
}
cameraFreeMode_ = !cameraFreeMode_;
}
UI *ui = GetSubsystem<UI>();
// Get movement controls and assign them to the vehicle component. If UI has a focused element, clear controls
if (!ui->GetFocusElement()) {
if (!isAnyBallMoving_) {
// allow to push ball only after all balls stopped
whiteBall_->controls_.Set(CTRL_PUSH, input->GetKeyDown(KEY_SPACE));
}
if (!cameraFreeMode_) {
whiteBall_->controls_.yaw_ += (float) input->GetMouseMoveX() * MOUSE_SENSITIVITY;
whiteBall_->controls_.pitch_ += (float) input->GetMouseMoveY() * MOUSE_SENSITIVITY;
// Limit pitch
whiteBall_->controls_.pitch_ = Clamp(whiteBall_->controls_.pitch_, 10.0f, 70.0f);
}
} else {
whiteBall_->controls_.Set(CTRL_PUSH, 0);
}
} else {
cameraFreeMode_ = true;
}
// Move the camera, scale movement with time step
if (cameraFreeMode_) {
MoveCamera(timeStep);
}
// Toggle physics debug geometry with X
if (input->GetKeyPress(KEY_X))
drawDebug_ = !drawDebug_;
}
void Billiards::HandlePostUpdate(StringHash eventType, VariantMap &eventData) {
if (!whiteBall_ && !whiteBallInPocket_)
return;
if (whiteBallInPocket_) {
if (!isAnyBallMoving_) {
CreateWhiteBall();
whiteBallInPocket_ = false;
cameraFreeMode_ = false;
interface_->HideWhiteBallInPocketInfo();
}
return;
}
Node *whiteBallNode = whiteBall_->GetNode();
if (!cameraFreeMode_) {
Quaternion dir(whiteBall_->controls_.yaw_, Vector3::UP);
dir = dir * Quaternion(whiteBall_->controls_.pitch_, Vector3::RIGHT);
Vector3 cameraTargetPos = whiteBallNode->GetPosition() - dir * Vector3(0.0f, 0.0f, CAMERA_DISTANCE);
Vector3 cameraStartPos = whiteBallNode->GetPosition();
// Raycast camera against static objects (physics collision mask 2)
// and move it closer to the vehicle if something in between
Ray cameraRay(cameraStartPos, cameraTargetPos - cameraStartPos);
float cameraRayLength = (cameraTargetPos - cameraStartPos).Length();
PhysicsRaycastResult result;
scene_->GetComponent<PhysicsWorld>()->RaycastSingle(result, cameraRay, cameraRayLength, 2);
if (result.body_)
cameraTargetPos = cameraStartPos + cameraRay.direction_ * (result.distance_ - 0.5f);
cameraNode_->SetPosition(cameraTargetPos);
cameraNode_->SetRotation(dir);
}
if (!isAnyBallMoving_) {
// set push level
const int level = int(
whiteBall_->pushButtonHoldingTime_ / MAX_PUSH_BUTTON_HOLD_TIME * PUSH_FORCE_LEVEL_BAR_DOTS_COUNT
);
interface_->UpdatePushLevel(level);
}
interface_->SetStatusText(isAnyBallMoving_);
}
void Billiards::HandlePostRenderUpdate(StringHash eventType, VariantMap &eventData) {
// If draw debug mode is enabled, draw physics debug geometry. Use depth test to make the result easier to interpret
if (drawDebug_) {
scene_->GetComponent<PhysicsWorld>()->DrawDebugGeometry(true);
}
}
bool Billiards::IsAnyBallMoving() {
isAnyBallMoving_ = false;
if (whiteBall_ && whiteBall_->IsMoving()) {
// if white ball is moving - do not even check other balls
isAnyBallMoving_ = true;
} else {
for (Vector<WeakPtr<Ball>>::Iterator it = balls_.Begin(); it != balls_.End(); ++it) {
WeakPtr<Ball> ball = (*it);
if (ball && ball->IsMoving()) {
isAnyBallMoving_ = true;
break;
}
}
}
return isAnyBallMoving_;
}