Cleaned up notes; restructured test scene plus cpu controller script; added WIP action stack logic

This commit is contained in:
2026-11-15 00:29:26 -06:00
parent 90370b8837
commit 1c6878e39b
17 changed files with 225 additions and 138 deletions
+7 -52
View File
@@ -2,6 +2,12 @@
<img src="intuiting-pi.jpg" alt="Intuiting PI" width="400"> <img src="intuiting-pi.jpg" alt="Intuiting PI" width="400">
# Tools
-- https://www.desmos.com/calculator
# Thoughts
var target_position = jet.to_local(jet.target.global_position) var target_position = jet.to_local(jet.target.global_position)
-- Gets us the position of a target jet relative to the jet itself. -- Gets us the position of a target jet relative to the jet itself.
-- This treats the jet as the center of the world when calculating. -- This treats the jet as the center of the world when calculating.
@@ -73,6 +79,7 @@ If queue do we push "rotate towards using xyz pattern" as a command/state to che
### Thoughts on using dot product to check the normalized target position from 'to_local': ### Thoughts on using dot product to check the normalized target position from 'to_local':
- https://www.youtube.com/watch?v=2PrSUK1VrKA
- https://www.youtube.com/watch?v=-udnENzlV4U - https://www.youtube.com/watch?v=-udnENzlV4U
- From, Godot docu: - From, Godot docu:
@@ -118,55 +125,3 @@ Is there anything meaningfully gained from that?
##### Dot Product Formula ##### Dot Product Formula
- From, GPT:
The formula is: A⋅B=∣A∣∣B∣cos(θ)
Where:
- `A` = Your target direction vector.
- `B` = Your forward direction vector.
- `θ` = Angle between the two vectors.
- `|A|` and `|B|` = Magnitudes (lengths) of the vectors.
Since both vectors are normalized (length = 1), the formula simplifies to:
A⋅B=cos(θ)
You have:
A=(0,0,1)
B=(46,33,9)
1. Multiply corresponding components
The dot product formula is:
A⋅B=(AxBx)+(AyBy)+(AzBz)
Substitute your values:
=(0×46)+(0×33)+(1×9)
=9
2. Normalize by vector lengths
Remember, the full formula also divides by the magnitudes when finding cosine:
cos⁡(θ)=A⋅B/(∣A∣∣B∣)
Calculate the lengths/magnitudes:
∣A∣=sqrt(0^2+0^2+1^2)=1
∣B∣=sqrt(46^2+33^2+9^2)
∣B∣=sqrt(3266)≈57.15
∣B∣=3266​≈57.15
3. Calculate cosine
cos⁡(θ)=9/(1x57.15)
cos(θ)≈0.1575​
-- Note: 0.1575 is not radians or degrees.
-- It's a dimensionless number representing the cosine of the angle.
-- Think of it as an intermediate result.
4. Calculate the angle
θ=cosec(0.1575) = ≈1.412 radians
θ≈80.94degrees
+3
View File
@@ -15,6 +15,7 @@ class_name Jet extends CharacterBody3D
var control_frame: BaseJet = null var control_frame: BaseJet = null
var target: Jet = null var target: Jet = null
var targeted_by: Jet = null var targeted_by: Jet = null
var target_direction: Vector3 = Vector3.ZERO
func _ready() -> void: func _ready() -> void:
@@ -24,6 +25,8 @@ func _process(delta: float) -> void:
var action: Globals.CompoundCommand = controller.process(delta) var action: Globals.CompoundCommand = controller.process(delta)
if action: if action:
control_frame.do_action(action, delta, self) control_frame.do_action(action, delta, self)
controller.cpu.no_action()
controller.player.no_action()
func _physics_process(_delta: float) -> void: func _physics_process(_delta: float) -> void:
velocity = -transform.basis.z * control_frame.speed velocity = -transform.basis.z * control_frame.speed
@@ -1,5 +1,5 @@
class_name ActionBase extends Node class_name ActionBase extends Node
func do(_jet: Jet) -> void: func process(_jet: Jet) -> void:
assert(false, "Must override 'do' method from child") assert(false, "Must override 'process' method from child")
@@ -1,18 +1,21 @@
class_name MoveTowards extends ActionBase class_name MoveTowards extends ActionBase
func do(jet: Jet) -> void: func process(jet: Jet) -> void:
if not is_instance_valid(jet.target): return if not is_instance_valid(jet.target): return
var target_local_position: Vector3 = jet.to_local(jet.target.global_position) var target_local_position: Vector3 = jet.to_local(jet.target.global_position)
var target_direction: Vector3 = target_local_position.normalized() var target_direction: Vector3 = target_local_position.normalized()
jet.target_ray.target_position = target_direction jet.target_ray.target_position = target_direction
jet.target_direction = target_direction
#NOTE: When using unit (normalized) vectors, the result will always be #NOTE: When using unit (normalized) vectors, the result will always be
# between -1.0 (180 degree angle) when the vectors are facing opposite # between -1.0 (180 degree angle) when the vectors are facing opposite
# directions, and 1.0 (0 degree angle) when the vectors are aligned. # directions, and 1.0 (0 degree angle) when the vectors are aligned.
# Note also that, probably want to align order around node 'rotation_order' which is YXZ
var forward_dot: float = target_direction.dot(Vector3.UP) var forward_dot: float = target_direction.dot(Vector3.UP)
var ud_dot: float = target_direction.dot(Vector3.FORWARD)
var lr_dot: float = target_direction.dot(Vector3.RIGHT) var lr_dot: float = target_direction.dot(Vector3.RIGHT)
var ud_dot: float = target_direction.dot(Vector3.FORWARD)
jet.controller.cpu.yaw_left() jet.controller.cpu.yaw_left()
@@ -0,0 +1,7 @@
class_name RollLeft extends ActionBase
func process(jet: Jet) -> void:
if not is_instance_valid(jet): return
jet.controller.cpu.roll_left()
@@ -0,0 +1 @@
uid://daep5cdrl6pwv
@@ -0,0 +1,12 @@
class_name RollLeftSideUp extends ActionBase
func process(jet: Jet) -> void:
if not is_instance_valid(jet): return
check_orientation(jet)
jet.controller.cpu.roll_left()
func check_orientation(jet: Jet) -> void:
#var rotation_dot = clampf(jet.rotation_degrees.z / 360.0, -1, 1)
var rotation_norm = jet.rotation_degrees.normalized()
@@ -0,0 +1 @@
uid://tqa0386hofab
@@ -0,0 +1,7 @@
class_name RollRight extends ActionBase
func process(jet: Jet) -> void:
if not is_instance_valid(jet): return
jet.controller.cpu.roll_right()
@@ -0,0 +1 @@
uid://jgirej8jaxa3
@@ -0,0 +1,12 @@
class_name RollRightSideUp extends ActionBase
func process(jet: Jet) -> void:
if not is_instance_valid(jet): return
check_orientation(jet)
jet.controller.cpu.roll_right()
func check_orientation(jet: Jet) -> void:
#var rotation_dot = clampf(jet.rotation_degrees.z / 360.0, -1, 1)
var rotation_norm = jet.rotation_degrees.normalized()
@@ -0,0 +1 @@
uid://gp6yl6ti2xvu
@@ -34,67 +34,77 @@ func assign_jet_target(jet: Jet) -> void:
func assign_jet_action(jet: Jet) -> void: func assign_jet_action(jet: Jet) -> void:
jet.strategy_stack.add_child( MoveTowards.new() ) jet.strategy_stack.add_child( MoveTowards.new() )
jet.strategy_stack.add_child( RollLeftSideUp.new() )
func process_jet_plan(jet: Jet) -> void: func process_jet_plan(jet: Jet) -> void:
jet.strategy_stack.process_action(jet) #process_jet_plan_old(jet)
#process_jet_plan_gpt(jet)
#jet.strategy_stack.process_action(jet)
#if maxf(target_direction.z, 0.0) == 0.0: var target_local_position: Vector3 = jet.to_local(jet.target.global_position)
#get_node("../../base_ui/target_position_fb").text = "infront" var target_direction: Vector3 = target_local_position.normalized()
#jet.controller.cpu.no_action() jet.target_direction = target_direction
#
#if target_direction.x > 0.2:
#jet.controller.cpu.yaw_right()
#elif target_direction.x < -0.2:
#jet.controller.cpu.yaw_left()
#
#elif maxf(target_direction.y, 0.5) == 0.5:
#jet.controller.cpu.no_action()
#
#if target_direction.y > 0.2:
#get_node("../../base_ui/target_position_ab").text = "above"
#jet.controller.cpu.pitch_up()
#elif target_direction.y < -0.2:
#get_node("../../base_ui/target_position_ab").text = "below"
#jet.controller.cpu.pitch_down()
#
#elif maxf(target_direction.y, -0.5) == -0.5:
#get_node("../../base_ui/target_position_ab").text = "below"
#jet.controller.cpu.no_action()
#
#if target_direction.y > 0.2:
#jet.controller.cpu.yaw_right()
#elif target_direction.y < -0.2:
#jet.controller.cpu.yaw_left()
#
#else:
#get_node("../../base_ui/target_position_fb").text = "behind"
#jet.controller.cpu.no_action()
#
#if target_direction.x > 0.2:
#jet.controller.cpu.yaw_right()
#elif target_direction.x < -0.2:
#jet.controller.cpu.yaw_left()
#
#
#get_node("../../base_ui/target_z").text = str(target_direction.z)
#get_node("../../base_ui/target_x").text = str(target_direction.x)
func process_jet_plan_old(jet: Jet) -> void:
#var direction: Vector3 = ( #var direction: Vector3 = (
#jet.target.global_position - jet.global_position #jet.target.global_position - jet.global_position
#).normalized() #).normalized()
var target_local_position: Vector3 = jet.to_local(jet.target.global_position)
var target_direction: Vector3 = target_local_position.normalized()
jet.target_ray.target_position = target_direction
if maxf(target_direction.z, 0.0) == 0.0:
get_node("../../base_ui/target_position_fb").text = "infront"
jet.controller.cpu.no_action()
if target_direction.x > 0.2:
jet.controller.cpu.yaw_right()
elif target_direction.x < -0.2:
jet.controller.cpu.yaw_left()
elif maxf(target_direction.y, 0.5) == 0.5:
jet.controller.cpu.no_action()
if target_direction.y > 0.2:
get_node("../../base_ui/target_position_ab").text = "above"
jet.controller.cpu.pitch_up()
elif target_direction.y < -0.2:
get_node("../../base_ui/target_position_ab").text = "below"
jet.controller.cpu.pitch_down()
elif maxf(target_direction.y, -0.5) == -0.5:
get_node("../../base_ui/target_position_ab").text = "below"
jet.controller.cpu.no_action()
if target_direction.y > 0.2:
jet.controller.cpu.yaw_right()
elif target_direction.y < -0.2:
jet.controller.cpu.yaw_left()
else:
get_node("../../base_ui/target_position_fb").text = "behind"
jet.controller.cpu.no_action()
if target_direction.x > 0.2:
jet.controller.cpu.yaw_right()
elif target_direction.x < -0.2:
jet.controller.cpu.yaw_left()
get_node("../../base_ui/target_z").text = str(target_direction.z)
get_node("../../base_ui/target_x").text = str(target_direction.x)
func process_jet_plan_gpt(jet: Jet) -> void:
# NOTE: (From GPT) Mostly works but with precession nor do I really understand it... # NOTE: (From GPT) Mostly works but with precession nor do I really understand it...
#var forward_dot: float = target_direction.dot(Vector3.UP) var target_direction: Vector3 = jet.to_local(jet.target.global_position)
#if forward_dot >= cos(deg_to_rad(37.5)): var forward_dot: float = target_direction.dot(Vector3.UP)
#get_node("../../base_ui/target_position_fb").text = "infront" if forward_dot >= cos(deg_to_rad(37.5)):
#jet.controller.cpu.no_action() get_node("../../base_ui/target_position_fb").text = "infront"
#else: jet.controller.cpu.no_action()
#get_node("../../base_ui/target_position_fb").text = "behind" else:
#if target_direction.x >= 0.2: get_node("../../base_ui/target_position_fb").text = "behind"
#jet.controller.cpu.yaw_right() if target_direction.x >= 0.2:
#elif target_direction.x <= -0.2: jet.controller.cpu.yaw_right()
#jet.controller.cpu.yaw_left() elif target_direction.x <= -0.2:
jet.controller.cpu.yaw_left()
@@ -1,21 +1,23 @@
class_name StrategyStack extends Node class_name StrategyStack extends Node
var active_action: ActionBase
func _init() -> void: func _init() -> void:
tree_entered.connect(add_action) child_entered_tree.connect( add_action.bind() )
tree_exited.connect(remove_action) child_exiting_tree.connect( remove_action.bind() )
func add_action() -> void: func add_action(action: ActionBase) -> void:
pass if active_action: return
active_action = action
func remove_action() -> void: func remove_action(action: ActionBase) -> void:
pass if not active_action == action: return
# NOTE: 1st node hasn't actually been removed yet
active_action = null if not get_child_count() > 1 else get_child(1)
func process_action(jet: Jet) -> void: func process_action(jet: Jet) -> void:
var action = get_child(0) if not active_action: return
if not action: return active_action.process(jet)
action.do(jet)
-2
View File
@@ -5,8 +5,6 @@ var commands: Globals.CompoundCommand = Globals.CompoundCommand.new()
func process(_delta: float) -> Globals.CompoundCommand: func process(_delta: float) -> Globals.CompoundCommand:
no_action()
if Input.is_action_pressed("pitch_up"): if Input.is_action_pressed("pitch_up"):
commands.PITCH = Globals.ControllerCommand.PITCH_UP commands.PITCH = Globals.ControllerCommand.PITCH_UP
elif Input.is_action_pressed("pitch_down"): elif Input.is_action_pressed("pitch_down"):
+71 -14
View File
@@ -1,41 +1,98 @@
class_name TestWorld extends Node3D class_name TestWorld extends Node3D
@onready var jet: Node3D = $jet @onready var jet: Node3D = $jet
@onready var jet2: Node3D = $jet2 @onready var jet2: Node3D = $jet2
var target_ray: RayCast3D @onready var marker: Marker3D = $marker
@onready var ray: RayCast3D = $ray
@onready var target_ray: RayCast3D = $jet/target_ray
func _ready() -> void: func _ready() -> void:
target_ray = jet.get_node("target_ray")
func _process(delta: float) -> void:
#test_dot_product(delta)
pass pass
func _process(delta: float) -> void:
test_dot_product(delta)
pass
func test_dot_product(_delta: float) -> void: func test_dot_product(_delta: float) -> void:
#NOTE: When using unit (normalized) vectors, the result will always be #NOTE: When using unit (normalized) vectors, the result will always be
# between -1.0 (180 degree angle) when the vectors are facing opposite # between -1.0 (180 degree angle) when the vectors are facing opposite
# directions, and 1.0 (0 degree angle) when the vectors are aligned. # directions, and 1.0 (0 degree angle) when the vectors are aligned.
var target_local_position: Vector3 = jet.to_local(jet2.global_position) manual_direction_and_length()
var target_direction: Vector3 = target_local_position.normalized() var target_direction = tool_direction_and_length()
target_ray.target_position = target_direction
var forward_dot: float = target_direction.dot(Vector3.UP) var forward_dot: float = target_direction.dot(Vector3.UP)
var ud_dot: float = target_direction.dot(Vector3.FORWARD)
var lr_dot: float = target_direction.dot(Vector3.RIGHT) 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'. #NOTE: if neg 'below'; if pos 'above'.
# Need window between +-0.2 or something for centered # Need window between +-0.2 or something for centered
print("Forward : ", forward_dot) #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))) #print("Forward : ", rad_to_deg(cosh(forward_dot)))
#NOTE: if neg 'behind'; if pos 'ahead'. #NOTE: if neg 'behind'; if pos 'ahead'.
# Need window between +-0.2 or something for centered # Need window between +-0.2 or something for centered
print("Up | Down : ", ud_dot) #print("Up | Down : ", ud_dot)
#print("Up | Down : ", rad_to_deg(cosh(ud_dot))) #print("Up | Down : ", rad_to_deg(cosh(ud_dot)))
#NOTE: if neg 'left'; if pos 'right'. #NOTE: if neg 'left'; if pos 'right'.
# Need window between +-0.2 or something for centered # Need window between +-0.2 or something for centered
print("Left | Right : ", lr_dot) #print("Left | Right : ", lr_dot)
#print("Left | Right : ", rad_to_deg(cosh(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
+19 -2
View File
@@ -1,4 +1,4 @@
[gd_scene load_steps=15 format=3 uid="uid://boihqkqrbm0ti"] [gd_scene load_steps=17 format=3 uid="uid://boihqkqrbm0ti"]
[ext_resource type="Script" uid="uid://emw28v4vimpy" path="res://skunk_works/test_world.gd" id="1_nnwr0"] [ext_resource type="Script" uid="uid://emw28v4vimpy" path="res://skunk_works/test_world.gd" id="1_nnwr0"]
[ext_resource type="PackedScene" uid="uid://bhjfvc7gkrvah" path="res://base_environment.tscn" id="2_iba6n"] [ext_resource type="PackedScene" uid="uid://bhjfvc7gkrvah" path="res://base_environment.tscn" id="2_iba6n"]
@@ -51,6 +51,14 @@ emission = Color(0, 0.18431373, 0.4745098, 1)
[sub_resource type="BoxMesh" id="BoxMesh_530sk"] [sub_resource type="BoxMesh" id="BoxMesh_530sk"]
material = SubResource("StandardMaterial3D_ggpwi") material = SubResource("StandardMaterial3D_ggpwi")
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_530sk"]
albedo_color = Color(0.7921569, 0, 0, 1)
emission_enabled = true
emission = Color(0.7921569, 0, 0, 1)
[sub_resource type="SphereMesh" id="SphereMesh_gwly4"]
material = SubResource("StandardMaterial3D_530sk")
[node name="test_world" type="Node3D"] [node name="test_world" type="Node3D"]
script = ExtResource("1_nnwr0") script = ExtResource("1_nnwr0")
@@ -113,6 +121,15 @@ mesh = SubResource("BoxMesh_ggpwi")
transform = Transform3D(0.5, 0, 0, 0, 0.5, 0, 0, 0, 0.5, 0, 0, -2) transform = Transform3D(0.5, 0, 0, 0, 0.5, 0, 0, 0, 0.5, 0, 0, -2)
mesh = SubResource("BoxMesh_530sk") mesh = SubResource("BoxMesh_530sk")
[node name="ray" type="RayCast3D" parent="."]
[node name="marker" type="Marker3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -15.739372)
[node name="dot" type="MeshInstance3D" parent="marker"]
transform = Transform3D(0.2, 0, 0, 0, 0.2, 0, 0, 0, 0.2, 0.01133728, 0, 0)
mesh = SubResource("SphereMesh_gwly4")
[node name="camera" type="Camera3D" parent="."] [node name="camera" type="Camera3D" parent="."]
transform = Transform3D(0.45554495, -0.23939586, 0.8574196, 0, 0.96316254, 0.26891983, -0.8902128, -0.12250507, 0.43876383, 20.807, 12.386, 0.178) transform = Transform3D(0.6978729, -0.025204023, 0.71577805, 0, 0.99938065, 0.03519026, -0.71622163, -0.024558326, 0.6974406, 8.063868, 1.6829107, 1.3785272)
current = true current = true