2023-12-15 22:44:25 +01:00
|
|
|
# (c) 2023-present Eroax
|
|
|
|
# (c) 2023-present Yagich
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
extends DeckNode
|
|
|
|
|
|
|
|
|
|
|
|
func _init() -> void:
|
|
|
|
name = "Normalize Vector"
|
|
|
|
node_type = "vector_normalize"
|
|
|
|
description = "Normalizes a vector so its' length (magnitude) is exactly 1."
|
|
|
|
|
|
|
|
add_input_port(
|
|
|
|
DeckType.Types.DICTIONARY,
|
|
|
|
"Vector"
|
|
|
|
)
|
|
|
|
|
|
|
|
add_output_port(
|
|
|
|
DeckType.Types.DICTIONARY,
|
|
|
|
"Result"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-07-28 12:39:09 +02:00
|
|
|
func _receive(_port : int, data : Variant) -> void:
|
2024-02-26 12:36:19 +01:00
|
|
|
|
|
|
|
var normalized = normalize_vector(data)
|
|
|
|
if !normalized is Dictionary:
|
|
|
|
return
|
|
|
|
|
|
|
|
send(0, normalized)
|
|
|
|
|
|
|
|
|
2023-12-15 22:44:25 +01:00
|
|
|
func _value_request(_on_port: int) -> Variant:
|
2024-02-26 12:36:19 +01:00
|
|
|
|
2024-01-25 08:20:41 +01:00
|
|
|
var v = await request_value_async(0)
|
2024-02-26 12:36:19 +01:00
|
|
|
|
|
|
|
return normalize_vector(v)
|
|
|
|
|
|
|
|
|
|
|
|
func normalize_vector(v):
|
|
|
|
|
2024-02-21 07:11:29 +01:00
|
|
|
if not v:
|
2023-12-15 22:44:25 +01:00
|
|
|
DeckHolder.logger.log_node("Vector Normalize: the vector is invalid.", Logger.LogType.ERROR)
|
|
|
|
return null
|
|
|
|
|
2024-02-26 12:36:19 +01:00
|
|
|
if !DeckType.is_valid_vector(v):
|
2023-12-15 22:44:25 +01:00
|
|
|
DeckHolder.logger.log_node("Vector Normalize: the vector is invalid.", Logger.LogType.ERROR)
|
|
|
|
return null
|
|
|
|
|
|
|
|
var l: float = (v.x ** 2.0) + (v.y ** 2.0)
|
|
|
|
var res := {"x": v.x, "y": v.y}
|
|
|
|
if l != 0:
|
|
|
|
l = sqrt(l)
|
|
|
|
res.x = res.x / l
|
|
|
|
res.y = res.y / l
|
|
|
|
return res
|
|
|
|
|
|
|
|
DeckHolder.logger.log_node("Vector Normalize: the vector is length 0. Returning null.", Logger.LogType.ERROR)
|
|
|
|
|
|
|
|
return null
|