-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRigidBodyCollisionSimulation.cpp
385 lines (329 loc) · 12.6 KB
/
RigidBodyCollisionSimulation.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
#pragma once
#include <gui/application.h>
#include <gui/camera.h>
#include <gui/light.h>
#include <gui/renderer.h>
#include <gui/shader.h>
#include <sim/RBPhysicsEngine.h>
#include <utils/logger.h>
#include "scenes.h"
using namespace crl::gui;
namespace crl {
namespace app {
namespace rigidbody {
class App : public Basic3DAppWithShadows {
public:
App(const char *title = "CMM Assignment 5 - Rigid Bodies",
std::string iconPath = CMM_DATA_FOLDER "/crl_icon_red.png")
: Basic3DAppWithShadows(title, iconPath) {
camera = TrackingCamera(5);
camera.rotAboutUpAxis = 0;
camera.rotAboutRightAxis = 0.25;
light.s = 0.04f;
shadowbias = 0.00001f;
camera.aspectRatio = float(width) / height;
glEnable(GL_DEPTH_TEST);
showConsole = true;
automanageConsole = true;
Logger::maxConsoleLineCount = 10;
consoleHeight = 225;
this->targetFramerate = 30;
this->limitFramerate = true;
this->showConsole = false;
// setup world
setupWorld();
}
virtual ~App() override {
if (physicsEngine) {
delete physicsEngine;
}
}
virtual void resizeWindow(int width, int height) override {
camera.aspectRatio = float(width) / height;
return Application::resizeWindow(width, height);
}
bool mouseMove(double xpos, double ypos) override {
P3D rayOrigin;
V3D rayDirection;
camera.getRayFromScreenCoordinates(xpos, ypos, rayOrigin, rayDirection);
Ray mouseRay(rayOrigin, rayDirection);
if (mouseState.dragging) {
if (selectedRB != NULL && mouseState.lButtonPressed == true) {
mouseRay.getDistanceToPoint(selectedPoint,
&targetForSelectedPoint);
return true;
}
}
camera.processMouseMove(mouseState, keyboardState);
return true;
}
bool mouseButtonReleased(int button, int mods) override {
if (button == GLFW_MOUSE_BUTTON_LEFT) {
if (selectedRB) {
selectedRB->rbProps.selected = false;
selectedRB = nullptr;
}
}
return true;
}
virtual bool mouseButtonPressed(int button, int mods) override {
if (button == GLFW_MOUSE_BUTTON_LEFT ||
button == GLFW_MOUSE_BUTTON_RIGHT) {
if (this->selectedRB != NULL)
this->selectedRB->rbProps.selected = false;
P3D rayOrigin;
V3D rayDirection;
camera.getRayFromScreenCoordinates(mouseState.lastMouseX,
mouseState.lastMouseY, rayOrigin,
rayDirection);
Ray mouseRay(rayOrigin, rayDirection);
// ray tracing and check selection
crl::RB *selectedRB = NULL;
if (!selectedRB)
selectedRB =
physicsEngine->getFirstRBHitByRay(mouseRay, selectedPoint);
if (button == GLFW_MOUSE_BUTTON_LEFT) {
// select RB by left click
if (selectedRB != NULL) {
selectedRB->rbProps.selected = true;
selectedPointInLocalCoords =
selectedRB->state.getLocalCoordinates(selectedPoint);
targetForSelectedPoint = selectedPoint;
}
this->selectedRB = selectedRB;
}
}
return true;
}
virtual bool scrollWheel(double xoffset, double yoffset) override {
camera.processMouseScroll(xoffset, yoffset);
return true;
}
void process() override {
if (appIsRunning == false) return;
// substeps (simulation)
double simTime = 0;
while (simTime < 1.0 / targetFramerate) {
simTime += dt;
// apply force
if (selectedRB)
physicsEngine->applyForceTo(
selectedRB,
V3D(selectedRB->state.getWorldCoordinates(selectedPointInLocalCoords),targetForSelectedPoint) *500.0,
selectedPointInLocalCoords);//rb,force,position
physicsEngine->step(dt);
if (slowMo) break;
}
// traj data
if (selectedScene == SimulationScene::Projectile) {
if (projectileTraj.size() < 1.0 / dt) {
P3D p = physicsEngine->rbs[0]->state.pos;
projectileTraj.push_back(p);
}
}
}
virtual void drawAuxiliaryInfo() override {
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
drawFPS();
drawConsole();
drawImGui();
ImGui::EndFrame();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
// objects drawn with a shadowMapRenderer (during shadow pass) will cast a
// shadow
virtual void drawShadowCastingObjects() override {
physicsEngine->draw(shadowMapRenderer);
}
// objects drawn with a shadowShader (during the render pass) will have
// shadows cast on them
virtual void drawObjectsWithShadows() override {
ground.draw(shadowShader, V3D(0.6, 0.6, 0.8));
physicsEngine->draw(shadowShader);
}
// objects drawn with basic shadowShader (during the render pass) will not
// have shadows cast on them
virtual void drawObjectsWithoutShadows() override {
if (selectedScene == SimulationScene::Projectile) {
// draw analytic trajectory
double timestep = 0.01;
for (uint i = 0; i < 100; i++) {
double t = i * timestep;
P3D p = projectileP0 + projectileV0 * t +
0.5 * V3D(0, RBGlobals::g, 0) * t * t;
drawSphere(p, 0.01, basicShader);
}
// draw real trajectory
for (uint i = 0; i < projectileTraj.size(); i++) {
drawSphere(projectileTraj[i], 0.02, basicShader, V3D(0, 1, 0));
}
}
if (selectedRB != NULL) {
drawArrow3d(selectedRB->state.getWorldCoordinates(
selectedPointInLocalCoords),
V3D(selectedRB->state.getWorldCoordinates(
selectedPointInLocalCoords),
targetForSelectedPoint),
0.025, basicShader);
}
}
virtual bool keyPressed(int key, int mods) override {
bool dirty = false;
if (key == GLFW_KEY_SPACE) {
appIsRunning = !appIsRunning;
}
return false;
}
virtual void drawImGui() {
ImGui::SetNextWindowPos(ImVec2(0, 0), ImGuiCond_Once);
ImGui::Begin("Main Menu");
ImGui::Text("Simulation Scene:");
if (ImGui::BeginCombo("##scene",
simulationSceneNames[selectedScene].c_str())) {
for (uint n = 0; n < simulationSceneNames.size(); n++) {
bool is_selected = (selectedScene == n);
if (ImGui::Selectable(simulationSceneNames[n].c_str(),
is_selected)) {
selectedScene = (SimulationScene)n;
setupWorld();
}
if (is_selected) {
ImGui::SetItemDefaultFocus();
}
}
ImGui::EndCombo();
}
ImGui::Text("Play:");
ImGui::SameLine();
PlayPauseButton("Play App", &appIsRunning);
if (appIsRunning == false) {
ImGui::SameLine();
if (ImGui::ArrowButton("tmp", ImGuiDir_Right)) {
appIsRunning = true;
process();
appIsRunning = false;
}
}
ImGui::Text("Slow Motion Mode:");
ImGui::SameLine();
ToggleButton("SlowMo", &slowMo);
static int simRate = 30;
ImGui::Text("Simulation Rate:");
if (ImGui::SliderInt("Rate", &simRate, 30, 300)) dt = 1.0 / simRate;
ImGui::InputDouble("dt", &dt, 0.0, 0.0, "%.5f",
ImGuiInputTextFlags_ReadOnly);
ImGui::Text("Simulation Settings:");
if (physicsEngine->simulateCollisions) {
ImGui::SliderFloat("epsilon", &physicsEngine->eps, 0.0, 1.0);
}
if (ImGui::TreeNode("Draw options...")) {
ImGui::Checkbox("Draw Coordinate Frames",
&physicsEngine->showCoordFrame);
ImGui::TreePop();
}
if (ImGui::TreeNode("Debug options...")) {
ImGui::Checkbox("Draw debug info", &drawDebugInfo);
ImGui::TreePop();
}
ImGui::End();
}
private:
void setupWorld() {
// reset physics engine first
resetPhysicsEngine();
// clean data
projectileTraj.clear();
// setup scene now
switch (selectedScene) {
case SimulationScene::Projectile: {
physicsEngine->simulateCollisions = false;
camera.distanceToTarget = 5;
camera.rotAboutUpAxis = 0;
camera.rotAboutRightAxis = 0.25;
auto *rb = physicsEngine->addRigidBodyToEngine();
rb->state.pos = projectileP0;
rb->state.orientation = Quaternion(0.1, 0.5, 0, 0).normalized();
rb->state.velocity = projectileV0;
rb->state.angularVelocity = V3D(0, 10, 0);
break;
}
case SimulationScene::FixedSprings: {
physicsEngine->simulateCollisions = false;
camera.distanceToTarget = 6;
camera.rotAboutUpAxis = 0;
camera.rotAboutRightAxis = 0.25;
auto *rb = physicsEngine->addRigidBodyToEngine();
rb->state.pos = P3D(0, 0.5, 0);
physicsEngine->addSpringToEngine(
nullptr, rb, P3D(0.12, 2, 0.12), P3D(0.12, 0.12, 0.12));
break;
}
case SimulationScene::RigidBodySprings: {
physicsEngine->simulateCollisions = false;
camera.distanceToTarget = 6;
camera.rotAboutUpAxis = 0;
camera.rotAboutRightAxis = 0.25;
auto *rb1 = physicsEngine->addRigidBodyToEngine();
rb1->state.pos = P3D(0, 1.5, 0);
auto *rb2 = physicsEngine->addRigidBodyToEngine();
rb2->state.pos = P3D(0, 0.5, 0);
// world~rb1
physicsEngine->addSpringToEngine(
nullptr, rb1, P3D(0.12, 2, 0.12), P3D(0.12, 0.12, 0.12));
physicsEngine->addSpringToEngine(
nullptr, rb1, P3D(0.12, 2, -0.12), P3D(0.12, 0.12, -0.12));
// rb1~rb2
physicsEngine->addSpringToEngine(
rb1, rb2, P3D(-0.12, -0.12, 0.12), P3D(-0.12, 0.12, 0.12));
physicsEngine->addSpringToEngine(rb1, rb2,
P3D(-0.12, -0.12, -0.12),
P3D(-0.12, 0.12, -0.12));
break;
}
case SimulationScene::Collision: {
physicsEngine->simulateCollisions = true;
camera.distanceToTarget = 5;
camera.rotAboutUpAxis = 0;
camera.rotAboutRightAxis = 0.25;
auto *rb = physicsEngine->addCollidingRigidBodyToEngine();
rb->state.pos = collisionP0;
break;
}
default:
break;
}
}
void resetPhysicsEngine() {
if (physicsEngine) delete physicsEngine;
physicsEngine = new RBPhysicsEngine();
}
public:
SimpleGroundModel ground;
// simulation conf
double dt = 1 / 30.0;
// simulation engine
RBPhysicsEngine *physicsEngine = nullptr;
// simulation scene
SimulationScene selectedScene = SimulationScene::Projectile;
// interaction
crl::RB *selectedRB = NULL;
P3D selectedPoint;
P3D selectedPointInLocalCoords;
P3D targetForSelectedPoint;
// data
std::vector<P3D> projectileTraj;
// some constants
const P3D projectileP0 = P3D(RBGlobals::g * 0.25, 0.5, 0);
const V3D projectileV0 = V3D(-RBGlobals::g * 0.5, -RBGlobals::g * 0.5, 0);
const P3D collisionP0 = P3D(0, -RBGlobals::g * 0.15, 0);
// flags
bool slowMo = false;
bool appIsRunning = false;
bool drawDebugInfo = true;
};
} // namespace rigidbody
} // namespace app
} // namespace crl