PID controller for Box2D movement/turning

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
pgimeno
Party member
Posts: 3691
Joined: Sun Oct 18, 2015 2:58 pm

Re: PID controller for Box2D movement/turning

Post by pgimeno »

Alright, now it starts to make sense, and it doesn't need a PID to start with. It's the same problem as the autopilot for spaceships, for which there doesn't appear to be a formula, but when restricted to one dimension, like in your case, there is one.

The general problem is: you're in a spaceship, with a certain velocity vector, and you want to reach a certain point in space at zero velocity. Your only control is a thruster which you can orient however you want, but it has a limit in its maximum thrust. How should you orient the thruster to reach the goal at zero speed in minimum time? In 3D you can always transform it to a 2D problem, by considering only the plane formed by three points: the ship, the destination point, and the tip of the velocity vector when its base is at the ship. But even in 2D it's a hard problem.

However, when reduced to a 1D problem (which happens if the initial velocity vector is aligned with the destination point) you can use formulas in linear motion physics to solve it. In your case, the angle is 1D, so it's exactly this case, save for the extra complication of circle wraparound. In your case linear velocity translates to angular velocity, thrust to torque, and position to angles.

So you have a starting point, x0, an initial speed, v, and ending point, x1, and a final speed which needs to be 0. If your initial velocity is 0, then you need to accelerate at maximum thrust until you reach the midpoint, then decelerate until you reach the destination. Obviously the travel will be symmetrical and you'll be at the destination at zero speed as desired.

If you have a nonzero initial speed, you need to calculate the braking point, i.e. the point where you need to stop accelerating and start braking the point where you would stop if you started braking right away. Since you have limited thrust (for both accelerating and braking), it may be the case that your initial speed is so high that you can't avoid overshooting. That's OK, though - you can start again once you reach the point where your velocity is zero and reduce it to the first case.

I have the calculations worked out for the case of linear movement, with no circular wraparound. Let me know if you want to find them out by yourself or you want me to post the formulas.

(Edited per the strikethrough/underline)
User avatar
togFox
Party member
Posts: 835
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: PID controller for Box2D movement/turning

Post by togFox »

I see. The first bit is easy - thrust till half way then counter thrust. That's intuitive. The non-zero initial v and overshooting x1 was challenging.

I I think I'll try the math myself given this is angular and not longest. Thanks!
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/pad-and-pencil-gridiron
User avatar
pgimeno
Party member
Posts: 3691
Joined: Sun Oct 18, 2015 2:58 pm

Re: PID controller for Box2D movement/turning

Post by pgimeno »

I have a PoC made in case you get stuck :)
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 5 guests