-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlock.cpp
More file actions
75 lines (58 loc) · 1.59 KB
/
Copy pathBlock.cpp
File metadata and controls
75 lines (58 loc) · 1.59 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
#include <vector>
#include <memory>
#include <cmath>
# include <iostream>
#include "Block.h"
#include "InputReader.h"
using namespace std;
Block::Block(int xID_, int yID_, InputReader* &input){
inputReader = input;
xID = xID_;
yID = yID_;
xPos = xID*inputReader->d;
yPos = yID*inputReader->d;
};
Block::~Block()
{
}
void Block::fillNeighbors(vector<shared_ptr<Block>> &blocks)
{
double dx;
double dy;
for (shared_ptr<Block> block : blocks)
{
dx = abs(block->xID - xID);
dy = abs(block->yID - yID);
if ((dx == 1 || dx == 0) && (dy == 1 || dy == 0))
{
if (!(dx == 0 && dy == 0)){
neighbors.push_back(block);
if (dx == dy)
{
springConstModifier.push_back(0.5);
eqDistModifier.push_back(sqrt(2));
}
else
{
springConstModifier.push_back(1);
eqDistModifier.push_back(1);
}
}
}
}
}
void Block::calculateForces()
{
xForce = 0;
yForce = 0;
for (int i = 0; i < neighbors.size();i++)
{
double deltaX = neighbors[i]->xPos - this->xPos;
double deltaY = neighbors[i]->yPos - this->yPos;
double length = sqrt(pow(deltaX,2) + pow(deltaY,2));
xForce += inputReader->k*springConstModifier[i]*(length - inputReader->d*eqDistModifier[i])*deltaX/length;
yForce += inputReader->k*springConstModifier[i]*(length - inputReader->d*eqDistModifier[i])*deltaY/length;
xForce += -10*inputReader->eta*springConstModifier[i]*(this->xVel - neighbors[i]->xVel);
yForce += -10*inputReader->eta*springConstModifier[i]*(this->yVel - neighbors[i]->yVel);
}
}