From 767be784dc7fe293bf5c5ee6507df242526be3ed Mon Sep 17 00:00:00 2001
From: Normand Briere <nbriere@noware.ca>
Date: Tue, 24 Sep 2019 02:10:06 -0400
Subject: [PATCH] Rag doll is back.

---
 ObjEditor.java                                    |    2 
 BBox.java                                         |   65 +++++-----
 CameraPane.java                                   |    9 +
 PhysicsNode.java                                  |   38 +++--
 GroupEditor.java                                  |   14 +-
 com/bulletphysics/collision/shapes/ShapeHull.java |    7 +
 com/bulletphysics/dynamics/RigidBody.java         |   22 +++
 Object3D.java                                     |   60 +++++++--
 PhysicsEditor.java                                |    5 
 RagDoll.java                                      |   86 ++++++++------
 GenericJoint.java                                 |    5 
 11 files changed, 199 insertions(+), 114 deletions(-)

diff --git a/BBox.java b/BBox.java
index 4a28409..418bd06 100755
--- a/BBox.java
+++ b/BBox.java
@@ -104,12 +104,6 @@
         double t1 = (min.x - ray.eyePoint.x) / ray.viewDirection.x;
         double t2 = (max.x - ray.eyePoint.x) / ray.viewDirection.x;
 
-        double mint = t1;
-        if (mint > t2) mint = t2;
-        
-        double maxt = t1;
-        if (maxt < t2) maxt = t2;
-        
         if (ray.viewDirection.x == 0)
         {
             if (ray.eyePoint.x <= min.x || ray.eyePoint.x >= max.x)
@@ -117,18 +111,16 @@
         }
         else
         {
-            if (tmin > mint) tmin = mint;
-            if (tmax < maxt) tmax = maxt;
-        }
-        
-        t1 = (min.y - ray.eyePoint.y) / ray.viewDirection.y;
-        t2 = (max.y - ray.eyePoint.y) / ray.viewDirection.y;
+            if (t1 > t2)
+            {
+                double t = t1;
+                t1 = t2;
+                t2 = t;
+            }
 
-        mint = t1;
-        if (mint > t2) mint = t2;
-        
-        maxt = t1;
-        if (maxt < t2) maxt = t2;
+            if (tmin > t1) tmin = t1;
+            if (tmax < t2) tmax = t2;
+        }
         
         if (ray.viewDirection.y == 0)
         {
@@ -137,18 +129,19 @@
         }
         else
         {
-            if (tmin > mint) tmin = mint;
-            if (tmax < maxt) tmax = maxt;
-        }
-        
-        t1 = (min.z - ray.eyePoint.z) / ray.viewDirection.z;
-        t2 = (max.z - ray.eyePoint.z) / ray.viewDirection.z;
+            t1 = (min.y - ray.eyePoint.y) / ray.viewDirection.y;
+            t2 = (max.y - ray.eyePoint.y) / ray.viewDirection.y;
 
-        mint = t1;
-        if (mint > t2) mint = t2;
-        
-        maxt = t1;
-        if (maxt < t2) maxt = t2;
+            if (t1 > t2)
+            {
+                double t = t1;
+                t1 = t2;
+                t2 = t;
+            }
+
+            if (tmin > t1) tmin = t1;
+            if (tmax < t2) tmax = t2;
+        }
         
         if (ray.viewDirection.z == 0)
         {
@@ -157,10 +150,20 @@
         }
         else
         {
-            if (tmin > mint) tmin = mint;
-            if (tmax < maxt) tmax = maxt;
+            t1 = (min.z - ray.eyePoint.z) / ray.viewDirection.z;
+            t2 = (max.z - ray.eyePoint.z) / ray.viewDirection.z;
+
+            if (t1 > t2)
+            {
+                double t = t1;
+                t1 = t2;
+                t2 = t;
+            }
+
+            if (tmin > t1) tmin = t1;
+            if (tmax < t2) tmax = t2;
         }
         
-        return tmax > tmin && tmax > 0.0;
+        return tmax >= tmin && tmax > 0.0;
     }
 }
diff --git a/CameraPane.java b/CameraPane.java
index 7627a95..f35d440 100644
--- a/CameraPane.java
+++ b/CameraPane.java
@@ -1,4 +1,5 @@
 
+import com.bulletphysics.dynamics.RigidBody;
 import java.awt.*;
 import java.awt.event.*;
 import java.awt.image.*;
@@ -14667,6 +14668,14 @@
         // only one thread!? synchronized
                 void StepToTarget(boolean jump)
         {
+            RigidBody.pos.x = (float)manipCamera.lookAt.x;
+            RigidBody.pos.y = (float)manipCamera.lookAt.y;
+            RigidBody.pos.z = (float)manipCamera.lookAt.z;
+//            RigidBody.wind.x = (float)manipCamera.right.x;
+//            RigidBody.wind.y = (float)manipCamera.right.y;
+//            RigidBody.wind.z = (float)manipCamera.right.z;
+            //RigidBody.wind.normalize();
+                    
             if (mute)
                 return;
             
diff --git a/GenericJoint.java b/GenericJoint.java
index 4c7c0e0..9c45117 100644
--- a/GenericJoint.java
+++ b/GenericJoint.java
@@ -177,7 +177,8 @@
 
     public void spawnRagdoll(boolean walk) // random)
     {
-        RagDoll ragDoll = new RagDoll(null, new Vector3f(0f, 0f, 0f), 0.45f, walk); // Math.random() > 0.5/*true*/); // true);
+        // KIDS4
+        RagDoll ragDoll = new RagDoll(null, new Vector3f(0f, 0f, 0f), 0.45f/2, walk); // Math.random() > 0.5/*true*/); // true);
         ragdolls.add(ragDoll);
         
         bRep = null;
@@ -309,7 +310,7 @@
             walkdefined = true;
         }
         
-        ragdolls.get(0).init(/*staticdynamicsWorld,*/ new Vector3f(0f, 0f, 0f), 0.45f, walk);
+        ragdolls.get(0).init(/*staticdynamicsWorld,*/ new Vector3f(0f, 0f, 0f), ragdolls.get(0).scale, walk);
         
         bRep = null;
     }
diff --git a/GroupEditor.java b/GroupEditor.java
index d328971..13ef3d2 100644
--- a/GroupEditor.java
+++ b/GroupEditor.java
@@ -2515,13 +2515,13 @@
                     ragdoll.toParent = LA.newMatrix();
                     ragdoll.fromParent = LA.newMatrix();
                     
-                    ragdoll.fromParent[1][1] = 1/1.03;
-                    ragdoll.toParent[1][1] = 1.03;
-                    
-                    ragdoll.fromParent[3][1] = 0.02;
-                    ragdoll.toParent[3][1] = -0.02;
-                    ragdoll.fromParent[3][2] = 0.01;
-                    ragdoll.toParent[3][2] = -0.01;
+//                    ragdoll.fromParent[1][1] = 1/1.03;
+//                    ragdoll.toParent[1][1] = 1.03;
+//                    
+//                    ragdoll.fromParent[3][1] = 0.02;
+//                    ragdoll.toParent[3][1] = -0.02;
+//                    ragdoll.fromParent[3][2] = 0.01;
+//                    ragdoll.toParent[3][2] = -0.01;
                     
                     makeSomething(ragdoll);
 			//makeSomething(new VehicleDemo());
diff --git a/ObjEditor.java b/ObjEditor.java
index d5be27b..c8eff94 100644
--- a/ObjEditor.java
+++ b/ObjEditor.java
@@ -4617,7 +4617,7 @@
         
         ResetModel();
         
-        DuplicateVersion();
+        //DuplicateVersion();
         
         this.SetVersionStates();
         objEditor.refreshContents();
diff --git a/Object3D.java b/Object3D.java
index ba7e5c6..b4eaccb 100644
--- a/Object3D.java
+++ b/Object3D.java
@@ -1023,7 +1023,7 @@
     
     void Step()
     {
-        // marde pour serialization de Texture
+        // patch pour serialization de Texture
         resetmaxcount();
         resettransformcount();
         resetstep();
@@ -4101,11 +4101,6 @@
         Step();
         Touch();
         
-        StepRecur();
-    }
-    
-    void StepRecur()
-    {
         for (int i = 0; i < size(); i++)
         {
             Object3D child = (Object3D) get(i); // reserve(i);
@@ -6182,6 +6177,10 @@
         return live && bRep != null;
     }
     
+    static cVector minima = new cVector();
+    static cVector maxima = new cVector();
+    static javax.vecmath.Point3d center = new javax.vecmath.Point3d();
+    
     void Draw(iCameraPane display, Object3D /*Composite*/ root, boolean selected, boolean blocked)
     {
         Invariants(); // june 2013
@@ -6189,6 +6188,29 @@
         if (support != null)
         {
         //    System.err.println("Draw " + this + " Frame # " + ((Mocap)((Merge)support).object).frame);
+        }
+        
+        if (live && link2master && support == null && !this.marked) // project on ground
+        {
+            getBounds(minima, maxima, true);
+            center.x = (minima.x + maxima.x) / 2;
+            center.y = 10000; // (minima.y + maxima.y) / 2;
+            center.z = (minima.z + maxima.z) / 2;
+
+            Ray ray = new Ray(center, new Vector3d(0,-1,0));
+
+            IntersectResult res = new IntersectResult();
+            res.t = Double.POSITIVE_INFINITY;
+
+            if (Grafreed.grafreed.universe.intersect(ray, res))
+            {
+                double resx = ray.eyePoint.x + ray.viewDirection.x * res.t;
+                double resy = ray.eyePoint.y + ray.viewDirection.y * res.t;
+                double resz = ray.eyePoint.z + ray.viewDirection.z * res.t;
+                
+                LA.matTranslate(toParent, 0, resy - minima.y, 0);
+                LA.matInvert(toParent, fromParent);
+            }
         }
         
         if (display.DrawMode() == iCameraPane.SELECTION &&
@@ -6985,7 +7007,7 @@
                         facescompare[k] = new FaceCompare(k);
                     }
                     
-                    center = new cVector();
+                    centertriangle = new cVector();
                 }
                 else
                 {
@@ -7108,7 +7130,7 @@
          */
     }
 
-    transient cVector center;
+    transient cVector centertriangle;
     
     class FaceCompare implements Comparable
     {
@@ -7137,14 +7159,14 @@
                 Vertex q = bRep.GetVertex(face.q);
                 Vertex r = bRep.GetVertex(face.r);
 
-                center.set(p);
-                center.add(q);
-                center.add(r);
-                center.mul(1.0/3);
+                centertriangle.set(p);
+                centertriangle.add(q);
+                centertriangle.add(r);
+                centertriangle.mul(1.0/3);
                 
-                center.sub(Globals.theRenderer.EyeCamera().location);
+                centertriangle.sub(Globals.theRenderer.EyeCamera().location);
                 
-                distance = center.dot(center);
+                distance = centertriangle.dot(centertriangle);
             }
             
             return distance;
@@ -8903,7 +8925,7 @@
                 cVector min = new cVector();
                 cVector max = new cVector();
                 
-                this.getBounds(min, max, false);
+                this.getBounds(min, max, true);
                 
                 bbox.min.x = min.x;
                 bbox.min.y = min.y;
@@ -8914,7 +8936,7 @@
                 bbox.max.z = max.z;
             }
             
-            if (bbox.intersect(ray, result))
+            if (true) // NOT WORKING bbox.intersect(ray, result))
             {
                 success |= intersectMesh(ray, result);
             }
@@ -8958,6 +8980,12 @@
     
     private boolean intersectTriangle(Ray ray, IntersectResult result, Vertex v1, Vertex v2, Vertex v3)
     {
+        if (false)
+        {
+            result.t = 0;
+            return true;
+        }
+        
         /*
         Fast, Minimum Storage Ray/Triangle Intersection, Moller et al.
         
diff --git a/PhysicsEditor.java b/PhysicsEditor.java
index d75e46b..bc1c8c8 100644
--- a/PhysicsEditor.java
+++ b/PhysicsEditor.java
@@ -70,7 +70,7 @@
              */
             super.SetupUI2(oe);
                 
-        gravityPanel = AddSlider(oe.ctrlPanel, "Gravity:", 0, 20.0, physicsnode.gravity, 1);
+        gravityPanel = AddSlider(oe.ctrlPanel, "Gravity:", 0, 100.0, physicsnode.gravity, 1);
         gravityField = (cNumberSlider)gravityPanel.getComponent(1);
                 
 //        oe.ctrlPanel.add(gravityLabel = new JLabel("Gravity: ")); //, oe.aConstraints, oe.ctrlPanel.getComponentCount()-2);
@@ -79,7 +79,7 @@
         
         oe.ctrlPanel.Return();
         
-        windPanel = AddSlider(oe.ctrlPanel, "Wind:", 0, 20.0, physicsnode.wind, 1);
+        windPanel = AddSlider(oe.ctrlPanel, "Wind:", 0, 100.0, physicsnode.wind, 1);
         windField = (cNumberSlider)windPanel.getComponent(1);
         
 //        oe.ctrlPanel.add(windLabel = new JLabel("Wind: ")); //, oe.aConstraints, oe.ctrlPanel.getComponentCount()-2);
@@ -203,6 +203,7 @@
         physicsnode.floor = (float)floorField.getFloat(); // biparam.minUDivs, 99);
         //biparam.name = nameField.getText();
         
+        physicsnode.transientDynamicsWorld.setGravity(new javax.vecmath.Vector3f(0, -physicsnode.gravity, physicsnode.wind));
 // june 2013		super.applySelf();
     }
     /**/
diff --git a/PhysicsNode.java b/PhysicsNode.java
index 77051fd..ca3a422 100644
--- a/PhysicsNode.java
+++ b/PhysicsNode.java
@@ -78,6 +78,8 @@
         super("Physics");
         
         live = true;
+        
+        gravity = 1;
     }
     
     void Reset()
@@ -193,39 +195,41 @@
             //staticdynamicsWorld = dynamicsWorld;
 //		dynamicsWorld.setDebugDrawer(new GLDebugDrawer(gl));
 
+            float boxsize = 100;
+            
         // Setup a big ground box
 //        {
-            CollisionShape groundShape = new BoxShape(new Vector3f(200f, 10f, 200f));
+            CollisionShape groundShape = new BoxShape(new Vector3f(boxsize, 10f, boxsize));
             Transform groundTransform = new Transform();
             groundTransform.setIdentity();
             groundTransform.origin.set(0f, -floor - 10, 0f);
             localCreateRigidBody(0f, groundTransform, groundShape);
             
-            groundShape = new BoxShape(new Vector3f(200f, 200f, 200f));
+            groundShape = new BoxShape(new Vector3f(boxsize, boxsize, boxsize));
             groundTransform = new Transform();
             groundTransform.setIdentity();
-            groundTransform.origin.set(0f, 100, -201f);
-            //localCreateRigidBody(0f, groundTransform, groundShape);
+            groundTransform.origin.set(0f, boxsize/2, -boxsize-1);
+            localCreateRigidBody(0f, groundTransform, groundShape);
             
-            groundShape = new BoxShape(new Vector3f(200f, 200f, 200f));
+            groundShape = new BoxShape(new Vector3f(boxsize, boxsize, boxsize));
             groundTransform = new Transform();
             groundTransform.setIdentity();
-            groundTransform.origin.set(-201f, 100, 0f);
-            //localCreateRigidBody(0f, groundTransform, groundShape);
+            groundTransform.origin.set(-boxsize-1, boxsize/2, 0f);
+            localCreateRigidBody(0f, groundTransform, groundShape);
             
-            groundShape = new BoxShape(new Vector3f(200f, 200f, 200f));
+            groundShape = new BoxShape(new Vector3f(boxsize, boxsize, boxsize));
             groundTransform = new Transform();
             groundTransform.setIdentity();
-            groundTransform.origin.set(201f, 100, 0f);
-            //localCreateRigidBody(0f, groundTransform, groundShape);
+            groundTransform.origin.set(boxsize+1, boxsize/2, 0f);
+            localCreateRigidBody(0f, groundTransform, groundShape);
             
-            groundShape = new BoxShape(new Vector3f(200f, 200f, 200f));
+            groundShape = new BoxShape(new Vector3f(boxsize, boxsize, boxsize));
             groundTransform = new Transform();
             groundTransform.setIdentity();
-            groundTransform.origin.set(0f, 100, 201f);
-            //localCreateRigidBody(0f, groundTransform, groundShape);
+            groundTransform.origin.set(0f, boxsize/2, boxsize+1);
+            localCreateRigidBody(0f, groundTransform, groundShape);
             
-            transientDynamicsWorld.setGravity(new Vector3f(0, -gravity, wind));
+            transientDynamicsWorld.setGravity(new Vector3f(0.075f, -gravity, wind));
             
             SetDynamics(transientDynamicsWorld);
         }
@@ -251,7 +255,7 @@
 //        myoffset = dynamicsWorld.getNumCollisionObjects() - 1;
 //        
 //        if (myoffset == 0)
-            clientResetScene();
+//            clientResetScene();
     }
 
     public RigidBody localCreateRigidBody(float mass, Transform startTransform, CollisionShape shape)
@@ -271,7 +275,7 @@
         //#ifdef USE_MOTIONSTATE
         DefaultMotionState myMotionState = new DefaultMotionState(startTransform);
 
-        float friction = 1;
+        float friction = 1f;
         
         RigidBodyConstructionInfo cInfo = new RigidBodyConstructionInfo(mass, myMotionState, shape, localInertia, friction);
 
@@ -288,7 +292,7 @@
 
     public void clientResetScene()
     {
-//        System.exit(0);
+        System.exit(0);
         //#ifdef SHOW_NUM_DEEP_PENETRATIONS
         BulletStats.gNumDeepPenetrationChecks = 0;
         BulletStats.gNumGjkChecks = 0;
diff --git a/RagDoll.java b/RagDoll.java
index a0d1819..7e67857 100644
--- a/RagDoll.java
+++ b/RagDoll.java
@@ -123,6 +123,8 @@
 		JOINT_COUNT
 	}
 
+        float scale;
+        
 	//DynamicsWorld ownerWorld;
 	CollisionShape[] shapes = new CollisionShape[BodyPart.BODYPART_COUNT.ordinal()];
 	RigidBody/*Serial*/[] bodies = new RigidBody/*Serial*/[BodyPart.BODYPART_COUNT.ordinal()];
@@ -140,6 +142,8 @@
         
 	public void init(/*DynamicsWorld ownerWorld,*/ Vector3f positionOffset, float scale_ragdoll, boolean walk)
         {
+            scale = scale_ragdoll;
+            
                 this.walkmotion = walk;
                 
                 boolean hasbodies = (bodies[BodyPart.BODYPART_SPINE.ordinal()] != null);
@@ -149,32 +153,40 @@
 		Transform tmpTrans = new Transform();
 		Vector3f tmp = new Vector3f();
 
-                boolean node = true;
+                boolean node = false; // true;
+                
+                // KIDS4
+                float upperarmlength = 0.3f; // 0.25
+                float lowerarmlength = 0.25f; // 0.2
+                float handlength = 0.1f; // 0.1
+                float upperarmdist = 0.15f;
                 
 		// Setup the geometry
 		//shapes[BodyPart.BODYPART_GROUND.ordinal()] = new CapsuleShape(scale_ragdoll * 0.125f, scale_ragdoll * 0.20f);
                 //scale_ragdoll /= 1.25;
                 if (!hasbodies)
                 {
-                    shapes[BodyPart.BODYPART_PELVIS.ordinal()] = new CapsuleShape(scale_ragdoll * 0.15f, scale_ragdoll * 0.35f,false);
-                    shapes[BodyPart.BODYPART_SPINE.ordinal()] = new CapsuleShape(scale_ragdoll * 0.2f, scale_ragdoll * 0.5f,false);
-                    shapes[BodyPart.BODYPART_HEAD.ordinal()] = new CapsuleShape(scale_ragdoll * 0.15f, scale_ragdoll * 0.15f,false);
+                    float thin = 0.1f;
+                    
+                    shapes[BodyPart.BODYPART_PELVIS.ordinal()] = new CapsuleShape(scale_ragdoll * 0.15f, scale_ragdoll * 0.35f, false);
+                    shapes[BodyPart.BODYPART_SPINE.ordinal()] = new CapsuleShape(scale_ragdoll * 0.2f, scale_ragdoll * 0.5f, false);
+                    shapes[BodyPart.BODYPART_HEAD.ordinal()] = new CapsuleShape(scale_ragdoll * 0.15f, scale_ragdoll * 0.15f, false);
 
-                    shapes[BodyPart.BODYPART_LEFT_UPPER_LEG.ordinal()] = new CapsuleShape(scale_ragdoll * 0.1f, scale_ragdoll * 0.4f,false);
-                    shapes[BodyPart.BODYPART_LEFT_LOWER_LEG.ordinal()] = new CapsuleShape(scale_ragdoll * 0.06f, scale_ragdoll * 0.37f,node);
-                    shapes[BodyPart.BODYPART_LEFT_FOOT.ordinal()] = new CapsuleShape(scale_ragdoll * 0.04f, scale_ragdoll * 0.1f,node);
+                    shapes[BodyPart.BODYPART_LEFT_UPPER_LEG.ordinal()] = new CapsuleShape(scale_ragdoll * 0.4f * thin, scale_ragdoll * 0.4f, node);
+                    shapes[BodyPart.BODYPART_LEFT_LOWER_LEG.ordinal()] = new CapsuleShape(scale_ragdoll * 0.37f * thin, scale_ragdoll * 0.37f, node);
+                    shapes[BodyPart.BODYPART_LEFT_FOOT.ordinal()] = new CapsuleShape(scale_ragdoll * 0.1f * thin, scale_ragdoll * 0.1f, node);
 
-                    shapes[BodyPart.BODYPART_RIGHT_UPPER_LEG.ordinal()] = new CapsuleShape(scale_ragdoll * 0.1f, scale_ragdoll * 0.4f,false);
-                    shapes[BodyPart.BODYPART_RIGHT_LOWER_LEG.ordinal()] = new CapsuleShape(scale_ragdoll * 0.06f, scale_ragdoll * 0.37f,node);
-                    shapes[BodyPart.BODYPART_RIGHT_FOOT.ordinal()] = new CapsuleShape(scale_ragdoll * 0.04f, scale_ragdoll * 0.1f,node);
+                    shapes[BodyPart.BODYPART_RIGHT_UPPER_LEG.ordinal()] = new CapsuleShape(scale_ragdoll * 0.4f * thin, scale_ragdoll * 0.4f, node);
+                    shapes[BodyPart.BODYPART_RIGHT_LOWER_LEG.ordinal()] = new CapsuleShape(scale_ragdoll * 0.37f * thin, scale_ragdoll * 0.37f, node);
+                    shapes[BodyPart.BODYPART_RIGHT_FOOT.ordinal()] = new CapsuleShape(scale_ragdoll * 0.1f * thin, scale_ragdoll * 0.1f, node);
 
-                    shapes[BodyPart.BODYPART_LEFT_UPPER_ARM.ordinal()] = new CapsuleShape(scale_ragdoll * 0.08f, scale_ragdoll * 0.25f,node);
-                    shapes[BodyPart.BODYPART_LEFT_LOWER_ARM.ordinal()] = new CapsuleShape(scale_ragdoll * 0.06f, scale_ragdoll * 0.2f,node);
-                    shapes[BodyPart.BODYPART_LEFT_HAND.ordinal()] = new CapsuleShape(scale_ragdoll * 0.04f, scale_ragdoll * 0.1f,node);
+                    shapes[BodyPart.BODYPART_LEFT_UPPER_ARM.ordinal()] = new CapsuleShape(scale_ragdoll * upperarmlength * thin, scale_ragdoll * upperarmlength, node);
+                    shapes[BodyPart.BODYPART_LEFT_LOWER_ARM.ordinal()] = new CapsuleShape(scale_ragdoll * lowerarmlength * thin, scale_ragdoll * lowerarmlength, node);
+                    shapes[BodyPart.BODYPART_LEFT_HAND.ordinal()] = new CapsuleShape(scale_ragdoll * handlength * thin, scale_ragdoll * handlength, node);
 
-                    shapes[BodyPart.BODYPART_RIGHT_UPPER_ARM.ordinal()] = new CapsuleShape(scale_ragdoll * 0.08f, scale_ragdoll * 0.25f,node);
-                    shapes[BodyPart.BODYPART_RIGHT_LOWER_ARM.ordinal()] = new CapsuleShape(scale_ragdoll * 0.06f, scale_ragdoll * 0.2f,node);
-                    shapes[BodyPart.BODYPART_RIGHT_HAND.ordinal()] = new CapsuleShape(scale_ragdoll * 0.04f, scale_ragdoll * 0.1f,node);
+                    shapes[BodyPart.BODYPART_RIGHT_UPPER_ARM.ordinal()] = new CapsuleShape(scale_ragdoll * upperarmlength * thin, scale_ragdoll * upperarmlength, node);
+                    shapes[BodyPart.BODYPART_RIGHT_LOWER_ARM.ordinal()] = new CapsuleShape(scale_ragdoll * lowerarmlength * thin, scale_ragdoll * lowerarmlength, node);
+                    shapes[BodyPart.BODYPART_RIGHT_HAND.ordinal()] = new CapsuleShape(scale_ragdoll * handlength * thin, scale_ragdoll * handlength, node);
                 }
                 //scale_ragdoll *= 1.25;
 
@@ -185,7 +197,7 @@
 
 		Transform transform = new Transform();
                 
-                float depth = -0.02f; // .02f;
+                float depth = -0.01f; // .02f;
                 
 //		transform.setIdentity();
 //		transform.origin.set(0f, scale_ragdoll * 1f, 0f);
@@ -242,7 +254,7 @@
 		transform.setIdentity();
 		if ((body = bodies[BodyPart.BODYPART_LEFT_FOOT.ordinal()]) == null)
                     body = bodies[BodyPart.BODYPART_LEFT_FOOT.ordinal()] = localCreateRigidBody(massfeet, poisefeet, transform, shapes[BodyPart.BODYPART_LEFT_FOOT.ordinal()]);
-		transform.origin.set(-0.09f * scale_ragdoll, 0.075f * scale_ragdoll, 0.025f);
+		transform.origin.set(-0.09f * scale_ragdoll, 0.075f * scale_ragdoll, 0.0125f);
 		MatrixUtil.setEulerZYX(transform.basis, BulletGlobals.SIMD_PI/2, 0,BulletGlobals.SIMD_PI); // BulletGlobals.SIMD_PI, 0);
 		tmpTrans.mul(offset, transform);
 		body.worldTransform.set(tmpTrans);
@@ -252,7 +264,7 @@
 		transform.setIdentity();
 		if ((body = bodies[BodyPart.BODYPART_RIGHT_FOOT.ordinal()]) == null)
                     body = bodies[BodyPart.BODYPART_RIGHT_FOOT.ordinal()] = localCreateRigidBody(massfeet, poisefeet, transform, shapes[BodyPart.BODYPART_RIGHT_FOOT.ordinal()]);
-		transform.origin.set(0.09f * scale_ragdoll, 0.075f * scale_ragdoll, 0.025f);
+		transform.origin.set(0.09f * scale_ragdoll, 0.075f * scale_ragdoll, 0.0125f);
 		MatrixUtil.setEulerZYX(transform.basis, BulletGlobals.SIMD_PI/2, 0,BulletGlobals.SIMD_PI); // BulletGlobals.SIMD_PI, 0);
 		tmpTrans.mul(offset, transform);
 		body.worldTransform.set(tmpTrans);
@@ -300,7 +312,7 @@
 		transform.setIdentity();
 		if ((body = bodies[BodyPart.BODYPART_LEFT_UPPER_ARM.ordinal()]) == null)
                     body = bodies[BodyPart.BODYPART_LEFT_UPPER_ARM.ordinal()] = localCreateRigidBody(1f, 0, transform, shapes[BodyPart.BODYPART_LEFT_UPPER_ARM.ordinal()]);
-		transform.origin.set(-0.275f * scale_ragdoll, 1.325f * scale_ragdoll, depth);
+		transform.origin.set(-(upperarmlength/2 + upperarmdist) * scale_ragdoll, 1.325f * scale_ragdoll, depth);
 		MatrixUtil.setEulerZYX(transform.basis, 0, 0, BulletGlobals.SIMD_HALF_PI);
 		tmpTrans.mul(offset, transform);
 		body.worldTransform.set(tmpTrans);
@@ -310,7 +322,7 @@
 		transform.setIdentity();
 		if ((body = bodies[BodyPart.BODYPART_LEFT_LOWER_ARM.ordinal()]) == null)
                     body = bodies[BodyPart.BODYPART_LEFT_LOWER_ARM.ordinal()] = localCreateRigidBody(1f, 0, transform, shapes[BodyPart.BODYPART_LEFT_LOWER_ARM.ordinal()]);
-		transform.origin.set(-0.5f * scale_ragdoll, 1.325f * scale_ragdoll, depth);
+		transform.origin.set(-(lowerarmlength/2 + upperarmlength + upperarmdist) * scale_ragdoll, 1.325f * scale_ragdoll, depth);
 		MatrixUtil.setEulerZYX(transform.basis, 0, 0, BulletGlobals.SIMD_HALF_PI);
 		tmpTrans.mul(offset, transform);
 		body.worldTransform.set(tmpTrans);
@@ -320,7 +332,7 @@
 		transform.setIdentity();
 		if ((body = bodies[BodyPart.BODYPART_LEFT_HAND.ordinal()]) == null)
                     body = bodies[BodyPart.BODYPART_LEFT_HAND.ordinal()] = localCreateRigidBody(1f, 0, transform, shapes[BodyPart.BODYPART_LEFT_HAND.ordinal()]);
-		transform.origin.set(-0.65f * scale_ragdoll, 1.325f * scale_ragdoll, depth);
+		transform.origin.set(-(handlength/2 + lowerarmlength + upperarmlength + upperarmdist) * scale_ragdoll, 1.325f * scale_ragdoll, depth);
 		MatrixUtil.setEulerZYX(transform.basis, 0, 0, BulletGlobals.SIMD_HALF_PI);
 		tmpTrans.mul(offset, transform);
 		body.worldTransform.set(tmpTrans);
@@ -330,7 +342,7 @@
 		transform.setIdentity();
 		if ((body = bodies[BodyPart.BODYPART_RIGHT_UPPER_ARM.ordinal()]) == null)
                     body = bodies[BodyPart.BODYPART_RIGHT_UPPER_ARM.ordinal()] = localCreateRigidBody(1f, 0, transform, shapes[BodyPart.BODYPART_RIGHT_UPPER_ARM.ordinal()]);
-		transform.origin.set(0.275f * scale_ragdoll, 1.325f * scale_ragdoll, depth);
+		transform.origin.set((upperarmlength/2 + upperarmdist) * scale_ragdoll, 1.325f * scale_ragdoll, depth);
 		MatrixUtil.setEulerZYX(transform.basis, 0, 0, -BulletGlobals.SIMD_HALF_PI);
 		tmpTrans.mul(offset, transform);
 		body.worldTransform.set(tmpTrans);
@@ -340,7 +352,7 @@
 		transform.setIdentity();
 		if ((body = bodies[BodyPart.BODYPART_RIGHT_LOWER_ARM.ordinal()]) == null)
                     body = bodies[BodyPart.BODYPART_RIGHT_LOWER_ARM.ordinal()] = localCreateRigidBody(1f, 0, transform, shapes[BodyPart.BODYPART_RIGHT_LOWER_ARM.ordinal()]);
-		transform.origin.set(0.50f * scale_ragdoll, 1.325f * scale_ragdoll, depth);
+		transform.origin.set((lowerarmlength/2 + upperarmlength + upperarmdist) * scale_ragdoll, 1.325f * scale_ragdoll, depth);
 		MatrixUtil.setEulerZYX(transform.basis, 0, 0, -BulletGlobals.SIMD_HALF_PI);
 		tmpTrans.mul(offset, transform);
 		body.worldTransform.set(tmpTrans);
@@ -350,7 +362,7 @@
 		transform.setIdentity();
 		if ((body = bodies[BodyPart.BODYPART_RIGHT_HAND.ordinal()]) == null)
                     body = bodies[BodyPart.BODYPART_RIGHT_HAND.ordinal()] = localCreateRigidBody(1f, 0, transform, shapes[BodyPart.BODYPART_RIGHT_HAND.ordinal()]);
-		transform.origin.set(0.65f * scale_ragdoll, 1.325f * scale_ragdoll, depth);
+		transform.origin.set((handlength/2 + lowerarmlength + upperarmlength + upperarmdist) * scale_ragdoll, 1.325f * scale_ragdoll, depth);
 		MatrixUtil.setEulerZYX(transform.basis, 0, 0, -BulletGlobals.SIMD_HALF_PI);
 		tmpTrans.mul(offset, transform);
 		body.worldTransform.set(tmpTrans);
@@ -418,10 +430,10 @@
 			localA.setIdentity();
 			localB.setIdentity();
 
-			localA.origin.set(-0.15f * scale_ragdoll, 0.125f * scale_ragdoll, depth);
+			localA.origin.set(-upperarmdist * scale_ragdoll, 0.125f * scale_ragdoll, depth);
 
 			MatrixUtil.setEulerZYX(localB.basis, 0/*BulletGlobals.SIMD_HALF_PI*/, 0, -BulletGlobals.SIMD_HALF_PI);
-			localB.origin.set(0f, -0.125f * scale_ragdoll, 0);
+			localB.origin.set(0f, -upperarmlength/2 * scale_ragdoll, 0);
 
 			joint6DOF = new Generic6DofConstraint(bodies[BodyPart.BODYPART_SPINE.ordinal()], bodies[BodyPart.BODYPART_LEFT_UPPER_ARM.ordinal()], localA, localB, useLinearReferenceFrameA);
 
@@ -447,9 +459,9 @@
 			localA.setIdentity();
 			localB.setIdentity();
 
-			localA.origin.set(0.15f * scale_ragdoll, 0.125f * scale_ragdoll, depth);
+			localA.origin.set(upperarmdist * scale_ragdoll, 0.125f * scale_ragdoll, depth);
 			MatrixUtil.setEulerZYX(localB.basis, 0, 0, BulletGlobals.SIMD_HALF_PI);
-			localB.origin.set(0f, -0.125f * scale_ragdoll, 0);
+			localB.origin.set(0f, -upperarmlength/2 * scale_ragdoll, 0);
 			joint6DOF = new Generic6DofConstraint(bodies[BodyPart.BODYPART_SPINE.ordinal()], bodies[BodyPart.BODYPART_RIGHT_UPPER_ARM.ordinal()], localA, localB, useLinearReferenceFrameA);
 
 			//#ifdef RIGID
@@ -474,8 +486,8 @@
 			localA.setIdentity();
 			localB.setIdentity();
 
-			localA.origin.set(0f, 0.125f * scale_ragdoll, 0f);
-			localB.origin.set(0f, -0.1f * scale_ragdoll, 0f);
+			localA.origin.set(0f, upperarmlength/2 * scale_ragdoll, 0f);
+			localB.origin.set(0f, -lowerarmlength/2 * scale_ragdoll, 0f);
 			joint6DOF = new Generic6DofConstraint(bodies[BodyPart.BODYPART_LEFT_UPPER_ARM.ordinal()], bodies[BodyPart.BODYPART_LEFT_LOWER_ARM.ordinal()], localA, localB, useLinearReferenceFrameA);
 
 			//#ifdef RIGID
@@ -500,8 +512,8 @@
 			localA.setIdentity();
 			localB.setIdentity();
 
-			localA.origin.set(0f, 0.125f * scale_ragdoll, 0f);
-			localB.origin.set(0f, -0.1f * scale_ragdoll, 0f);
+			localA.origin.set(0f, upperarmlength/2 * scale_ragdoll, 0f);
+			localB.origin.set(0f, -lowerarmlength/2 * scale_ragdoll, 0f);
 			joint6DOF = new Generic6DofConstraint(bodies[BodyPart.BODYPART_RIGHT_UPPER_ARM.ordinal()], bodies[BodyPart.BODYPART_RIGHT_LOWER_ARM.ordinal()], localA, localB, useLinearReferenceFrameA);
 
 			//#ifdef RIGID
@@ -526,8 +538,8 @@
 			localA.setIdentity();
 			localB.setIdentity();
 
-			localA.origin.set(0f, 0.1f * scale_ragdoll, 0f);
-			localB.origin.set(0f, -0.05f * scale_ragdoll, 0f);
+			localA.origin.set(0f, lowerarmlength/2 * scale_ragdoll, 0f);
+			localB.origin.set(0f, -handlength/2 * scale_ragdoll, 0f);
 			joint6DOF = new Generic6DofConstraint(bodies[BodyPart.BODYPART_LEFT_LOWER_ARM.ordinal()], bodies[BodyPart.BODYPART_LEFT_HAND.ordinal()], localA, localB, useLinearReferenceFrameA);
 
 			//#ifdef RIGID
@@ -552,8 +564,8 @@
 			localA.setIdentity();
 			localB.setIdentity();
 
-			localA.origin.set(0f, 0.1f * scale_ragdoll, 0f);
-			localB.origin.set(0f, -0.05f * scale_ragdoll, 0f);
+			localA.origin.set(0f, lowerarmlength/2 * scale_ragdoll, 0f);
+			localB.origin.set(0f, -handlength/2 * scale_ragdoll, 0f);
 			joint6DOF = new Generic6DofConstraint(bodies[BodyPart.BODYPART_RIGHT_LOWER_ARM.ordinal()], bodies[BodyPart.BODYPART_RIGHT_HAND.ordinal()], localA, localB, useLinearReferenceFrameA);
 
 			//#ifdef RIGID
diff --git a/com/bulletphysics/collision/shapes/ShapeHull.java b/com/bulletphysics/collision/shapes/ShapeHull.java
index 424d67a..9a95825 100755
--- a/com/bulletphysics/collision/shapes/ShapeHull.java
+++ b/com/bulletphysics/collision/shapes/ShapeHull.java
@@ -118,6 +118,12 @@
                         if (loop == 1)
                         {
                             //vec2.scale(0.5f);
+                            
+                            // Nodes are bigger
+                            vec2.x *= 5;
+                            vec2.y *= 5;
+                            vec2.z *= 5;
+                            
                             switch (capsule.getUpAxis())
                             {
                                 case 0: vec2.x /= capsule.getHalfHeight(); vec2.x *= capsule.getRadius()/2; vec2.x -= capsule.getHalfHeight(); break;
@@ -125,6 +131,7 @@
                                 case 2: vec2.z /= capsule.getHalfHeight(); vec2.z *= capsule.getRadius()/2; vec2.z -= capsule.getHalfHeight(); break;
                             }
                         }
+                        
                         vertices.getQuick(loop*NUM_UNITSPHERE_POINTS + i*rows + j).set(vec2);
                         normals.getQuick(loop*NUM_UNITSPHERE_POINTS + i*rows + j).set(normout);
                     }
diff --git a/com/bulletphysics/dynamics/RigidBody.java b/com/bulletphysics/dynamics/RigidBody.java
index 98e8092..75a9ea1 100755
--- a/com/bulletphysics/dynamics/RigidBody.java
+++ b/com/bulletphysics/dynamics/RigidBody.java
@@ -214,11 +214,31 @@
 		}
 	}
 	
+        static Vector3f g = new Vector3f();
+        static Vector3f w = new Vector3f();
+        static public Vector3f wind = new Vector3f();
+        static public Vector3f pos = new Vector3f();
+        
 	public void applyGravity() {
 		if (isStaticOrKinematicObject())
 			return;
 
-		applyCentralForce(gravity);
+                g.y = gravity.y;
+		applyCentralForce(g);
+                
+                wind.set(pos);
+                wind.x -= worldTransform.origin.x;
+                wind.y -= worldTransform.origin.y;
+                wind.z -= worldTransform.origin.z;
+                
+                float dot = pos.x * wind.x + pos.y * wind.y + pos.z * wind.z;
+                
+                dot = 10;
+                
+                w.x = wind.x * gravity.z * dot;
+                w.y = wind.y * gravity.z * dot;
+                w.z = wind.z * gravity.z * dot;
+		applyCentralForce(w);
 	}
 	
 	public void setGravity(Vector3f acceleration) {

--
Gitblit v1.6.2