Help with object rotation using quaternion with Leap Motion.

Hello everyone, I expose briefly the problem. I wrote a python module that allows me to interact with the Blender game engine using the device Leap Motion. The only problem that I find concerns with the rotation of the objects performed with the palm of the hand.
I want you to notice that the exact same code works right when it runs outside of the blender game engine as an add-on.
I’ll post my update function of the orientation of the object right down and a short video of the result


def update(self, leap_dict):
        
        #print("rotation:
" + str(self.target_object.localOrientation))
        
        if(self.use_finger):
            pointable = self.pointable_selector.select(leap_dict)
            if(pointable == None):
                return

            dir = mathutils.Vector(pointable["direction"])
        
            if(self.pointable_start_direction == None):
                self.pointable_start_direction = mathutils.Vector(dir)

            # Rotation with respect to the original pointable direction       
            delta_rot = self.pointable_start_direction.rotation_difference  (dir)

        else:   # Use hand palm
     
            hand = self.hand_selector.select(leap_dict)
            if(hand == None):
                return

            h_x = mathutils.Vector(hand["direction"])
            h_y = mathutils.Vector(hand["palmNormal"])
            h_z = h_x.cross(h_y)
    
            # Build the rotation matrix using the data of the 3 orthogonal vectors (hand direction, palm normal, and their outgoing cross product)
            rot_mat = mathutils.Matrix.Rotation(0, 3, 'X')
            rot_mat[0][0], rot_mat[1][0], rot_mat[2][0] = h_x[0], h_x[1], h_x[2]
            rot_mat[0][1], rot_mat[1][1], rot_mat[2][1] = h_y[0], h_y[1], h_y[2]
            rot_mat[0][2], rot_mat[1][2], rot_mat[2][2] = h_z[0], h_z[1], h_z[2]

            rot = rot_mat.to_quaternion()
        
            if(self.palm_start_rotation == None):
                self.palm_start_rotation = rot

            # Rotation with respect to the original palm rotation
            delta_rot = rot * self.palm_start_rotation.inverted()

        # rotate the delta vector according to the view matrix
        cam_rot = self.view_matrix.to_quaternion()        
        delta_rot = cam_rot.inverted() * delta_rot * cam_rot

        # New rotation
        new_rot = delta_rot * self.target_object.localOrientation.to_quaternion(  )
        
        #Finally put it in there
        if(self.target_object != None):
            self.target_object.localOrientation = new_rot


[video]https://www.dropbox.com/s/ypppox5j2av64xo/Screen%20Capture%20Tool_2013-11-14%2020.38.08.mov[/video]

I hope some of you should know how to help me… :frowning:

A few issues I’ve encountered when working with motion tracking/gesture recognition:

  1. Quaternion format.
    Blender’s ordering for quaternions: w,x,y,z. I think the more common ordering I’ve seen is x,y,z,w.

  2. Coordinate system
    For example, sometimes I have to map the z axis to the y axis and vice versa or flip the values across an axis.

If you could, seeing your hand in the video might help also.

Thank you for your reply :slight_smile: I’ll check these two things that you pointed out.
But I emphasize that the exact same logic works fine outside of the game engine. It must be inferred that the game engine uses a different system of representation and use of quaternions than Blender Internal?

Problem solved by setting localOrientaton as the delta rotation :slight_smile:



...
# Rotation with respect to the original pointable direction       
delta_rot = self.pointable_start_direction.rotation_difference  (dir)
...
self.target_object.localOrientation = delta_rot

:smiley: