-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRigid2.cpp
More file actions
205 lines (153 loc) · 4.49 KB
/
Rigid2.cpp
File metadata and controls
205 lines (153 loc) · 4.49 KB
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
#if 0
#include "Particle.h"
#include "Application.h"
#include "Shader.h"
#include "RigidBody.h"
#include <random>
// Std. Includes
#include <string>
#include <time.h>
#include <array>
#include <cmath>
// Other Libs
#include "SOIL2/SOIL2.h"
#include "Rigid.h"
/*
Vectors and Arrays
*/
glm::vec3 cpoint = glm::vec3(-6.0f, 0.0f, -6.0f);
glm::vec3 size = glm::vec3(12.0f, 12.0f, 12.0f);
bool check = false;
bool check2 = false;
bool check3 = true;
bool respond = false;
bool collide = false;
glm::vec3 lowestPoint = glm::vec3(0.0f);
glm::vec3 pointOfCollision = glm::vec3(0.0f);
std::set<Vertex> collidingVerts(float yCoordinate, RigidBody &rb)
{
std::set<Vertex> collidingVertices;
// Check if it collides with plane
for (auto v : rb.getMesh().getVertices())
{
// check the y coord of: localCoord * sclae + worldCoord
// note: with opengl you need to use mat * vec
glm::vec3 wCoord = glm::mat3(rb.getMesh().getModel()) * v.getCoord() + rb.getPos();
if (wCoord.y < yCoordinate)
{
collidingVertices.insert(wCoord);
}
}
return collidingVertices;
}
int main()
{
// create application
Application app = Application::Application();
app.initRender();
Application::camera.setCameraPosition(glm::vec3(0.0f, 5.0f, 20.0f));
// create ground plane
Mesh plane = Mesh::Mesh();
// scale it up x5
plane.scale(glm::vec3(20.0f, 1.0f, 20.0f));
plane.setShader(Shader("resources/shaders/core.vert", "resources/shaders/core.frag"));
Gravity g = Gravity();
Drag drag = Drag();
Wind wind = Wind();
wind.setWind(glm::vec3(1.0f, 0.0f, .0f));
// Set up a cubic rigid body .
RigidBody rb = RigidBody();
Mesh m = Mesh::Mesh(Mesh::CUBE);
rb.setMesh(m);
Shader rbShader = Shader("resources/shaders/core.vert", "resources/shaders/core_blue.frag");
rb.getMesh().setShader(rbShader);
std::set<Vertex> pointsCollided;
// rigid body motion values
rb.translate(glm::vec3(0.0f, 10.0f, 0.0f));
rb.setVel(glm::vec3(5.0f, 0.0f, 0.0f));
rb.setAngVel(glm::vec3(0.0f, 0.0f, 0.0f));
rb.getMesh().scale(glm::vec3(1.0f, 3.0f, 1.0f));
rb.setMass(2.0f);
// add forces to Rigid body
rb.addForce(&g);
// new time
const float dt = 0.01f;
float accumulator = 0.0f;
GLfloat currentTime = (GLfloat)glfwGetTime();
glm::vec3 j = glm::vec3(1.0f, 0.0f, 0.0f);
bool appliedImpulse = false;
// Game loop
while (!glfwWindowShouldClose(app.getWindow()))
{
//New frame time
GLfloat newTime = (GLfloat)glfwGetTime();
GLfloat frameTime = newTime - currentTime;
//*******************************************************************************************************************
frameTime *= 2;
currentTime = newTime;
accumulator += frameTime;
app.doMovement(dt);
while (accumulator >= dt)
{
std::set<Vertex> collidingVertices = collidingVerts(plane.getPos().y, rb);
bool collisionOccurs = collidingVertices.size() > 0;
if (collisionOccurs)
{
Vertex lowestVertex;
for (auto v : collidingVertices)
{
lowestVertex = v;
}
for (auto v : collidingVertices)
{
if (v.getCoord().y < lowestVertex.getCoord().y)
{
lowestVertex = v;
}
}
glm::vec3 displacement = glm::vec3(0.0f);
displacement.y = glm::abs(lowestVertex.getCoord().y);
rb.translate(displacement);
rb.getAcc() = glm::vec3(0.0f);
rb.getVel() = glm::vec3(0.0f);
rb.setAngVel(glm::vec3(0.0f));
glm::vec3 sum = glm::vec3(0.0f);
for (auto v : collidingVertices)
{
sum += v.getCoord();
}
Vertex averageCollidingPoint = Vertex(sum / collidingVertices.size());
if (!check)
{
for (auto v : collidingVertices)
{
std::cout << "Coordinate of colliding vertex ->" << glm::to_string(v.getCoord())<<std::endl;
}
std::cout << "Point of collision ->" << glm::to_string(averageCollidingPoint.getCoord()) << std::endl;
check = true;
}
}
// integration position
rb.setAcc(rb.applyForces(rb.getPos(), rb.getVel()));
rb.getVel() = rb.getVel() + dt * rb.getAcc();
rb.setPos(rb.getPos() + dt*(rb.getVel()));
// integration rotation
rb.setAcc(rb.applyForces(rb.getPos(), rb.getVel()));
rb.setAngVel(rb.getAngVel() + dt * rb.getAngAcc());
glm::mat3 angVelSkew = glm::matrixCross3(rb.getAngVel());
glm::mat3 R = glm::mat3(rb.getRotate());
R += dt * angVelSkew * R;
R = glm::orthonormalize(R);
rb.getMesh().setRotate(glm::mat4(R));
accumulator -= dt;
}
// clear buffer
app.clear();
app.draw(rb.getMesh());
app.draw(plane);
app.display();
}
app.terminate();
return EXIT_SUCCESS;
}
#endif