I’m doing a minecart game, here’s a gif of the minecart movement:

As you can see, sometimes the cart starts rolling. It’s not a big issue because it will always get back to be on correct position, and it actually looks like fun, but I’ve been trying to balance the cart, making it rotate towards the angle perpendicular to the railroad below it, and I don’t have a clue on how to do it.

I already can get the current angle in radians of the slope of the rails with a RayCast2D I use to check if the cart is on the floor, like this:

Hi Efi, thanks a lot for your answer! I’m actually trying to find out the correct equation to find the torque amount to apply to balance the cart given its current rotation

Oh, when I said “speed” I meant a factor of your choice to change the amount of influence. The speed it should turn at. Not the linear velocity.
In any case, it seems you’ll need a number there that’s quite higher. Either that or you need to use the impulse version of the function. I’ve had trouble understanding when to use which, but impulse seems more reliable to use in “correction” motions (both the force and torque have both versions).

Oooh I see! I’m in bed now, so I will try again tomorrow. I was thinking now that that mass of the cart alone is maybe not enough, since the cart is also holding the weight of the wheels and the characters on it, all of them are rigid bodies too! The sum of all the masses should definitely increase the value!

For the difference between the impulse and the regular, the docs say one is time dependant and should be called every frame, and the other is not, so I kind of assume I had to use the time dependant since the _integrate_forces method is called every frame.

Center of mass: Ensure your RigidBody2D’s center of mass is positioned strategically. Placing it low and closer to the ground helps maintain stability.

Torque: Apply torque forces opposite to the direction of imbalance. You can calculate the torque based on the object’s angular velocity and desired center of rotation.

Proportional-Integral-Derivative (PID) control: Implement a PID controller that analyzes the angle and angular velocity of the object and applies corrective torque based on their difference from the desired values.

About the center of mass, you can see where it is in the second gif. The RayCast2D origin is set to the center of mass, so it’s low-left of the cart.

About the other things, they sound like they should do the trick but I’m not a math person and I don’t really know how to calculate the torque based on the angular velocity.

I’ve also read somewhere else about the PID controller, but that sounded even more intimidating to me

I finally got it! Today I woke up from bed completely unaware that at the end of the day I will be able to understand what a PID controller is and how to implement one in gdscript! Thanks a lot to both of you.

This is a gif of the cart finally balancing itself:

The PID Controller implementation is rather simple once you understand it, here’s mine, based on this plugin: