## Stabilizing floating body

kazeno
Posts: 8
Joined: Sat Feb 21, 2015 1:46 am

### Stabilizing floating body

I can not figure out how to stabilize a floating body like a helicopter or a hover skate with thrusts in the front and rear ends.
I has trying to make the thrust equal to the distance to the target but it only work for bodies with fixed rotation and bounce too much when reach the target.
For acceleration instead of targeting a position should I apply an horizontal force or rotate the body?

Can someone help me with that?

iforce2d
Posts: 860
Joined: Sat Dec 22, 2012 7:20 pm

### Re: Stabilizing floating body

If you apply the force at the center of mass it should cause no rotation, so non-fixed-rotation bodies will behave the same as fixed-rotation bodies:
body->ApplyForce( force, body->GetWorldCenter() );

Making the thrust equal to the distance to the target is called "proportional" control, and will almost always cause the bouncing result that you mention. You also need to take into account the current speed of the body, because if it's already heading in the right direction then you don't need to add so much thrust right?

One simple way you can do this is to "look ahead" and instead of using the current position of the body to calculate the thrust, use the position where the body will be a short time in the future, eg. half a second, one second etc. Examples here:
https://www.iforce2d.net/b2dtut/rotate-to-angle
https://www.iforce2d.net/b2dtut/suspension

A better way to do it might be to use a PID controller. There is a lot of info on the net about those, you can use the psuedo code from the wikipedia page to make a simple one. Example here: https://www.iforce2d.net/blog/2014-04-22

kazeno
Posts: 8
Joined: Sat Feb 21, 2015 1:46 am

### Re: Stabilizing floating body

But do the shape of the fixture make it rotate?
When it's asymmetric or the force is diagonal.
Helicopter is pretty asymmetric and can have extra load.
How they do all that with a single helix and a small one for yaw.

I'm studying the PID controller and it to works really great!
On the other hand the constants are hard to figure out.
I trying to move a motorcycle with it but the motor speed is fast close to the target and overshot too much.
I think I should consider the wheels friction and ground properties...

iforce2d
Posts: 860
Joined: Sat Dec 22, 2012 7:20 pm

### Re: Stabilizing floating body

In real life the shape of things affect how they rotate because we have wind, but Box2D doesn't so it should not.

Yes, figuring out good values for PID constants is something that people write entire university theses about