-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPhysics.cpp
More file actions
194 lines (160 loc) · 3.12 KB
/
Physics.cpp
File metadata and controls
194 lines (160 loc) · 3.12 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
#include "Physics.h"
namespace DifferentialArts
{
/*
=======================
Mass Class
=======================
*/
__forceinline Mass::Mass(void)
{
this->setM(1.0f);
pos = Math::Vector3(0, 0, 0);
vel = Math::Vector3(0, 0, 0);
force = Math::Vector3(0, 0, 0);
}
__forceinline Mass::Mass(float m)
{
this->setM(m);
pos = Math::Vector3(0, 0, 0);
vel = Math::Vector3(0, 0, 0);
force = Math::Vector3(0, 0, 0);
}
__forceinline Mass::Mass(float m, const Math::Vector3 &pos, const Math::Vector3 &vel)
{
this->setM(m);
this->pos = pos;
this->vel = vel;
this->force = Math::Vector3(0, 0, 0);
}
__forceinline void Mass::applyForce(const Math::Vector3 &f)
{
force += f;
}
__forceinline void Mass::init(void)
{
force.x = 0;
force.y = 0;
force.z = 0;
}
__forceinline void Mass::simulate(float dt)
{
vel += (force/m) * dt;
pos += vel * dt;
}
__forceinline void Mass::setVel(const Math::Vector3 &vel)
{
this->vel = vel;
}
__forceinline Math::Vector3 Mass::getVel(void)
{
return this->vel;
}
__forceinline void Mass::setPos(const Math::Vector3 &pos)
{
this->pos = pos;
}
__forceinline Math::Vector3 Mass::getPos(void)
{
return this->pos;
}
__forceinline void Mass::setM(float m)
{
if(this->m <= 0.0f)
{
m = 1.0f;
}
this->m = m;
this->inverseMass = 1/this->m;
}
__forceinline float Mass::getM(void)
{
return this->m;
}
__forceinline float Mass::getInverseM(void)
{
return this->inverseMass;
}
__forceinline Math::Vector3 Mass::getForce(void)
{
return force;
}
/*
=======================
Simulation Class
=======================
*/
Simulation::Simulation(int n, float m)
{
numberOfMasses = n;
masses = new Mass*[n];
for(int i=0;i<numberOfMasses;i++)
{
masses[i] = new Mass(m);
}
}
void Simulation::release(void)
{
for(int i=0;i<numberOfMasses;i++)
{
delete masses[i];
masses[i] = 0;
}
delete masses;
masses = 0;
}
void Simulation::init(void)
{
for(int i=0;i<numberOfMasses;i++)
{
masses[i]->init();
}
}
void Simulation::solve(void)
{
}
Mass* Simulation::getMass(int n)
{
if(n < 0 || n >=numberOfMasses)
return 0;
return masses[n];
}
void Simulation::simulate(float dt)
{
for(int i=0;i<numberOfMasses;i++)
{
masses[i]->simulate(dt);
}
}
void Simulation::operate(float dt)
{
init();
solve();
simulate(dt);
}
/*
=======================
Spring Class
=======================
*/
Spring::Spring(Mass* mass1, Mass* mass2, float springConstant, float springLength,
float frictionConstant)
{
this->mass1 = mass1;
this->mass2 = mass2;
this->springLength = springLength;
this->springConstant = springConstant;
this->frictionConstant = frictionConstant;
}
void Spring::solve(void)
{
Math::Vector3 force;
Math::Vector3 springVector = mass1->getPos() - mass2->getPos();
float inverseR = springVector.InverseMagnitude();
float r = 1/inverseR;
force += (springVector*inverseR) * (r-springLength) * -springConstant;
force += -(mass1->getVel() - mass2->getVel()) * frictionConstant;
mass1->applyForce(force);
mass2->applyForce(-force);
}
}