1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 #ifndef BLADERUNNER_VECTOR_H
24 #define BLADERUNNER_VECTOR_H
25
26 #include "common/types.h"
27
28 namespace BladeRunner {
29
30 class Vector2 {
31 public:
32 float x;
33 float y;
34
Vector2()35 Vector2() : x(0.0), y(0.0) {}
36
Vector2(float ax,float ay)37 Vector2(float ax, float ay) : x(ax), y(ay) {}
38 };
39
40 inline bool operator==(const Vector2 &a, const Vector2 &b) {
41 return a.x == b.x && a.y == b.y;
42 }
43
44 inline bool operator!=(const Vector2 &a, const Vector2 &b) {
45 return !(a == b);
46 }
47
48 class Vector3 {
49 public:
50 float x;
51 float y;
52 float z;
53
Vector3()54 Vector3() : x(0.0f), y(0.0f), z(0.0f) {}
55
Vector3(float ax,float ay,float az)56 Vector3(float ax, float ay, float az) : x(ax), y(ay), z(az) {}
57
length()58 inline float length() {
59 return sqrt(x * x + y * y + z * z);
60 }
61
normalize()62 inline Vector3 normalize() {
63 float len = length();
64 if (len == 0) {
65 return Vector3(0.0f, 0.0f, 0.0f);
66 }
67 return Vector3(x / len, y / len, z / len);
68 }
69
cross(Vector3 a,Vector3 b)70 inline static Vector3 cross(Vector3 a, Vector3 b) {
71 return Vector3(
72 a.y * b.z - a.z * b.y,
73 a.z * b.x - a.x * b.z,
74 a.x * b.y - a.y * b.x);
75 }
76
dot(Vector3 a,Vector3 b)77 inline static float dot(Vector3 a, Vector3 b) {
78 return a.x * b.x + a.y * b.y + a.z * b.z;
79 }
80
xz()81 Vector2 xz() const {
82 return Vector2(x, z);
83 }
84 };
85
86 inline Vector3 operator+(Vector3 a, Vector3 b) {
87 return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
88 }
89
90 inline Vector3 operator-(Vector3 a, Vector3 b) {
91 return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
92 }
93
94 inline Vector3 operator*(float f, Vector3 v) {
95 return Vector3(f * v.x, f * v.y, f * v.z);
96 }
97
98 class Vector4 {
99 public:
100 float x;
101 float y;
102 float z;
103 float w;
104
Vector4()105 Vector4() : x(0.0), y(0.0), z(0.0), w(0.0) {}
106
Vector4(float ax,float ay,float az,float aw)107 Vector4(float ax, float ay, float az, float aw) : x(ax), y(ay), z(az), w(aw) {}
108 };
109
110 inline Vector4 operator+(Vector4 a, Vector4 b) {
111 return Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
112 }
113
114 inline Vector4 operator-(Vector4 a, Vector4 b) {
115 return Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
116 }
117
118 inline Vector4 operator*(float f, Vector4 v) {
119 return Vector4(f * v.x, f * v.y, f * v.z, f * v.w);
120 }
121
122 inline Vector4 operator*(Vector4 v, float f) {
123 return Vector4(f * v.x, f * v.y, f * v.z, f * v.w);
124 }
125
126 inline Vector4 operator/(Vector4 a, Vector4 b) {
127 return Vector4(a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w);
128 }
129
angle_1024(float x1,float z1,float x2,float z2)130 inline int angle_1024(float x1, float z1, float x2, float z2) {
131 float angle_rad = atan2(x2 - x1, z1 - z2);
132 int a = int(512.0 * angle_rad / M_PI);
133 return (a + 1024) % 1024;
134 }
135
angle_1024(const Vector3 & v1,const Vector3 & v2)136 inline int angle_1024(const Vector3 &v1, const Vector3 &v2) {
137 return angle_1024(v1.x, v1.z, v2.x, v2.z);
138 }
139
distance(float x1,float z1,float x2,float z2)140 inline float distance(float x1, float z1, float x2, float z2) {
141 float dx = x1 - x2;
142 float dz = z1 - z2;
143 float d = sqrt(dx * dx + dz * dz);
144
145 float int_part = (int)d;
146 float frac_part = d - int_part;
147
148 if (frac_part < 0.001)
149 frac_part = 0.0;
150
151 return int_part + frac_part;
152 }
153
distance(const Vector2 & v1,const Vector2 & v2)154 inline float distance(const Vector2 &v1, const Vector2 &v2) {
155 return distance(v1.x, v1.y, v2.x, v2.y);
156 }
157
distance(const Vector3 & v1,const Vector3 & v2)158 inline float distance(const Vector3 &v1, const Vector3 &v2) {
159 return distance(v1.x, v1.z, v2.x, v2.z);
160 }
161
lineIntersection(Vector2 a1,Vector2 a2,Vector2 b1,Vector2 b2,Vector2 * intersection)162 inline bool lineIntersection(Vector2 a1, Vector2 a2, Vector2 b1, Vector2 b2, Vector2 *intersection) {
163 Vector2 s1(a2.x - a1.x, a2.y - a1.y);
164 Vector2 s2(b2.x - b1.x, b2.y - b1.y);
165
166 float s = (s1.x * (a1.y - b1.y) - s1.y * (a1.x - b1.x)) / (s1.x * s2.y - s2.x * s1.y);
167 float t = (s2.x * (a1.y - b1.y) - s2.y * (a1.x - b1.x)) / (s1.x * s2.y - s2.x * s1.y);
168
169 if (s >= 0.0f && s <= 1.0f && t >= 0.0f && t <= 1.0f) {
170 intersection->x = a1.x + (t * s1.x);
171 intersection->y = a1.y + (t * s1.y);
172 return true;
173 }
174
175 return false; // No collision
176 }
177
178 } // End of namespace BladeRunner
179
180 #endif
181