From 02e145cb923d601395acc7f15ae9e13f85ef2fbb Mon Sep 17 00:00:00 2001
From: Normand Briere <nbriere@noware.ca>
Date: Mon, 02 Jul 2018 21:38:58 -0400
Subject: [PATCH] Hip orientation.

---
 Mocap.java |  165 +++++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 135 insertions(+), 30 deletions(-)

diff --git a/Mocap.java b/Mocap.java
index fc96f41..172ab78 100644
--- a/Mocap.java
+++ b/Mocap.java
@@ -29,7 +29,7 @@
 
     void Fade()
     {
-        if (true) // currentbones == null || CameraPane.fullreset)
+        if (currentbones == null || CameraPane.fullreset)
             return;
         
         cVector temp = new cVector();
@@ -107,7 +107,7 @@
                         //data[f3 + i] += pos[i] - data[frame3 + i];
 //                                data[f3 + i] = k*data[f3+i] + (1-k)*
 //                                        currentbones[boneframe3 + i-end];
-                        data[f3 + i] = (float)CurveAngle(data[f3+i], currenthip[i], 1-k);
+//                        data[f3 + i] = (float)CurveAngle(data[f3+i], currenthip[i], 1-k);
                     }
                     
                     // TODO: translation
@@ -225,7 +225,7 @@
     // before resetting the mocap data.
     void SetGlobalTransform()
     {
-        SetCurrentBones(frame);
+        //SetCurrentBones(frame);
         
         cVector temp = new cVector();
         cVector pos = new cVector();
@@ -235,6 +235,12 @@
         double angleYhip = 0;
         
         Object3D hip = get(0);
+        
+        if (hip.get(0).toParent == null)
+        {
+            hip.get(0).toParent = LA.newMatrix();
+            hip.get(0).fromParent = LA.newMatrix();
+        }
         
         LA.matConcat(toParent, hip.get(0).toParent, matrix);
         poship.x = matrix[3][0];
@@ -277,28 +283,28 @@
         poship.x = hip.get(0).toParent[3][0];
         poship.y = hip.get(0).toParent[3][1];
         poship.z = hip.get(0).toParent[3][2];
+
+        temp.x = 1;
+        temp.y = 0;
+        temp.z = 0;
+
+        LA.xformDir(temp, hip.get(0).toParent, temp);
+
+        angleYhip = Math.atan2(-temp.z, temp.x);
         
-        // AT*AR = A'T*BT*A'R*BR
-        // 
-        LA.matInvert(hip.get(0).toParent, matrix);
+        LA.matIdentity(matrix);
+        LA.matYRotate(matrix, angleYhip);
+        LA.matTranslate(matrix, poship.x, poship.y, poship.z);
+        
+        //LA.matInvert(hip.get(0).toParent, matrix);
+        LA.matInvert(matrix, matrix);
         
         //LA.matIdentity(matrix);
         //LA.matTranslate(matrix, -poship.x, -poship.y, -poship.z);
         
         LA.matConcat(toParent, matrix, toParent);
         
-//        poship.x = hip.get(0).toParent[3][0];
-//        poship.y = hip.get(0).toParent[3][1];
-//        poship.z = hip.get(0).toParent[3][2];
-//
-//        temp.x = 1;
-//        temp.y = 0;
-//        temp.z = 0;
-//
-//        LA.xformDir(temp, hip.get(0).toParent, temp);
-//
-//        angleYhip = Math.atan2(-temp.z, temp.x);
-        
+       
         LA.matConcat(toParent, hip.get(0).toParent, matrix);
         pos.x = hip.get(0).toParent[3][0];
         pos.y = hip.get(0).toParent[3][1];
@@ -470,6 +476,94 @@
             currenthip[i] = data[frame3 + i];
         }
     }
+
+    void SetHipOrientation()
+    {
+        Object3D hip = get(0);
+        
+        LA.matConcat(toParent, hip.get(0).toParent, matrix);
+        
+        cVector centroid = new cVector();
+        centroid.x = matrix[3][0];
+        centroid.y = matrix[3][1];
+        centroid.z = matrix[3][2];
+        this.getCentroid(centroid, true);
+        
+        cVector goal = GetGoal(centroid);
+        if (goal != null)
+        {
+            System.err.println("GOAL change (" + this + "): " + goal + " (was " + goalx + ", " + goalz + ")");
+
+            goalx = goal.x;
+            goalz = goal.z;
+            targetx = targetz = 0;
+        }
+        
+        if (goalx == 0 && goalz == 0)
+        {
+            // No target
+            if (ScriptNode.speaker != null)
+            {
+                Object3D sourcenode = GetFileRoot();
+                Object3D speakernode = ScriptNode.speaker.GetFileRoot();
+
+            //System.err.println("speaker is " + speakernode + "; last was " + ScriptNode.lastspeaker);
+            
+                Object3D speaker = ScriptNode.speaker;
+                
+                if (speakernode == sourcenode)
+                {
+                    if (ScriptNode.lastspeaker != null)
+                    {
+                        speakernode = ScriptNode.lastspeaker.GetFileRoot();
+                        speaker = ScriptNode.lastspeaker;
+                    }
+                    else
+                        return;
+                }
+
+                cVector dst = new cVector();
+                
+                boolean success = speakernode.getCentroid(dst); // , true);
+
+                if (!success)
+                    new Exception().printStackTrace();
+
+                //speakernode.parent.TransformToWorld(dst);
+                //sourcenode.parent.TransformToLocal(dst);
+                this.parent.TransformToLocal(dst);
+                
+                goalx = dst.x;
+                goalz = dst.z;
+            }
+            else
+                return;
+        }
+        
+        cVector temp = new cVector();
+        
+        temp.x = 1;
+        temp.y = 0;
+        temp.z = 0;
+
+        LA.xformDir(temp, matrix, temp);
+
+        double angleYhip = Math.atan2(-temp.z, temp.x);
+        
+        double angleYtarget = Math.atan2(goalx - centroid.x, goalz - centroid.z);
+
+        LA.matIdentity(matrix);
+        LA.matTranslate(matrix, -centroid.x, -centroid.y, -centroid.z);
+        double angle = CurveAngle(0, angleYtarget - angleYhip, 1); // 0.1f);
+        LA.matYRotate(matrix, angle);
+        LA.matTranslate(matrix, centroid.x, centroid.y, centroid.z);
+
+        LA.matConcat(matrix, toParent, toParent);
+        
+        LA.matInvert(toParent, fromParent);
+        
+        CheckForAction(centroid);
+    }
     
     void WriteTo(java.io.Writer writer) throws Exception
     {
@@ -546,8 +640,8 @@
     double targetx;
     double targetz;
     
-    double goalx;
-    double goalz;
+    double goalx = -20;
+    double goalz = -10;
     
     boolean followpath;
     
@@ -904,6 +998,8 @@
             testvect.set(from);
             testvect.sub(fromto.from);
             
+            testvect.y = 0; // PATCH
+            
             int factor = 1;
             
             //if (CameraPane.FAST)
@@ -945,6 +1041,8 @@
             
             testvect.set(from);
             testvect.sub(fromto.from);
+            
+            testvect.y = 0; // PATCH
             
             int factor = 1;
             
@@ -2237,6 +2335,8 @@
             e.printStackTrace();
         }
         
+        SetHipOrientation();
+        
     if (true)
         return;
 
@@ -2664,8 +2764,8 @@
                 // set rotation
                 //if (bone._rotationEnabled)
                 if (//frame > 0 &&
-                    !bone.skipmocap //&&
-                    //!bone.name.contains("head") &&
+                    !bone.skipmocap &&
+                    !bone.name.contains("head") // &&
                  //   !bone.name.contains("rFoot") &&
                  //   !bone.name.contains("lFoot") &&
 //                    !bone.name.contains("Pinky") &&
@@ -3020,7 +3120,8 @@
         }
     }
     
-    static int offset = 1; // 5; // 10; // 5; // 100; // skip initial "T"
+    // skip initial "T"
+    static int offset = 1; // 5; // 10; // 5; // 100;
     
     void ReadBVH()
     {
@@ -3049,8 +3150,8 @@
     
     void Reset()
     {
-        if (fullname != null)
-            ReadBVH();
+//        if (fullname != null)
+//            ReadBVH();
         
         Object3D hip = get(0);
         
@@ -3089,7 +3190,7 @@
 //        LA.matIdentity(rot.fromParent);
 
         // july 2014
-        goalx = goalz = 0;
+//        goalx = goalz = 0;
         targetx = targetz = 0;
         
         //SetPositionDelta(true, true, true, false);
@@ -3132,7 +3233,7 @@
         baseframe += step;
         
         //frame = baseframe;
-        frame += step;
+        //frame += step;
         
 //        if (frame != baseframe)
 //        {
@@ -3184,13 +3285,17 @@
                     }
                 }
             }
+            
+            frame = GetFirstFrame();
         }
+        else
+            frame += step;
         
  //SetPositionDelta(false);
             
-        if (frame >= bvh.animation.getNumFrames())
-            //baseframe =
-                    frame = GetFirstFrame(); // 0; // offset; // initial point
+//        if (frame >= bvh.animation.getNumFrames())
+//            //baseframe =
+//                    frame = GetFirstFrame(); // 0; // offset; // initial point
         
         //System.err.println("frame = " + frame);
         if (lastframe != 0)

--
Gitblit v1.6.2