-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.gd
323 lines (235 loc) · 7.92 KB
/
math.gd
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
class_name Math
enum Order2 { XX, XY, XZ, YX, YY, YZ, ZX, ZY, ZZ}
enum Order3 { XYZ, XZY, YXZ, YZX, ZXY, ZYX }
class Lib:
# Bitflag checks
# Honestly, this is more of a how-to guide than a real "use these methods"
static func has_flag(flags: int, flag: int) -> bool:
return (flags & flag) != 0
static func has_all_flags(value: int, flags: int) -> bool:
return (value & flags) == flags
static func set_flag(flags: int, flag: int) -> int:
return flags | flag
static func clear_flag(flags: int, flag: int) -> int:
return flags & ~flag
static func toggle_flag(flags: int, flag: int) -> int:
return flags ^ flag
static func get_flags(value: int, n_flags: int) -> Array[bool]:
var _r := [] as Array[bool]
for i in n_flags:
_r.append(has_flag(value, 1 << i))
return _r
# Angle and vector math
# yes i know this is redundant but hey forget about it ok i have my cursed little freak reasons
static func get_vec3xz(vec: Vector3, with_slide: bool = false) -> Vector3:
return vec.slide(Vector3.UP) if with_slide else Vector3(vec.x, 0, vec.z)
static func get_vec3y(vec: Vector3) -> Vector3:
return Vector3(0, vec.y, 0)
static func embed_vec2(vec: Vector2, order: Math.Order3, flatten: bool = false) -> Vector3:
var _r := Vector3()
match order:
Order3.XYZ:
_r.x = vec.x
_r.y = vec.y
Order3.XZY:
_r.x = vec.y
_r.z = vec.x
Order3.YXZ:
_r.y = vec.x
_r.x = vec.y
Order3.YZX:
_r.y = vec.x
_r.z = vec.y
Order3.ZXY:
_r.z = vec.x
_r.x = vec.y
Order3.ZYX:
_r.z = vec.x
_r.y = vec.y
return _r
## TODO: define what order is besides ZX or signed_angle. Pseudo euler order
@warning_ignore("untyped_declaration")
static func vec_to_angle(vec, as_deg: bool = false, order: Math.Order2 = Order2.ZX) -> float:
var _r: float = NAN
if (vec is Vector2i || vec is Vector2):
_r = atan2(vec.y, vec.x)
elif (vec is Vector3 || vec is Vector3i):
match order:
#enum Order { XYZ, XZY, YXZ, YZX, ZXY, ZYX }
Order2.ZX: _r = atan2(vec.z, vec.x)
Order2.ZY: _r = atan2(vec.y, vec.x)
Order2.XY: _r = atan2(vec.z, vec.x)
Order2.XZ: _r = atan2(vec.y, vec.x)
Order2.XY: _r = atan2(vec.z, vec.x)
Order2.XZ: _r = atan2(vec.y, vec.x)
_: _r = vec.signed_angle_to(Vector3.FORWARD, Vector3.UP)
if _r != NAN:
return rad_to_deg(_r) if as_deg else _r
push_warning("Warning: Math.Lib.vec2ang was supplied with an invalid type. Valid types include Vector2/3 and Vector2i/3i.")
return _r
# TODO: make arbitrary angle calculation loop, to support N dimensional sectors, rather than 8 only
static func get_sector8(angle: float) -> int:
if (angle > -22.5 && angle < 22.6):
return 0
if (angle >= 22.5 && angle < 67.5):
return 7
if (angle >= 67.5 && angle < 112.5):
return 6
if (angle >= 112.5 && angle < 157.5):
return 5
if (angle <= -157.5 || angle >= 157.5):
return 4
if (angle >= -157.4 && angle < -112.5):
return 3
if (angle >= -112.5 && angle < -67.5):
return 2
if (angle >= -67.5 && angle <= -22.5):
return 1
return 0
static func get_sprite_dir(
sprite_xform: Transform3D,
view_xform: Transform3D,
only_xz: bool = true) -> int:
var from := sprite_xform.origin
var to := view_xform.origin
var look_dir := -sprite_xform.basis.z
if only_xz:
from.y = 0
to.y = 0
look_dir.y = 0
# get the sector angles by arctan then lock to 0 < x < 360.0
var pos := vec_to_angle(from - to, true)
var basis := vec_to_angle(from - look_dir, true)
var angle := wrap_fmod(pos, basis, 360.0)
return get_sector8(angle)
# custom ccd
static func get_shape_hull(_shape: Shape3D) -> AABB:
return _shape.get_debug_mesh().get_aabb()
# code below has roots from idTech3 and btan2's Q_Move for Godot 3.5
static func cast_trace(
what: CollisionObject3D, shape: Shape3D,
from: Vector3, to: Vector3) -> Math.Trace:
var _collided: bool = false
var motion := to - from
var params := PhysicsShapeQueryParameters3D.new()
params.set_shape(shape)
params.transform.origin = from
params.collide_with_bodies = true
params.set_motion(motion)
params.exclude = [what.get_rid()]
var space_state := what.get_world_3d().direct_space_state
var results := space_state.cast_motion(params)
if results[0] == 1.0:
_collided = false
return Math.Trace.new(to)
_collided = true
var end_pos := from + motion * results[1]
params.transform.origin = end_pos
var rest := space_state.get_rest_info(params)
var norm := rest.get("normal", Vector3.UP) as Vector3
return Math.Trace.new(end_pos, results[0], norm)
static func blast_radius(who: Node3D, radius: float) -> Array[Dictionary]:
var shape_rid = PhysicsServer3D.sphere_shape_create()
PhysicsServer3D.shape_set_data(shape_rid, radius)
var params = PhysicsShapeQueryParameters3D.new()
params.shape_rid = shape_rid
params.collide_with_bodies = true
params.collide_with_areas = false
params.transform = who.global_transform
PhysicsServer3D.free_rid.bind(shape_rid).call_deferred()
return who.get_world_3d().direct_space_state.intersect_shape(params)
# misc funcs
static func short_path(path: String, delim: String, pad: int = 0) -> String:
var split := path.split(delim)
var _r: String = ""
for s in split:
_r += (s if s == split[-1] else s[0] + delim)
if pad > 0:
for i in (pad - _r.length()):
_r += " "
return _r
static func get_cam_info(camera: Camera3D, excludes: Array[CollisionObject3D] = [null], layers: int = 0xFFFFFFFF) -> CamInfo:
return CamInfo.new(camera, layers, excludes)
static func get_dir() -> String:
if Engine.is_editor_hint() || OS.has_feature("editor"):
return "res:/"
else:
return OS.get_executable_path().get_base_dir()
@warning_ignore("shadowed_global_identifier")
static func wrap_fmod(x: float, y: float, range: float) -> float:
return fmod(x - y + range, range)
## data objects
# TODO: add surface flag data in player collision checks
class Trace extends RefCounted:
var end_pos: Vector3
var fraction: float
var normal: Vector3
var surface_flags: Array
@warning_ignore("shadowed_variable")
func _init(end_pos: Vector3, fraction: float = 1.0, normal: Vector3 = Vector3.UP) -> void:
self.end_pos = end_pos
self.fraction = fraction
self.normal = normal
return
class CamInfo extends RefCounted:
var cam_3d: Camera3D:
set(v):
cam_3d = v
viewport = v.get_viewport()
world_3d = v.get_world_3d()
projection = v.get_camera_projection()
ray_origin = v.project_ray_origin(screen_size / 2)
ray_end = ray_origin + v.project_ray_normal(screen_size / 2) * 1000
var projection: Projection:
set(v):
projection = v
fov_x = projection.get_fov()
aspect = projection.get_aspect()
fov_y = Projection.get_fovy(fov_x, aspect)
var fov_x: float
var fov_y: float
var aspect: float
var screen_size: Vector2
var world_3d: World3D
var ray_origin: Vector3
var ray_end: Vector3
var viewport: Viewport:
set(v):
viewport = v
if v is SubViewport:
screen_size = (v as SubViewport).size
else:
screen_size = Vector2i(
ProjectSettings.get_setting("size/viewport_width"),
ProjectSettings.get_setting("size/viewport_height")
)
var params: PhysicsRayQueryParameters3D
var collided: bool = false
var collider: Node = null
var collision_point: Vector3
var collision_normal: Vector3
func _init(
_camera: Camera3D, layers: int = 0xFFFFFFFF, excludes: Array[CollisionObject3D] = [null]) -> void:
cam_3d = _camera
var exc: Array[RID] = []
for node in excludes:
if node:
exc.append(node.get_rid())
params = PhysicsRayQueryParameters3D.create(
ray_origin,
ray_end,
layers,
exc
)
var results := world_3d.direct_space_state.intersect_ray(params)
if !results.is_empty():
collided = true
collider = results.collider
collision_point = results.position
collision_normal = results.normal
else:
collided = false
collider = null
collision_point = ray_end
collision_normal = (ray_end - ray_origin).normalized()
return