-
Notifications
You must be signed in to change notification settings - Fork 0
/
rails.cpp
142 lines (118 loc) · 4.98 KB
/
rails.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
#include "rails.hpp"
#include "helper.hpp"
#include <iostream>
#define _USE_MATH_DEFINES
#include <math.h>
#include <cassert>
namespace loopline
{
Rails::Rails(std::vector<sf::Vector2f> const &pts, Pathfinding path)
: path(path)
{
setControlPoints(pts);
}
Rails::~Rails()
{
}
void Rails::setControlPoints(std::vector<sf::Vector2f> const &pts)
{
controlPoints = pts;
railLengths.empty();
railLengths.resize(controlPoints.size());
auto controlDiff = controlPoints[0] - controlPoints[controlPoints.size() - 1];
float railLength = 0.f;
for(unsigned int i=1; i<controlPoints.size(); i++)
{
auto controlDiff = controlPoints[i] - controlPoints[i-1];
railLength += sqrtf(powf(controlDiff.x, 2.0f) + powf(controlDiff.y, 2.0f));
railLengths[i-1] = railLength;
}
controlDiff = controlPoints[0] - controlPoints[controlPoints.size() - 1];
railLength += sqrtf(powf(controlDiff.x, 2.0f) + powf(controlDiff.y, 2.0f));
railLengths[controlPoints.size()-1] = railLength;
}
sf::Vector2f Rails::getWorldPosition(float railPosition) const
{
sf::Vector2f worldPos;
railPosition = fmod(railPosition, railLengths[railLengths.size() - 1]);
int previouspreviousControl = railLengths.size() - 1;
int previousControl = 0;
int nextControl = 1;
int nextnextControl = 2;
float railPositionDiscard = 0.f;
while (railPosition > railLengths[previousControl])
{
railPositionDiscard = railLengths[previousControl];
previousControl = (previousControl + 1) % railLengths.size();
nextControl = (nextControl + 1) % railLengths.size();
previouspreviousControl = (previouspreviousControl + 1) % railLengths.size();
nextnextControl = (nextnextControl + 1) % railLengths.size();
}
float multiplier = (railPosition - railPositionDiscard) / (railLengths[previousControl] - railPositionDiscard);
switch (path)
{
case CUBIC:
worldPos = CubicInterpolate(controlPoints[previouspreviousControl], controlPoints[previousControl], controlPoints[nextControl], controlPoints[nextnextControl], multiplier);
break;
case LINEAR:
worldPos = multiplier * controlPoints[nextControl] + (1.f - multiplier) * controlPoints[previousControl];
break;
}
return worldPos;
}
void Rails::update(sf::Time const &deltaTime)
{
for(auto& train : trains)
{
train.update(deltaTime);
train.railPosition = fmod(train.railPosition, railLengths[railLengths.size() - 1]);
auto firstTrainPoint = getWorldPosition(train.railPosition);
auto secondTrainPoint = getWorldPosition(train.railPosition - train.length);
auto diff = firstTrainPoint - secondTrainPoint;
float length = sqrtf(powf(diff.x, 2.0f) + powf(diff.y, 2.0f));
diff /= length;
float rotationAngle = atan2f(diff.y, diff.x);
float rotationAngleDeg = rotationAngle / M_PI * 180.f;
if (rotationAngleDeg > 90.f || rotationAngleDeg < -90.f)
train.setSpriteScale({1.f, -1.f});
else
train.setSpriteScale({1.f, 1.f});
train.setSpritePosition(firstTrainPoint);
train.setSpriteRotation(rotationAngleDeg);
float wagonRailPosition = train.railPosition - train.length;
for(auto& wagon : train.wagons)
{
if(wagonRailPosition < 0.f) wagonRailPosition += railLengths[railLengths.size() - 1];
wagonRailPosition = fmod(wagonRailPosition, railLengths[railLengths.size() - 1]);
auto firstTrainPoint = getWorldPosition(wagonRailPosition);
auto secondTrainPoint = getWorldPosition(wagonRailPosition - train.length);
auto diff = firstTrainPoint - secondTrainPoint;
float length = sqrtf(powf(diff.x, 2.0f) + powf(diff.y, 2.0f));
diff /= length;
float rotationAngle = atan2f(diff.y, diff.x);
float rotationAngleDeg = rotationAngle / M_PI * 180.f;
if (rotationAngleDeg > 90.f || rotationAngleDeg < -90.f)
wagon.setSpriteScale({1.f, -1.f});
else
wagon.setSpriteScale({1.f, 1.f});
wagon.setSpritePosition(firstTrainPoint);
wagon.setSpriteRotation(rotationAngleDeg);
wagonRailPosition = wagonRailPosition - wagon.length;
}
}
}
void Rails::fixedUpdate(sf::Time const &deltaTime)
{
for(auto& train : trains)
{
train.fixedUpdate(deltaTime);
}
}
void Rails::draw(sf::RenderWindow &window) const
{
for(auto& train : trains)
{
train.draw(window);
}
}
} // namespace loopline