# How to calculate an object's global speed??

Attention Topic was automatically imported from the old Question2Answer platform.

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 distance = self.position.distance_to( get_parent().get_parent().position )

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

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

I’d suggest measuring it every `_physics_process` so you can use delta and `(prev_pos-current_pos).length()` to calculate the velocity