2020-01-28 00:31:10 +01:00
|
|
|
# Math and vector utility functions.
|
2020-04-03 02:31:59 +02:00
|
|
|
# @Category - Utilities
|
2020-02-11 18:33:25 +01:00
|
|
|
class_name GSAIUtils
|
2019-12-19 20:04:08 +01:00
|
|
|
|
2020-01-28 00:31:10 +01:00
|
|
|
# Returns the `vector` with its length capped to `limit`.
|
2019-12-21 03:32:21 +01:00
|
|
|
static func clampedv3(vector: Vector3, limit: float) -> Vector3:
|
2020-01-16 09:44:44 +01:00
|
|
|
var length_squared := vector.length_squared()
|
|
|
|
var limit_squared := limit * limit
|
2019-12-23 17:38:27 +01:00
|
|
|
if length_squared > limit_squared:
|
|
|
|
vector *= sqrt(limit_squared / length_squared)
|
2019-12-19 20:04:08 +01:00
|
|
|
return vector
|
2020-01-27 18:57:51 +01:00
|
|
|
|
2020-01-28 00:31:10 +01:00
|
|
|
# Returns an angle in radians between the positive X axis and the `vector`.
|
|
|
|
#
|
2020-02-13 09:33:03 +01:00
|
|
|
# This assumes orientation for 3D agents that are upright and rotate
|
|
|
|
# around the Y axis.
|
2020-01-29 16:04:47 +01:00
|
|
|
static func vector3_to_angle(vector: Vector3) -> float:
|
2020-02-13 09:33:03 +01:00
|
|
|
return atan2(vector.x, vector.z)
|
|
|
|
|
|
|
|
# Returns an angle in radians between the positive X axis and the `vector`.
|
|
|
|
static func vector2_to_angle(vector: Vector2) -> float:
|
2020-01-27 18:57:51 +01:00
|
|
|
return atan2(vector.x, -vector.y)
|
2020-01-29 16:04:47 +01:00
|
|
|
|
|
|
|
# Returns a directional vector from the given orientation angle.
|
|
|
|
#
|
|
|
|
# This assumes orientation for 2D agents or 3D agents that are upright and
|
|
|
|
# rotate around the Y axis.
|
|
|
|
static func angle_to_vector2(angle: float) -> Vector2:
|
|
|
|
return Vector2(sin(-angle), cos(angle))
|
2020-02-06 20:54:12 +01:00
|
|
|
|
|
|
|
# Returns a vector2 with `vector`'s x and y components.
|
|
|
|
static func to_vector2(vector: Vector3) -> Vector2:
|
|
|
|
return Vector2(vector.x, vector.y)
|
|
|
|
|
|
|
|
# Returns a vector3 with `vector`'s x and y components and 0 in z.
|
|
|
|
static func to_vector3(vector: Vector2) -> Vector3:
|
|
|
|
return Vector3(vector.x, vector.y, 0)
|