Setup an inverted pendulum simulation with BGE I'm very new to Blender. I'd like to setup a very simple rigid body physics system using the game engine. The system is simply a pole hinged in the middle of a block, which can move horizontally on a plane with friction, and walls to prevent it flying off the plane. It responds to a horizontal force that acts on the block, at every frame/timestep. 

 

It's well known in engineering as the 2D cart/pole problem, aka inverted pendulum, and the rigid body equations are well known, but in Blender we don't need them! A simulation would start with the pole upright, and then apply random forces/impulses, (actuate it), horizontally on the block. When the pole falls below horizontal, its physically impossible for it to get back to vertical, and the simulation can end. 

Setting this up procedurally would be a really nice/helpful introduction to rigid bodies and actuators in the BGE. I think in terms of the BGE/Physics the only scripting needed is to use a random, applyForce(...) or applyImpulse(...), on the block at each frame/timestep. 

I'm not really sure that the game engine is needed for this simulation, only physics, as there is no user interaction? The output is just the sequence of rendered images, for a small time step/frames per second, and the value of the actuator/force applied at each timestep/frame.

Perhaps a the easiest place to start is from the [domino's tutotrial][4]         

The things I'm stick on are,


How to hinge the rod to the block, I think I need a constraint, OR Maybe I should use a single armature bone ?
To apply a random force or impulse at each keyframe, I think I need something like bpy.ops.anim.keyframe_insert ?
I made a simple example in the game engine of a mouse controlled inverted pendulum. It answers only your question about the hinge, but maybe you can go from there.

The body block has a rigid body constraint, type hinge, target is the pole. The weight at the top is parented to the pole.

I'm sure it's not physically correct, but I had to change translation and rotation damping in the physics properties of the pole to have a chance at controlling the pendulum. I also reduced the mass of the pole.

If you'd like to have a look, here's the blend file: