-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathVector2.cpp
197 lines (179 loc) · 7.2 KB
/
Vector2.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <onut/Matrix.h>
#include <onut/Point.h>
#include <onut/Vector2.h>
#include <onut/Vector3.h>
const Vector2 Vector2::Zero(0.f, 0.f);
const Vector2 Vector2::One(1.f, 1.f);
const Vector2 Vector2::UnitX(1.f, 0.f);
const Vector2 Vector2::UnitY(0.f, 1.f);
Vector2::Vector2(const Vector3& v3) : x(v3.x), y(v3.y)
{
}
void Vector2::Cross(const Vector2& V, Vector3& result) const
{
result = Vector3(0, 0, (x * V.y) - (y * V.x));
}
Vector3 Vector2::Cross(const Vector2& V) const
{
return Vector3(0, 0, (x * V.y) - (y * V.x));
}
float Vector2::Angle(const Vector2& from, const Vector2& to)
{
if (from == to) return 0.0f;
auto dir = to - from;
auto rad = std::atan2(dir.y, dir.x);
return rad;
}
void Vector2::Transform(const Vector2& v, const Matrix& m, Vector2& result)
{
float w = 0.0f;
result.x = (m._11 * v.x) + (m._21 * v.y) + m._41;
result.y = (m._12 * v.x) + (m._22 * v.y) + m._42;
w = (m._14 * v.x) + (m._24 * v.y) + m._44;
result /= w;
}
Vector2 Vector2::Transform(const Vector2& v, const Matrix& m)
{
Vector2 result;
float w = 0.0f;
result.x = (m._11 * v.x) + (m._21 * v.y) + m._41;
result.y = (m._12 * v.x) + (m._22 * v.y) + m._42;
w = (m._14 * v.x) + (m._24 * v.y) + m._44;
result /= w;
return result / w;
}
void Vector2::TransformNormal(const Vector2& v, const Matrix& m, Vector2& result)
{
result.x = (m._11 * v.x) + (m._21 * v.y) + m._41;
result.y = (m._12 * v.x) + (m._22 * v.y) + m._42;
}
Vector2 Vector2::TransformNormal(const Vector2& v, const Matrix& m)
{
Vector2 result;
result.x = (m._11 * v.x) + (m._21 * v.y) + m._41;
result.y = (m._12 * v.x) + (m._22 * v.y) + m._42;
return result;
}
namespace onut
{
static bool walkableAt(const Vector2& pos, bool* pPassableTiles, int width, int height, float tileSizef)
{
Point mapPos((int)(pos.x / tileSizef), (int)(pos.y / tileSizef));
if (mapPos.x < 0 || mapPos.y < 0 || mapPos.x >= width || mapPos.y >= height) return false;
return pPassableTiles[mapPos.y * width + mapPos.x];
}
static bool walkableAt(const Vector2& pos, float* pPassableTiles, int width, int height, float tileSizef)
{
Point mapPos((int)(pos.x / tileSizef), (int)(pos.y / tileSizef));
if (mapPos.x < 0 || mapPos.y < 0 || mapPos.x >= width || mapPos.y >= height) return false;
return pPassableTiles[mapPos.y * width + mapPos.x] > 0.0f;
}
Vector2 tilesCollision(const Vector2& from, const Vector2& to, const Vector2& size, bool* pPassableTiles, int width, int height, int tileSize)
{
if (to == from) return to;
float tileSizef = (float)tileSize;
Point lastMapPos((int)(from.x / tileSizef), (int)(from.y / tileSizef));
Vector2 dir = to - from;
Vector2 result = to;
Vector2 hSize = size / 2.0f;
if (dir.x < 0)
{
if (!walkableAt(Vector2(to.x - hSize.x, from.y - hSize.y), pPassableTiles, width, height, tileSizef))
{
result.x = (float)(lastMapPos.x) * tileSizef + hSize.x;
}
else if (!walkableAt(Vector2(to.x - hSize.x, from.y + hSize.y), pPassableTiles, width, height, tileSizef))
{
result.x = (float)(lastMapPos.x) * tileSizef + hSize.x;
}
}
else if (dir.x > 0)
{
if (!walkableAt(Vector2(to.x + hSize.x, from.y - hSize.y), pPassableTiles, width, height, tileSizef))
{
result.x = (float)(lastMapPos.x + 1) * tileSizef - hSize.x - (tileSizef / 100.0f);
}
else if (!walkableAt(Vector2(to.x + hSize.x, from.y + hSize.y), pPassableTiles, width, height, tileSizef))
{
result.x = (float)(lastMapPos.x + 1) * tileSizef - hSize.x - (tileSizef / 100.0f);
}
}
if (dir.y < 0)
{
if (!walkableAt(Vector2(from.x - hSize.x, to.y - hSize.y), pPassableTiles, width, height, tileSizef))
{
result.y = (float)(lastMapPos.y) * tileSizef + hSize.y;
}
else if (!walkableAt(Vector2(from.x + hSize.x, to.y - hSize.y), pPassableTiles, width, height, tileSizef))
{
result.y = (float)(lastMapPos.y) * tileSizef + hSize.y;
}
}
else if (dir.y > 0)
{
if (!walkableAt(Vector2(from.x - hSize.x, to.y + hSize.y), pPassableTiles, width, height, tileSizef))
{
result.y = (float)(lastMapPos.y + 1) * tileSizef - hSize.y - (tileSizef / 100.0f);
}
else if (!walkableAt(Vector2(from.x + hSize.x, to.y + hSize.y), pPassableTiles, width, height, tileSizef))
{
result.y = (float)(lastMapPos.y + 1) * tileSizef - hSize.y - (tileSizef / 100.0f);
}
}
return result;
}
Vector2 tilesCollision(const Vector2& from, const Vector2& to, const Vector2& size, float* pPassableTiles, int width, int height, int tileSize)
{
if (to == from) return to;
float tileSizef = (float)tileSize;
Point lastMapPos((int)(from.x / tileSizef), (int)(from.y / tileSizef));
Vector2 dir = to - from;
Vector2 result = to;
Vector2 hSize = size / 2.0f;
if (dir.x < 0)
{
if (!walkableAt(Vector2(to.x - hSize.x, from.y - hSize.y), pPassableTiles, width, height, tileSizef))
{
result.x = (float)(lastMapPos.x) * tileSizef + hSize.x;
}
else if (!walkableAt(Vector2(to.x - hSize.x, from.y + hSize.y), pPassableTiles, width, height, tileSizef))
{
result.x = (float)(lastMapPos.x) * tileSizef + hSize.x;
}
}
else if (dir.x > 0)
{
if (!walkableAt(Vector2(to.x + hSize.x, from.y - hSize.y), pPassableTiles, width, height, tileSizef))
{
result.x = (float)(lastMapPos.x + 1) * tileSizef - hSize.x - (tileSizef / 100.0f);
}
else if (!walkableAt(Vector2(to.x + hSize.x, from.y + hSize.y), pPassableTiles, width, height, tileSizef))
{
result.x = (float)(lastMapPos.x + 1) * tileSizef - hSize.x - (tileSizef / 100.0f);
}
}
if (dir.y < 0)
{
if (!walkableAt(Vector2(from.x - hSize.x, to.y - hSize.y), pPassableTiles, width, height, tileSizef))
{
result.y = (float)(lastMapPos.y) * tileSizef + hSize.y;
}
else if (!walkableAt(Vector2(from.x + hSize.x, to.y - hSize.y), pPassableTiles, width, height, tileSizef))
{
result.y = (float)(lastMapPos.y) * tileSizef + hSize.y;
}
}
else if (dir.y > 0)
{
if (!walkableAt(Vector2(from.x - hSize.x, to.y + hSize.y), pPassableTiles, width, height, tileSizef))
{
result.y = (float)(lastMapPos.y + 1) * tileSizef - hSize.y - (tileSizef / 100.0f);
}
else if (!walkableAt(Vector2(from.x + hSize.x, to.y + hSize.y), pPassableTiles, width, height, tileSizef))
{
result.y = (float)(lastMapPos.y + 1) * tileSizef - hSize.y - (tileSizef / 100.0f);
}
}
return result;
}
};