-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhysics.cpp
More file actions
169 lines (140 loc) · 4.78 KB
/
Physics.cpp
File metadata and controls
169 lines (140 loc) · 4.78 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
//
// Physics.cpp
// VirtualMarbleGame
//
// Created by Maximilian Weber
//
#include <iostream>
#include "Physics.h"
Physics::Physics(Labyrinth* labyrinth, Marble* marble, Pose* pose, Pose* gravity)
{
m_labyrinth = labyrinth;
m_marble = marble;
m_pose = pose;
m_gravity = gravity;
rotate90 = false;
m_calibrate = 1;
}
void Physics::collideX(float border) {
m_marble->v_x *= -0.5;
m_marble->m_x = border;
}
void Physics::collideY(float border) {
m_marble->v_y *= -0.5;
m_marble->m_y = border;
}
int x_old = -1, y_old = -1;
/**
We check all 8 neighbours here
**/
void Physics::collisionDetection()
{
// /* Position in maze grid has changed */
// if((int)(m_marble->m_x) != x_old || (int)(m_marble->m_y) != y_old ) {
//
// x_old = (int)(m_marble->m_x);
// y_old = (int)(m_marble->m_y);
//
// printf("we are in %i, %i\n", (int)(m_marble->m_x), (int)(m_marble->m_y));
//
// printf("checking %i, %i: %i\n", (int)(m_marble->m_x) + 1, (int)(m_marble->m_y), m_labyrinth->hasBlock(m_marble->m_x+1, m_marble->m_y));
// printf("checking %i, %i: %i\n", (int)(m_marble->m_x) - 1, (int)(m_marble->m_y), m_labyrinth->hasBlock(m_marble->m_x-1, m_marble->m_y));
// printf("checking %i, %i: %i\n", (int)(m_marble->m_x), (int)(m_marble->m_y) + 1, m_labyrinth->hasBlock(m_marble->m_x, m_marble->m_y + 1));
// printf("checking %i, %i: %i\n", (int)(m_marble->m_x), (int)(m_marble->m_y) - 1, m_labyrinth->hasBlock(m_marble->m_x, m_marble->m_y - 1));
//
// }
/* Level borders */
float max = Labyrinth_size - m_marble->m_radius;
float min = m_marble->m_radius;
if(x_new > max) {
collideX(max);
} else if(x_new < min) {
collideX(min);
}
if(y_new > max) {
collideY(max);
} else if(y_new < min) {
collideY(min);
}
/* Maze blocks: x + 1 */
if(m_labyrinth->hasBlock(m_marble->m_x + 1, m_marble->m_y)) {
if((int)(x_new + m_marble->m_radius) > (int)m_marble->m_x) {
collideX((int)(m_marble->m_x + 1) - m_marble->m_radius);
}
}
/* Maze blocks: x - 1 */
if(m_labyrinth->hasBlock(m_marble->m_x - 1, m_marble->m_y)) {
if((int)(x_new - m_marble->m_radius) < (int)m_marble->m_x) {
collideX((int)(m_marble->m_x) + m_marble->m_radius);
}
}
/* Maze blocks: y + 1 */
if(m_labyrinth->hasBlock(m_marble->m_x, m_marble->m_y + 1)) {
if((int)(y_new + m_marble->m_radius) > (int)m_marble->m_y) {
collideY((int)(m_marble->m_y + 1) - m_marble->m_radius);
}
}
/* Maze blocks: y - 1 */
if(m_labyrinth->hasBlock(m_marble->m_x, m_marble->m_y - 1)) {
if((int)(y_new - m_marble->m_radius) < (int)m_marble->m_y) {
collideY((int)(m_marble->m_y) + m_marble->m_radius);
}
}
}
void Physics::switch90()
{
rotate90 =! rotate90;
}
void Physics::process()
{
if(m_calibrate)
return;
float t = 0.033;
// calculate components of gravity vector
// assuming g = ( x = 0, y = -9.81, z = 0 ) in camera coordinates
// float a_x = m_gravity->matrix[4] * 9.81;
// float a_y = m_gravity->matrix[5] * 9.81;
// float a_z = m_gravity->matrix[6] * 9.81;
// transform gravity coordinates to world coordinates
// assuming g = ( x = 0, y = -9.81, z = 0 ) in calibration coordinates
float x_world;
float y_world;
float z_world;
if(rotate90)
{
x_world = m_gravity->matrix[8] * 9.81;
y_world = m_gravity->matrix[9] * 9.81;
z_world = m_gravity->matrix[10] * 9.81;
}
else
{
x_world = m_gravity->matrix[4] * -9.81;
y_world = m_gravity->matrix[5] * -9.81;
z_world = m_gravity->matrix[6] * -9.81;
}
// transform world coordinates to labyrinth coordinates
float a_x = m_pose->matrix[0] * x_world + m_pose->matrix[4] * y_world + m_pose->matrix[8] * z_world;
float a_y = m_pose->matrix[1] * x_world + m_pose->matrix[5] * y_world + m_pose->matrix[9] * z_world;
// float a_z = m_pose->matrix[2] * x_world + m_pose->matrix[6] * y_world + m_pose->matrix[10] * z_world;
// calculate speed
m_marble->v_x += a_x * t * 10;
m_marble->v_y += a_y * t * 10;
// calculate new position
x_new = m_marble->m_x + m_marble->v_x * t;
y_new = m_marble->m_y + m_marble->v_y * t;
// collision with level boundaries?
collisionDetection();
// set new position
float increment_x = m_marble->v_x * t;
//if(abs(increment_x) > 0.05)
m_marble->m_x += increment_x;
float increment_y = m_marble->v_y * t;
//if(abs(increment_y) > 0.05)
m_marble->m_y += increment_y;
}
Physics::~Physics()
{
}
void Physics::switchCalibrate() {
m_calibrate ^= 1;
}