To apply torque off center to a RigidBody2D

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

I am by now pretty sure there is no dedicated method available in the engine, but maybe someone can point me to some other way. How would one apply torque to a RigidBody2D, at an arbitrary point that does not match its origin?

What do you need the force to do exactly? Do you want the object to rotate around an axis that is not the center of mass? You could use a PinJoint2D if you need the object to rotate around a fixed point.

a_world_of_madness | 2023-06-11 11:02

I need to apply given physical torque at a given point, and have objects linear and angular velocities altered accordingly. What axis it should rotate about would depend on other forces, joints and collisions present. Thanks but pin joint won’t do.

Rpahut | 2023-06-12 07:26

Would this Godot demo project be the solution?

a_world_of_madness | 2023-06-12 08:35

Tried apply_impulse at some point. Problem is how to recalculate from the torque relative to a point into an impulse to apply relative to origin.
It also seem to put the entire impulse into linear velocity, which is not correct for a force acting off center. Would be better off altering velocity directly. Only I have no idea how to approach the math.

Rpahut | 2023-06-12 11:57

But is the behavior of the demo I uploaded correct? apply_impulse is good for one off forces, like in the example. apply_central_impulse automatically applies the force at the center of mass of the object, meaning no angular momentum, but apply_impulse and apply_force allow you to specify the offset from center of mass, meaning you will get angular momentum.

In the example I provided, I apply a force off-center that causes the object to rotate, then apply the inverse of same force at the center of mass, which negates the linear velocity, so the object only rotates. The math is pretty simple, it’s just finding the vector from the center of mass to the mouse position, then rotates the vector 90 degrees.

Changing the physics state (angular and linear velocities) directly can easily cause problems if you need to have external forces affect the object as well.

Can you provide more info about the problem you are trying to solve?

a_world_of_madness | 2023-06-12 12:26

Thank you for investing your time a_world_of_madness, really, only applying impulse off center and getting some any angular acceleration out of it is not the same as applying torque off center. I do need help but having to explain the difference I doubt you have the answer so it would be a waste of time for both of us.

Rpahut | 2023-06-12 18:47

:bust_in_silhouette: Reply From: Rpahut

I was partially able to acheive what I wanted, in the end.
The way I came up with is to split a body into parts, one for each point of torque application, and fix them together. This essentially creates a composite body that, unlike RigidBody2D, is able to accept torque at more than one point.
I couldn’t however make the torque have accurate and precise effect. Some of it must be getting lost while forces are transferred between the parts, or to floating point errors, or maybe I have a mistake somewhere. I gave up on that part for now and just tune the unitless strenght to get the desired effect.