Shot projection target is oriented to surface normal

This commit is contained in:
Rob Kelly 2024-12-09 17:04:19 -07:00
parent 424ee2f2ae
commit 902888be87
1 changed files with 12 additions and 1 deletions

View File

@ -73,6 +73,8 @@ func _process(_delta: float) -> void:
var pos := global_position
var vel := -global_basis.z * initial_speed
var final_normal: Vector3
for t in range(0, max_steps):
# TODO: smooth curve with bezier handles
path.curve.add_point(pos - global_position)
@ -102,6 +104,7 @@ func _process(_delta: float) -> void:
vel = PUTT_ATTRITION * (vel - 2 * norm * vel.dot(norm))
else:
# End projection!
final_normal = collision["normal"]
break
pos = next_pos
@ -109,12 +112,20 @@ func _process(_delta: float) -> void:
# Add terminal point (possibly collision point)
path.curve.add_point(pos - global_position)
var child_basis := Basis.IDENTITY
if final_normal:
var up := final_normal.normalized()
var forward := Vector3(up.y, -up.x, 0)
var right := up.cross(forward).normalized()
forward = right.cross(up).normalized()
child_basis = Basis(right, up, forward)
# Reposition any children
for n: Node in get_children():
if n is Node3D and n != path:
var node_3d: Node3D = n
node_3d.global_position = pos
node_3d.global_basis = Basis.IDENTITY
node_3d.global_basis = child_basis
(%DebugDraw as CanvasItem).queue_redraw()