-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvectors.c
More file actions
70 lines (54 loc) · 1.5 KB
/
Copy pathvectors.c
File metadata and controls
70 lines (54 loc) · 1.5 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
#include <stdint.h>
#include "vectors.h"
#include <math.h>
vec3_t vec3_normalize(const vec3_t v) {
const float length_sq = v.x * v.x + v.y * v.y + v.z * v.z;
if (length_sq == 0.0f) {
return (vec3_t){0.0f, 0.0f, 0.0f};
}
const float inv_length = fast_inverse_sqrt(length_sq);
return (vec3_t) {
v.x * inv_length,
v.y * inv_length,
v.z * inv_length
};
}
float fast_inverse_sqrt(const float x) {
union {
float f;
int32_t i;
} conv;
conv.f = x;
conv.i = 0x5f3759df - (conv.i >> 1);
float y = conv.f;
return y * (1.5f - 0.5f * x * y * y);
}
vec3_t vec3_cross(const vec3_t v1, const vec3_t v2) {
return (vec3_t) {
v1.y * v2.z - v1.z * v2.y,
v1.z * v2.x - v1.x * v2.z,
v1.x * v2.y - v1.y * v2.x
};
}
float vec3_dot(const vec3_t v1, const vec3_t v2) {
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}
void vec3_floor_xy(vec3_t *v) {
v->x = floorf(v->x);
v->y = floorf(v->y);
}
vec3_t vec3_diff(const vec3_t v1, const vec3_t v2) {
return (vec3_t){v1.x - v2.x, v1.y - v2.y, v1.z - v2.z};
}
vec3_t vec3_add(const vec3_t v1, const vec3_t v2) {
return (vec3_t){v1.x + v2.x, v1.y + v2.y, v1.z + v2.z};
}
vec3_t vec3_mul(const vec3_t v, const float s) {
return (vec3_t){v.x * s, v.y * s, v.z * s};
}
vec2_t vec2_diff(const vec2_t v1, const vec2_t v2) {
return (vec2_t){v1.x - v2.x, v1.y - v2.y};
}
vec2_t vec2_from_vec3(const vec3_t v) {
return (vec2_t){v.x, v.y};
}