apply_impulse on RigidBody2D is unpredictable when rotation > PI/2

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

EDIT: Found the solution, it’s so simple !!
Sooo :)) instead of doing apply_impulse(offset, my_force * delta) just do apply_impulse(offset.rotated(rotation), my_force * delta)

Of course, the Center of pressure was no following plane… So the force wasn’t applied at the right place


Hi !

I’m making a 2D plane model

I use a “center of pressure” which is the point where I want to apply my forces (drag, lift, etc…)
So I do for lift: apply_impulse(COP, get_lift_force() * delta)

Where COP is a Vector2(-30, 0) constant behind the center of mass: Vector2(0, 0)

It works pretty well when the plane is going to the right
until rotation > PI/2 or < -PI/2

So lift keep the plane stable because of that COP behind the Center of mass. But as soon as rotation exceeds that 90°, I have to do apply_impulse(-COP, get_lift_force() * delta) to keep it stable
(well in fact it glitches after a few seconds)

On the paper I’m pretty confident about my angles, vectors, etc…

It’s like the calculated torque caused by the offset is inverted as rotation is going above 90°
I tried making something with “elses” and “ifs” but it is even worse
I also tried calculating my own torque with the cross product but same result as apply_impulse in the end

Also, for the drag force, same problem. normaly, I would have done abs(get_aoa) (see below)
But this solution works great with apply_impulse(COP, get_aerobrakes_drag() * delta) even if it isn’t intuitive at all as my drag is in fact wrong (aoa < 0 => drag in same direction as velocity) but corrected by the function apply_impulse()

func get_aoa()->float:
	return linear_velocity.angle() - rotation
func get_aerobrakes_drag()->Vector2:
	var u = -linear_velocity.normalized() # drag is in opposite direction of velocity
	var drag_factor = aerobrakes_drag_coeff * get_aoa()
	var v2 = linear_velocity.length_squared()
	
	return drag_factor * v2 * u

I’m stuck here because I don’t understand why the torque applied by apply_impulse is inverted, do you have any lead to help me find the solution ?