57

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?

Ranjit
  • 5,130
  • 3
  • 30
  • 66
Anaphory
  • 6,045
  • 4
  • 37
  • 68
  • 16
    Rotations in 3D space can be quite tough. In my opinion, it's easiest to just put the time into understanding quaternions, and use those instead. – fluffels Oct 19 '12 at 13:03
  • If using euler angles (instead of quaternions) one of the issues you may incur into is gimbal lock (http://en.wikipedia.org/wiki/Gimbal_lock), which might cause quite some problems – decden Apr 06 '13 at 19:53
  • Is it easy to try this out when you have Blender installed? (Not a Blender expert) – Gerard Jun 10 '13 at 19:50
  • 6
    That link to the FPSController structure doesn't work anymore. Do you have a mirror? – Asad Saeeduddin Aug 14 '13 at 03:34

2 Answers2

4

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

Marcin
  • 48,559
  • 18
  • 128
  • 201
0

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)

Teck-freak
  • 127
  • 8