-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rope.cpp
54 lines (46 loc) · 1.45 KB
/
Rope.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
#include "Rope.h"
#include <string>
template <typename pos_t, typename mass_t>
Matrix<pos_t, 3, 1> Rope<pos_t, mass_t>::project(const Vec3d& a, const Vec3d& b)
{
pos_t length = a.dot(b)/b.dot(b);
Vec3d direction = b;
return direction*length;
}
template <typename pos_t, typename mass_t>
Rope<pos_t, mass_t>::Rope(pos_t mass, const Vec3d& start, const Vec3d& end, unsigned num_links)
{
mass_t link_mass = mass/num_links;
if (link_mass == 0)
throw string("link_mass in Rope::Rope() has a value of zero. This is not allowed.")
+ string("Check to see of mass is zero or if mass_t is integer and mass < num_links.");
Vec3d diff = end - start;
for (unsigned i = 0; i <= num_links; ++i)
{
links.push_back(PointMass<pos_t, mass_t>(link_mass, diff*pos_t(i)/num_links + start));
}
}
template <typename pos_t, typename mass_t>
void Rope<pos_t, mass_t>::addPermanentForceAt(Vec3d force, size_t link)
{
links[link].accel += force/links[link].mass;
}
template <typename pos_t, typename mass_t>
void Rope<pos_t, mass_t>::addPermanentForce(Vec3d force)
{
for (size_t i = 0; i < links.size(); ++i)
links[i].accel += force/links[i].mass;
}
template <typename pos_t, typename mass_t>
void Rope<pos_t, mass_t>::addImpulseAt(Vec3d momentum, size_t link)
{
links[link].vel_change += momentum/links[link].mass;
}
template <typename pos_t, typename mass_t>
void Rope<pos_t, mass_t>::step(int millisecs)
{
for (size_t i = 0; i < links.size(); ++i)
{
links[i].step(millisecs);
}
}