ScummVM API documentation
vector.h
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 modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (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, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#ifndef BLADERUNNER_VECTOR_H
23#define BLADERUNNER_VECTOR_H
24
25#include "common/types.h"
26
27namespace BladeRunner {
28
29class Vector2 {
30public:
31 float x;
32 float y;
33
34 Vector2() : x(0.0), y(0.0) {}
35
36 Vector2(float ax, float ay) : x(ax), y(ay) {}
37};
38
39inline bool operator==(const Vector2 &a, const Vector2 &b) {
40 return a.x == b.x && a.y == b.y;
41}
42
43inline bool operator!=(const Vector2 &a, const Vector2 &b) {
44 return !(a == b);
45}
46
47class Vector3 {
48public:
49 float x;
50 float y;
51 float z;
52
53 Vector3() : x(0.0f), y(0.0f), z(0.0f) {}
54
55 Vector3(float ax, float ay, float az) : x(ax), y(ay), z(az) {}
56
57 inline float length() {
58 return sqrt(x * x + y * y + z * z);
59 }
60
61 inline Vector3 normalize() {
62 float len = length();
63 if (len == 0) {
64 return Vector3(0.0f, 0.0f, 0.0f);
65 }
66 return Vector3(x / len, y / len, z / len);
67 }
68
69 inline static Vector3 cross(Vector3 a, Vector3 b) {
70 return Vector3(
71 a.y * b.z - a.z * b.y,
72 a.z * b.x - a.x * b.z,
73 a.x * b.y - a.y * b.x);
74 }
75
76 inline static float dot(Vector3 a, Vector3 b) {
77 return a.x * b.x + a.y * b.y + a.z * b.z;
78 }
79
80 Vector2 xz() const {
81 return Vector2(x, z);
82 }
83};
84
85inline Vector3 operator+(Vector3 a, Vector3 b) {
86 return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
87}
88
89inline Vector3 operator-(Vector3 a, Vector3 b) {
90 return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
91}
92
93inline Vector3 operator*(float f, Vector3 v) {
94 return Vector3(f * v.x, f * v.y, f * v.z);
95}
96
97class Vector4 {
98public:
99 float x;
100 float y;
101 float z;
102 float w;
103
104 Vector4() : x(0.0), y(0.0), z(0.0), w(0.0) {}
105
106 Vector4(float ax, float ay, float az, float aw) : x(ax), y(ay), z(az), w(aw) {}
107};
108
109inline Vector4 operator+(Vector4 a, Vector4 b) {
110 return Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
111}
112
113inline Vector4 operator-(Vector4 a, Vector4 b) {
114 return Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
115}
116
117inline Vector4 operator*(float f, Vector4 v) {
118 return Vector4(f * v.x, f * v.y, f * v.z, f * v.w);
119}
120
121inline Vector4 operator*(Vector4 v, float f) {
122 return Vector4(f * v.x, f * v.y, f * v.z, f * v.w);
123}
124
125inline Vector4 operator/(Vector4 a, Vector4 b) {
126 return Vector4(a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w);
127}
128
129inline int angle_1024(float x1, float z1, float x2, float z2) {
130 float angle_rad = atan2(x2 - x1, z1 - z2);
131 int a = int(512.0 * angle_rad / M_PI);
132 return (a + 1024) % 1024;
133}
134
135inline int angle_1024(const Vector3 &v1, const Vector3 &v2) {
136 return angle_1024(v1.x, v1.z, v2.x, v2.z);
137}
138
139inline float distance(float x1, float z1, float x2, float z2) {
140 float dx = x1 - x2;
141 float dz = z1 - z2;
142 float d = sqrt(dx * dx + dz * dz);
143
144 float int_part = (int)d;
145 float frac_part = d - int_part;
146
147 if (frac_part < 0.001)
148 frac_part = 0.0;
149
150 return int_part + frac_part;
151}
152
153inline float distance(const Vector2 &v1, const Vector2 &v2) {
154 return distance(v1.x, v1.y, v2.x, v2.y);
155}
156
157inline float distance(const Vector3 &v1, const Vector3 &v2) {
158 return distance(v1.x, v1.z, v2.x, v2.z);
159}
160
161inline bool lineIntersection(Vector2 a1, Vector2 a2, Vector2 b1, Vector2 b2, Vector2 *intersection) {
162 Vector2 s1(a2.x - a1.x, a2.y - a1.y);
163 Vector2 s2(b2.x - b1.x, b2.y - b1.y);
164
165 float s = (s1.x * (a1.y - b1.y) - s1.y * (a1.x - b1.x)) / (s1.x * s2.y - s2.x * s1.y);
166 float t = (s2.x * (a1.y - b1.y) - s2.y * (a1.x - b1.x)) / (s1.x * s2.y - s2.x * s1.y);
167
168 if (s >= 0.0f && s <= 1.0f && t >= 0.0f && t <= 1.0f) {
169 intersection->x = a1.x + (t * s1.x);
170 intersection->y = a1.y + (t * s1.y);
171 return true;
172 }
173
174 return false; // No collision
175}
176
177} // End of namespace BladeRunner
178
179#endif
Definition: vector.h:29
Definition: vector.h:47
Definition: vector.h:97