-
Notifications
You must be signed in to change notification settings - Fork 0
/
phys_ray.c
190 lines (171 loc) · 5.12 KB
/
phys_ray.c
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
#include "phys_ray.h"
#include "core/core_data.h"
#include "phys/phys_types.h"
#include "phys_world.h"
#include "phys_collision.h"
#include "phys_debug_draw.h"
#include "stb/stb_ds.h"
// @TODO: @OPTIMIZE: optimize this, chunks
bool phys_ray_cast_dbg(ray_t* ray, ray_hit_t* out, const char* _file, const char* _func, const int _line)
{
(void)_file; (void)_func; (void)_line;
u32 len = 0;
phys_obj_t* arr = phys_get_obj_arr(&len);
ray_hit_t* hit_arr = NULL;
u32 hit_arr_len = 0;
for (int i = 0; i < (int)len; ++i)
{
phys_obj_t* obj = &arr[i];
if (ray->mask_arr && ray->mask_arr_len > 0)
{
bool skip = false;
for (int m = 0; m < ray->mask_arr_len; ++m)
{ if (obj->entity_idx == ray->mask_arr[m]) { skip = true; break; } }
if (skip) { continue; }
}
if (!PHYS_OBJ_HAS_COLLIDER(obj) || obj->collider.is_trigger) { continue; }
// f32 dist = 0;
// vec3 hit_point;
ray_hit_t hit;
switch (obj->collider.type)
{
case PHYS_COLLIDER_SPHERE:
if ( phys_collision_check_ray_v_sphere_obj(ray, obj, &hit) ) // &dist, hit_point) )
{
hit.entity_idx = obj->entity_idx,
arrput(hit_arr, hit);
hit_arr_len++;
}
break;
case PHYS_COLLIDER_BOX:
if ( phys_collision_check_ray_v_aabb_obj(ray, obj, &hit) ) // &dist, hit_point) )
{
hit.entity_idx = obj->entity_idx,
arrput(hit_arr, hit);
hit_arr_len++;
}
break;
}
}
// no hits
if (hit_arr_len <= 0) { goto no_hit_exit; }
// get closest hit
int idx = 0;
for (int i = 0; i < (int)hit_arr_len; ++i)
{
if (hit_arr[i].dist < hit_arr[idx].dist) { idx = i; }
}
// set out to hit
*out = hit_arr[idx];
// if len <= 0 ignore len
out->hit = ray->len <= 0.0f || out->dist <= ray->len;
if (!out->hit) { goto no_hit_exit; }
// debug lines on hit
if (ray->draw_debug)
{
debug_draw_line_t(ray->pos, hit_arr[idx].hit_point, RGB_F(0, 1, 1), 1.0f);
debug_draw_sphere_t(hit_arr[idx].hit_point, 0.1f, RGB_F(0, 1, 0), 1.0f);
vec3 norm;
vec3_copy(hit_arr[idx].hit_point, norm);
vec3_add(norm, hit_arr[idx].normal, norm);
debug_draw_line_t(hit_arr[idx].hit_point, norm, RGB_F(0, 1, 0), 1.0f);
}
ARRFREE(hit_arr);
return out->hit;
no_hit_exit:;
if (ray->draw_debug)
{
vec3 ray_end;
if (ray->len <= 0.0f)
{ vec3_mul_f(ray->dir, 25, ray_end); }
else
{ vec3_mul_f(ray->dir, ray->len, ray_end); }
vec3_add(ray_end, ray->pos, ray_end);
debug_draw_line(ray->pos, ray_end, RGB_F(1, 0, 0));
}
return false;
}
// // @TODO: @OPTIMIZE: optimize this, chunks
// bool phys_ray_cast_mask_dbg(ray_t* ray, ray_hit_t* out, u32* mask_arr, int mask_arr_len, const char* _file, const char* _func, const int _line)
// {
// u32 len = 0;
// phys_obj_t* arr = phys_get_obj_arr(&len);
//
//
// ray_hit_t* hit_arr = NULL;
// u32 hit_arr_len = 0;
//
// for (int i = 0; i < len; ++i)
// {
// phys_obj_t* obj = &arr[i];
//
// bool skip = false;
// for (int m = 0; m < mask_arr_len; ++m)
// { if (obj->entity_idx == mask_arr[m]) { skip = true; break; } }
// if (skip) { continue; }
//
// if (!PHYS_OBJ_HAS_COLLIDER(obj) || obj->collider.is_trigger) { continue; }
//
// // f32 dist = 0;
// // vec3 hit_point;
// ray_hit_t hit;
// switch (obj->collider.type)
// {
// case PHYS_COLLIDER_SPHERE:
// if ( phys_collision_check_ray_v_sphere_obj(ray, obj, &hit) ) // &dist, hit_point) )
// {
// hit.entity_idx = obj->entity_idx,
// arrput(hit_arr, hit);
// hit_arr_len++;
// }
// break;
//
// case PHYS_COLLIDER_BOX:
// if ( phys_collision_check_ray_v_aabb_obj(ray, obj, &hit) ) // &dist, hit_point) )
// {
// hit.entity_idx = obj->entity_idx,
// arrput(hit_arr, hit);
// hit_arr_len++;
// }
// break;
// }
// }
//
// // no hits
// if (hit_arr_len <= 0) { goto no_hit_exit; }
//
// // get closest hit
// int idx = 0;
// for (int i = 0; i < hit_arr_len; ++i)
// {
// if (hit_arr[i].dist < hit_arr[idx].dist) { idx = i; }
// }
//
// // set out to hit
// *out = hit_arr[idx];
// // if len <= 0 ignore len
// out->hit = ray->len <= 0.0f || out->dist <= ray->len;
// if (!out->hit) { goto no_hit_exit; }
//
// // debug lines on hit
// debug_draw_line_register_t(ray->pos, hit_arr[idx].hit_point, RGB_F(0, 1, 1), 1.0f);
// debug_draw_sphere_register_t(hit_arr[idx].hit_point, 0.1f, RGB_F(0, 1, 0), 1.0f);
// vec3 norm;
// vec3_copy(hit_arr[idx].hit_point, norm);
// vec3_add(norm, hit_arr[idx].normal, norm);
// debug_draw_line_register_t(hit_arr[idx].hit_point, norm, RGB_F(0, 1, 0), 1.0f);
//
// ARRFREE(hit_arr);
//
// return out->hit;
//
// no_hit_exit:;
// vec3 ray_end;
// if (ray->len <= 0.0f)
// { vec3_mul_f(ray->dir, 25, ray_end); }
// else
// { vec3_mul_f(ray->dir, ray->len, ray_end); }
// vec3_add(ray_end, ray->pos, ray_end);
// debug_draw_line_register(ray->pos, ray_end, RGB_F(1, 0, 0));
// return false;
// }