Velocity based damage in a topdown game

Godot Version

4.0.2

Question

I’m currently making a fighting game where a character’s punch power is based on its movement. Currently, I’m using this line to calculate damage:

func calculate_damage():
return baseDamage + (get_real_velocity().length() * 0.05)

However, that is not satisfying enough for me. I want the rotation speed of the character to impact the output damage. Imagine a hammer or a flail, the harder you swing it the more deadly it is. My question is, how can I improve my code to get the results I want?

Excuse the rough formatting, I’m new here

If you want to format your code you can use the standard triple backtick (```)

func hello():
  print("Hello World!")

Or use the </> button at the top of the edit box. I suggest you always do that when pasting code as it makes the life of everyone much easier.

Regarding your issue I do not know how your current implementation looks. If you are manually rotating the object then you should already have all the information necessary to do a calculation based on that. If you are using physics simulations then look into properties like angular velocity and then do a calculation based on that.

1 Like

Thanks for the tips on formatting.

Regarding implementation, I’m using a CharacterBody2D for the player character, and a look_at() function to point it towards the mouse. As far as I’m concerned, angular velocity is a property only reserved for rigid bodies and I can’t find anything else for character bodies.

I’ve been searching for this problem online before and it seems that this Reddit thread is the closest to the solution I’m looking for but there are problems with it too.

  1. It works only for rigid body (again)
  2. It’s for 3D enviroment, not 2D

You can get the angular velocity with look_at() by keeping track of the rotation on the previous frame. Dividing the difference in rotation by delta gives you the angular velocity.

@onready var previous_rotation = rotation

func _physics_process(delta):
    look_at(something)
    var rotation_per_frame = fposmod(rotation - previous_rotation + PI, TAU) - PI
    var angular_velocity = rotation_per_frame / delta
    previous_rotation = rotation

If a weapon perfectly follows the mouse cursor, the angular velocity can get ridiculously high, so you might want to limit the maximum value for damage calculations.

base_damage + minf(absf(angular_velocity), MAX_ANGULAR_VELOCITY) * SWING_DAMAGE_MULTIPLIER

If the damage should depend on the combined velocity of the character and its weapon, you need to calculate the velocity of the weapon and add it to the character’s velocity.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.