-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRigidSolver.hpp
More file actions
168 lines (134 loc) · 4.63 KB
/
Copy pathRigidSolver.hpp
File metadata and controls
168 lines (134 loc) · 4.63 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
// ----------------------------------------------------------------------------
// RigidSolver.hpp
//
// Created on: 18 Dec 2020
// Author: Kiwon Um
// Mail: kiwon.um@telecom-paris.fr
//
// Description: Simple Rigid Body Solver (DO NOT DISTRIBUTE!)
//
// Copyright 2020-2023 Kiwon Um
//
// The copyright to the computer program(s) herein is the property of Kiwon Um,
// Telecom Paris, France. The program(s) may be used and/or copied only with
// the written permission of Kiwon Um or in accordance with the terms and
// conditions stipulated in the agreement/contract under which the program(s)
// have been supplied.
// ----------------------------------------------------------------------------
#ifndef _RIGIDSOLVER_HPP_
#define _RIGIDSOLVER_HPP_
#include <glm/ext/matrix_transform.hpp>
#include "Vector3.hpp"
#include "Matrix3x3.hpp"
#include "Quaternion.h"
struct BodyAttributes {
BodyAttributes() :
X(0, 0, 0), R(Mat3f::I()), P(0, 0, 0), L(0, 0, 0),
V(0, 0, 0), omega(0, 0, 0), F(0, 0, 0), tau(0, 0, 0), Q(1 , Vec3f(0, 0, 0)) {}
glm::mat4 worldMat() const
{
return glm::mat4( // column-major
R(0,0), R(1,0), R(2,0), 0,
R(0,1), R(1,1), R(2,1), 0,
R(0,2), R(1,2), R(2,2), 0,
X[0], X[1], X[2], 1);
}
tReal M; // mass
Mat3f I0, I0inv; // inertia tensor and its inverse in body space
Mat3f Iinv; // inverse of inertia tensor
// rigid body state
Vec3f X; // position
Mat3f R; // rotation
Vec3f P; // linear momentum
Vec3f L; // angular momentum
Quaternion Q;
// auxiliary quantities
Vec3f V; // linear velocity
Vec3f omega; // angular velocity
// force and torque
Vec3f F; // force
Vec3f tau; // torque
// mesh's vertices in body space
std::vector<Vec3f> vdata0;
};
class Box : public BodyAttributes {
public:
explicit Box(
const tReal w=1.0, const tReal h=1.0, const tReal d=1.0, const tReal dens=10.0,
const Vec3f v0=Vec3f(0, 0, 0), const Vec3f omega0=Vec3f(0, 0, 0)) :
width(w), height(h), depth(d)
{
V = v0; // initial velocity
omega = omega0; // initial angular velocity
// TODO: calculate physical attributes
M = dens * w * h * d;
I0 = Mat3f(Vec3f(
(1.f / 12.f) * M * (pow(h, 2) + pow(d, 2)),
(1.f / 12.f) * M * (pow(w, 2) + pow(d, 2)),
(1.f / 12.f) * M * (pow(w, 2) + pow(h, 2)))
);
I0inv = I0.inverse();
Iinv = R * I0inv * R.transpose();
// vertices data (8 vertices)
vdata0.push_back(Vec3f(-0.5*w, -0.5*h, -0.5*d));
vdata0.push_back(Vec3f( 0.5*w, -0.5*h, -0.5*d));
vdata0.push_back(Vec3f( 0.5*w, 0.5*h, -0.5*d));
vdata0.push_back(Vec3f(-0.5*w, 0.5*h, -0.5*d));
vdata0.push_back(Vec3f(-0.5*w, -0.5*h, 0.5*d));
vdata0.push_back(Vec3f( 0.5*w, -0.5*h, 0.5*d));
vdata0.push_back(Vec3f( 0.5*w, 0.5*h, 0.5*d));
vdata0.push_back(Vec3f(-0.5*w, 0.5*h, 0.5*d));
}
// rigid body property
tReal width, height, depth;
};
class RigidSolver {
public:
explicit RigidSolver(
BodyAttributes *body0=nullptr, const Vec3f g=Vec3f(0, 0, 0)) :
body(body0), _g(g), _step(0), _sim_t(0) {}
void init(BodyAttributes *body0)
{
body = body0;
_step = 0;
_sim_t = 0;
}
void step(tReal dt)
{
std::cout << "t=" << _sim_t << " (dt=" << dt << ")" << std::endl;
computeForceAndTorque();
// TODO: time integration
//Linear momentum
body->P += body->F * dt;
body->V = body->P / body->M;
body->X += dt * body->V;
//Angular momentum
body->Iinv = body->R * body->I0inv * body->R.transposed();
body->L += body->tau * dt;
body->omega = body->Iinv * body->L;
body->Q += Quaternion(0, body->omega) * body->Q * dt / 2;
body->R = body->Q.normalized().getR();
//body->R += body->omega.crossProductMatrix() * body->R * dt;
++_step;
_sim_t += dt;
}
BodyAttributes *body;
private:
void computeForceAndTorque()
{
// TODO: force and torque calculation
body->F = Vec3f(0, 0, 0);
body->F += body->M * _g;
body->tau = Vec3f(0, 0, 0);
// TODO: instance force at the very first step
if(_step==1) {
body->F = 2*Vec3f(0.5f, 1.0f, 0.05f);
body->tau = crossProductMatrix(body->R * body->vdata0[0]) * body->F;
}
}
// simulation parameters
Vec3f _g; // gravity
tIndex _step; // step count
tReal _sim_t; // simulation time
};
#endif /* _RIGIDSOLVER_HPP_ */