Blender: Walk around sphere

PythonBlenderCoordinate Transformation

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

There is a allign-function. If the game-object is called own it should be something like own.alignAxisToVect(vector, 2, 1) with 2 being the index for the Z-axis(x=0,y=1,z=2) and 1 being the speed of allignment (between 0 and 1)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnaphoryView Question on Stackoverflow
Solution 1 - PythonMarcinView Answer on Stackoverflow
Solution 2 - PythonTeck-freakView Answer on Stackoverflow