# Balance problem with a ball

In Box2D is a platform given with a ball on top. The platform can be moved to left and right, and the object is moving according to standard gravity of 9.8f which is set in the Box2D setting. To make the task a bit harder, there are two platforms at the same time. Left in the image is the demonstration and right is the repetition. The repetition platform gets the same input signals but starts with a slightly different position of the object. The position of the ball can be retrieved in realtime from the program.

The human operator is sending a “left” command, the platform is moving and the ball is rolling. How the controller looks like for the repetition platform if both balls should be in the same position?

My efforts to solve the problem

1. I've created the sourcecode in C++ which is 420 lines long
2. I've programmed a manual controller for testing if the physics engine works
3. I read some papers about Dynamic movement primitives, Learning from demonstration and Tracking control

But it seems, that something is missing for solving the problem. Any advice is welcome.

Sourcecode

The controller is executed in the mainloop. It works currently as a normal pid-controller, which measures the difference between the x-positions of both balls and executes a left or right counteraction. The idea is, that the human-operator controls the demonstration and this modifies the pid-control loop in the agent. But in most cases, it works not very well. The controller is not very intelligent.

mainloop:
myagent.automode();

class Agent {
public:
void automode() {
int posA=myphysics.mybox[6].positioncenter.x-200;
int posB=myphysics.mybox[9].positioncenter.x-400;
int diff = posB-posA;
std::cout << mysettings.framestep << " "<<diff << "\n";
if (diff>0) {
t1.detach();
}
if (diff<-0) {