This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
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:
enter image description here

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 by (137 points)
edited by

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 by

Thanks for the answer!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.