99 lines
4.0 KiB
GDScript
99 lines
4.0 KiB
GDScript
class_name TestWorld extends Node3D
|
|
|
|
|
|
@onready var jet: Node3D = $jet
|
|
@onready var jet2: Node3D = $jet2
|
|
@onready var marker: Marker3D = $marker
|
|
@onready var ray: RayCast3D = $ray
|
|
@onready var target_ray: RayCast3D = $jet/target_ray
|
|
|
|
|
|
|
|
func _ready() -> void:
|
|
pass
|
|
|
|
func _process(delta: float) -> void:
|
|
test_dot_product(delta)
|
|
pass
|
|
|
|
func test_dot_product(_delta: float) -> void:
|
|
#NOTE: When using unit (normalized) vectors, the result will always be
|
|
# between -1.0 (180 degree angle) when the vectors are facing opposite
|
|
# directions, and 1.0 (0 degree angle) when the vectors are aligned.
|
|
manual_direction_and_length()
|
|
var target_direction = tool_direction_and_length()
|
|
|
|
var forward_dot: float = target_direction.dot(Vector3.UP)
|
|
var lr_dot: float = target_direction.dot(Vector3.RIGHT)
|
|
var ud_dot: float = target_direction.dot(Vector3.FORWARD)
|
|
#var rotation_dot = jet.rotation.direction_to().normalized().dot(Vector3.FORWARD)
|
|
var rotation_dot = clampf(jet.rotation_degrees.z / 360.0, -1, 1)
|
|
|
|
#NOTE: if neg 'below'; if pos 'above'.
|
|
# Need window between +-0.2 or something for centered
|
|
#print("Rotation : ", rotation_dot)
|
|
#print("Rotation : ", rad_to_deg(cosh(rotation_dot)))
|
|
|
|
#NOTE: if neg 'below'; if pos 'above'.
|
|
# Need window between +-0.2 or something for centered
|
|
#print("Forward : ", forward_dot)
|
|
#print("Forward : ", rad_to_deg(cosh(forward_dot)))
|
|
|
|
#NOTE: if neg 'behind'; if pos 'ahead'.
|
|
# Need window between +-0.2 or something for centered
|
|
#print("Up | Down : ", ud_dot)
|
|
#print("Up | Down : ", rad_to_deg(cosh(ud_dot)))
|
|
|
|
#NOTE: if neg 'left'; if pos 'right'.
|
|
# Need window between +-0.2 or something for centered
|
|
#print("Left | Right : ", lr_dot)
|
|
#print("Left | Right : ", rad_to_deg(cosh(lr_dot)))
|
|
|
|
|
|
func tool_direction_and_length() -> Vector3:
|
|
var target_local_position: Vector3 = jet.to_local(jet2.global_position)
|
|
var target_direction: Vector3 = target_local_position.normalized()
|
|
target_ray.target_position = target_direction
|
|
|
|
return target_direction
|
|
|
|
func manual_direction_and_length() -> void:
|
|
# NOTE: Length is treated as length of a vector.
|
|
# Distance is a length between two points aquired by the 'difference vector' which to get is (to_vec - from_vec)
|
|
# VERY important to be to/from rather than from/to as from/to would give direction from target position than self
|
|
# To do the manual equivilint of:
|
|
# var target_direction: Vector3 = jet.to_local(jet2.global_position).normalized()
|
|
# We do:
|
|
# var target_direction: Vector3 = (
|
|
# (jet2.global_position - jet.global_position)
|
|
# /
|
|
# (jet2.global_position - jet.global_position).length()
|
|
# ) + jet.global_position
|
|
# Like so:
|
|
|
|
var target_direction_difference: Vector3 = (jet2.global_position - jet.global_position)
|
|
# NOTE: Length is the square root of the sum of the squared components
|
|
# length = sqrt(x^2 + y^2 + z^2) // Single vector length; note, could just be x,y if in 2D
|
|
# combined vectors way:
|
|
# length = sqrt( (b.x - a.x)^2 + (b.y - a.y)^2 + (b.z - a.z)^2 )
|
|
var target_direction_length: float = sqrt(
|
|
pow(target_direction_difference.x, 2.0)
|
|
+ pow(target_direction_difference.y, 2.0)
|
|
+ pow(target_direction_difference.z, 2.0)
|
|
)
|
|
|
|
# NOTE: Could divide by zero. Handle with exception or return 0,0,0 as we do here
|
|
var target_direction_normalized: Vector3 = \
|
|
(target_direction_difference / target_direction_length) \
|
|
if target_direction_length > 0.0 else Vector3.ZERO
|
|
|
|
ray.global_position = jet.global_position
|
|
# ray is in local space context so giving direction just works
|
|
ray.target_position = target_direction_normalized
|
|
|
|
# corrected position b/c target_global_direction starts at 0,0,0 globally.
|
|
# (normalized not a factor other than affecting length)
|
|
# thus add 'jet' position to move origin to it + resulting offset
|
|
var target_direction = target_direction_normalized + jet.global_position
|
|
marker.global_position = target_direction
|