How to calculate an object's global speed??

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Czselu349

Hello! I am developing a game in wich you’ll have a sword. The plan is that you can rotate the sword around (kinda like in “Nuclear Throne” but with a sword) Your damage would be highly affected by the speed of the sword you’re wielding. The question is how to actually calculate the speed?? Simply calculating the magnitude isn’t enough because i use two nodes to rotate the weapon around.

Here’s a pic:

Here Is my code That i Use currently:

extends Position2D


onready var speed = 0
onready var distance = self.position.distance_to( get_parent().get_parent().position )
onready var pre_distance = distance

func _physics_process(delta):
 distance = self.global_position.distance_to(
            get_parent().get_parent().global_position)
 speed = abs(pre_distance - distance)

# Update prev_magnitude per every 0.005 seconds
func _on_Timer_timeout():
 pre_distance = distance

I know that it is terrible code. It’s not even global, as you can see I made it, so it’s relative to the parent. I am only learning so don’t judge me. XD

Ps: Thanks for the answer.

Image is too small to actually read anything.

Did you try get_linear_velocity()? Is your sword Rigidbody?

blockchan | 2020-05-08 22:27

No, it’s a Area2D attached to a position2D, But I’ll probably make the position2D into a kinematic body, because I want the sword to be throwable.

Czselu349 | 2020-05-15 06:40

:bust_in_silhouette: Reply From: A112Studio

If you’re measuring the distance from the center, while going in a circle the distance willa remain the same.
You either should keep track of swords angular velocity or its global position change.
I’d suggest measuring it every _physics_process so you can use delta and (prev_pos-current_pos).length() to calculate the velocity

Thanks for the answer!

Czselu349 | 2020-05-09 19:15