-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.cpp
173 lines (131 loc) · 3.46 KB
/
block.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
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
#include "stdafx.h"
#include<fstream>
#include<iostream>
#include<vector>
#include<array>
#include <memory>
#include "Block.h"
using namespace std;
const int blockHeight = 5;
const int blockWidth = 5;
const int numBlocks = blockHeight*blockWidth;
double dt = 1e-7;
double nu = sqrt(0.1e3); //Dampning coefficient
double k = 2.3e6; // Stiffness between blocks
/*
double L = 0.14; // Physical length of block chain
double d = L / (numBlocks - 1); // Distance between blocks in block chain
double M = 0.12;
double m = M / numBlocks;
*/
block::block(){};
bool block::returnState() { return false; };
double lenght(double x1, double y1, double x2, double y2)
{
return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
}
//block(const block&) = default;
void block::setData(double x, double y, const int numberOfBlocks_, double L_, double M_, int xID_, int yID_)
{
xPos = x;
yPos = y;
springAttachmentPoint = x;
numberOfBlocks = numberOfBlocks_;
blockL = L_;
blockM = M_;
blockd = blockL / (numberOfBlocks - 1); // Distance between blocks in block chain
blockm = blockM / numberOfBlocks;
xID = xID_;
yID = yID_;
}
void block::fillNeigbours(vector<vector<shared_ptr<block>>> &blocks)
{
for (int m = int(yID) - 1; m <= int(yID) + 1; m++)
{
for (int n = int(xID) - 1; n <= int(xID) + 1; n++)
{
if (((m == this->yID) && (n == this->xID)))
{
}
else
{
if ((m >= 0 && m <= blockHeight -1) && (n >= 0 && n <= blockWidth -1))
{
neighbours.push_back(blocks[m][n]);
double len = lenght(xID, yID, blocks[m][n]->xID, blocks[m][n]->yID);
if (len > 0.998 && len < 1.002)
{
neighbourSpringConst.push_back(1.0);
}
else
{
neighbourSpringConst.push_back(0.5);
}
}
}
}
}
}
double block::springForce(double k, double d, double x, double y, int komponent)
{
double deltaX = x - xPos;
double deltaY = y - yPos;
double len = sqrt(pow(deltaX, 2) + pow(deltaY, 2));
if (komponent == 0)
{
return k*(len - d)*(deltaX) / len;
}
else if (komponent == 1)
{
return k*(len - d)*deltaY / len;
}
}
double block::dampningForce(double nu, double vx, double vy, int komponent)
{
double deltaX = vx - xVel;
double deltaY = vy - yVel;
double len = sqrt(pow(deltaX, 2) + pow(deltaY, 2));
if (len > 0) {
if (komponent == 0)
{
return -nu*(deltaX);
}
else if (komponent == 1)
{
return -nu*(deltaY);
}
}
else { return 0; }
}
void block::InternalForces()
{
xForce = 0;
yForce = 0;
int i = 0;
for each (shared_ptr<block> neigbour in neighbours)
{
double k_ = neighbourSpringConst[i] * k;
double nu_ = neighbourSpringConst[i] * nu;
double eps = 0.05;
double d_;
if (abs(neighbourSpringConst[i] - 0.5) < eps)
{
d_ = sqrt(2)*blockd;
}
else
{
d_ = blockd;
}
xForce += springForce(k_, d_, neigbour->xPos, neigbour->yPos, 0)-dampningForce(nu_, neigbour->xVel, neigbour->yVel, 0);
yForce += springForce(k_, d_, neigbour->xPos, neigbour->yPos, 1) -dampningForce(nu_, neigbour->xVel, neigbour->yVel, 1);
i++;
}
};
void block::integrate()
{
xVel += xForce / blockm*dt;
xPos += xVel*dt;
yVel += yForce / blockm*dt;
yPos += yVel*dt;
};
void block::calculateForces() {};