0 votes

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.

in Engine
edited

Image is too small to actually read anything.

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

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.

## 1 Answer

+2 votes
Best answer

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

by (178 points)
selected

Thanks for the answer!