I want to implement physics based damage to enemies from the velocity of the Velocity of a Rigidbody2d colliding with it, but I have no idea how it implement it while limiting the detection to only rigidbodies
You should create an area slightly bigger then the collision body. Use this area to capture the linear_velocity of the rigid body. Then when it collides with the body use the velocity to produce your damage value.
An edge case is if the body enters the area but misses the collision body. This can be handled in a way that can also handle multiple collision happening at the same time.
Save a reference to the body entering and search a list of potential bodies to check when they collide. Or you could cast a ray to see if it will hit the body when it enters the area.
Thanks for the info, but could you explain the last part a bit deeper? I’m confused with the purpose of it
func _on_hitbox_body_entered(body: Node2D) -> void:
if body is RigidBody2D:
var space_state := get_world_2d().direct_space_state
var queary := PhysicsShapeQueryParameters2D.new()
queary.shape_rid = PhysicsServer2D.body_get_shape(body.get_rid(), 0)
queary.motion = body.linear_velocity * get_process_delta_time()
var collisions : Array[Dictionary] = space_state.intersect_shape(queary)
if not collisions.is_empty():
for collision in collisions:
var player = collision.collider
if player is PlayerClass:
player.do_damage(body.linear_velocity)
The reason to use an area larger then the character collision is to catch the velocity before the collision. in my experience if you check velocity at collision, it my not be accurate since it could have been deflected to some degree by the actual collision. (you could just use the area as a hit box and not worry about the secondary collision, then fake the collision if you have something that may bounce off ithe player.)
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.