RigidBody2d will not move when it has an Area2D as a child

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By frostypaw

My scene tree looks like:

Character (RigidBody2D)
 -> CollisionShape2D
 -> MeleeHitbox (Area2D)
    -> CollisionShape2D

In my main scene’s “_process” function, I call character.linear_velocity = <non-zero Vector2> to make my characters move. When the MeleeHitbox is added to my Character scene, the characters will not move. When I remove it, they move perfectly as I’d expect.

Any ideas what’s going on here?

I’ve tried disabling all collision layers/masks on the MeleeHitbox. I’ve tried moving the MeleeHitbox CollisionShape2D so it doesn’t overlap my RigidBody2D’s CollisionShape2D. I’ve tried:

func _integrate_forces(state):
  state.set_linear_velocity(cur_velocity)

on my RigidBody2D instead of using self.linear_velocity. I’ve tried using apply_central_impulse() instead of self.linear_velocity. I’ve tried setting the linear_damp field on the MeleeHitbox to 0. None of these experiments had any effect; the only thing that works is if I remove the MeleeHitbox entirely.

It’s not possible to reproduce your situation and get the behavior that you’re getting by following exactly whatever you said you did. You clearly need to share more details with us so we can reproduce the situation with the minimum amount of steps. Can you recreate the objects and scripts from scratch and check at which exact point the problem occurs?

I’d like to ask you something:
Why are you controlling the velocity of a RigidBody2D? It’s explicitly stated in the documentation that one shouldn’t alter the position of linear velocity at every frame. Stick with applying a force or impulse instead.

nahiyan | 2021-06-17 15:36

I’ll work on a minimal example later today. WRT the linear velocity question; I agree that I’m abusing the system a bit here. It has been working for me so far. I’d like to transition to using the _integrate_forces system as I mentioned in my answer (where I set cur_velocity in my code then make the linear_velocity change in _integrate_forces). Hopefully that will be a good long term solution. Unless there’s a better practice to do this? My end goal is to have objects that react to impacts (getting hit by other objects), while still having their velocity being file-tunable so I can have them path around a math (e.g. with astar).

frostypaw | 2021-06-17 16:16

An update: it seems like this problem is caused by my usage of character.rotation = <angle> in the line of code either before or after my character.linear_velocity = <vector> usage. If I remove the rotation call, everything works as expected. Digging into why this might be…

frostypaw | 2021-06-17 20:15

I’d like to ask you something:
Why are you controlling the velocity of a RigidBody2D? It’s explicitly stated in the documentation that one shouldn’t alter the position of linear velocity at every frame. Stick with applying a force or impulse instead.

chavisww2 | 2021-06-20 16:44

:bust_in_silhouette: Reply From: wyattb

Did you try using

void add_force(offset: Vector2, force: Vector2)

#Adds a positioned force to the body. 
#Both the force and the offset from the body origin are in global coordinates.

He said that he tried applying an impulse. I don’t think applying a force would work when an impulse doesn’t.

nahiyan | 2021-06-17 15:36

:bust_in_silhouette: Reply From: frostypaw

I fixed this issue by not setting character.rotation = <angle> in my _process code, instead using:

  character.angular_velocity = 40 * sin((direction.angle() - character.rotation) / 10)

to smoothly have the character turn until my desired direction is equal to my character’s rotation.

Probably next I will mess around more with _integrate_forces, since that seems like the proper way to mess with linear/angular velocities.