In order to understand blender python game scripting, I currently try to build a scene in which one can walk around a sphere, using the FPSController structure from this link. For gravity and FPSController orientation I tried to construct a python Controller, which currently looks like this:

def main():
    print("Started")

    controller = bge.logic.getCurrentController()
    me = controller.owner

    distance, loc, glob = me.getVectTo((0,0,0))

    grav = controller.actuators['Gravity']

    strength = me['Gravity']
    force = strength*(distance*distance)*glob

    grav.force = force

    try:
        rot = Vector((0,0,-1)).rotation_difference(glob).to_matrix()
    except Exception as E:
        print(E)
        rot = (0,0,0)

    rotZ = me.orientation
    me.orientation = rot*rotZ

    controller.activate(grav)

main()

which roughly works until any angle goes over 180 degrees, and looks discontinuous then. I assume this comes from rotation_difference being discontinuous – blender documentation on Math Types & Utilities does not say anything, and I have not thought enough about quaternionic representations yet to see if a continuous map would be possible – and I guess there is a more elegant way to achieve that the local Z orientation is continuously mouse-dependent, while local X and Y orientations depend continuously on some given vector, but how?


The consensus seems to be that you should accomplish such rotations using quaternions.

See this for the api: http://www.blender.org/documentation/249PythonDoc/Mathutils.Quaternion-class.html

See this for an introduction to the maths: http://en.wikipedia.org/wiki/Rotation_formalisms_in_three_dimensions#Quaternions