private void OnDrawGizmosSelected()
        {
            Vector3 force0 = GetForce(0) * 50;
            float   width  = force0.magnitude * 0.2f;

            Gizmos.DrawRay(transform.position, force0);
            EZPhysicsBoneUtility.DrawGizmosArrow(transform.position, force0, width, transform.up);
            EZPhysicsBoneUtility.DrawGizmosArrow(transform.position, force0, width, transform.right);
        }
示例#2
0
 public override void Collide(ref Vector3 position, float spacing)
 {
     if (referenceCollider is SphereCollider)
     {
         SphereCollider collider = referenceCollider as SphereCollider;
         if (insideMode)
         {
             EZPhysicsBoneUtility.PointInsideSphere(ref position, collider, spacing + margin);
         }
         else
         {
             EZPhysicsBoneUtility.PointOutsideSphere(ref position, collider, spacing + margin);
         }
     }
     else if (referenceCollider is CapsuleCollider)
     {
         CapsuleCollider collider = referenceCollider as CapsuleCollider;
         if (insideMode)
         {
             EZPhysicsBoneUtility.PointInsideCapsule(ref position, collider, spacing + margin);
         }
         else
         {
             EZPhysicsBoneUtility.PointOutsideCapsule(ref position, collider, spacing + margin);
         }
     }
     else if (referenceCollider is BoxCollider)
     {
         BoxCollider collider = referenceCollider as BoxCollider;
         if (insideMode)
         {
             EZPhysicsBoneUtility.PointInsideBox(ref position, collider, spacing + margin);
         }
         else
         {
             EZPhysicsBoneUtility.PointOutsideBox(ref position, collider, spacing + margin);
         }
     }
     else if (referenceCollider is MeshCollider)
     {
         if (!CheckConvex(referenceCollider as MeshCollider))
         {
             Debug.LogError("Non-Convex Mesh Collider is not supported", this);
             enabled = false;
             return;
         }
         if (insideMode)
         {
             Debug.LogError("Inside Mode On Mesh Collider is not supported", this);
             insideMode = false;
             return;
         }
         EZPhysicsBoneUtility.PointOutsideCollider(ref position, referenceCollider, spacing + margin);
     }
 }
示例#3
0
        private void UpdateNode(TreeNode node, float deltaTime)
        {
            if (node.depth > startDepth)
            {
                Vector3 lastPosition = node.position;

                // Damping (inertia attenuation)
                if (node.speed.sqrMagnitude < sleepThreshold)
                {
                    node.speed = Vector3.zero;
                }
                else
                {
                    node.position += node.speed * deltaTime * (1 - sharedMaterial.GetDamping(node.normalizedLength));
                }

                // Resistance (outside force resistance)
                Vector3 force = gravity;
                if (forceModule != null)
                {
                    force += forceModule.GetForce(node.normalizedLength);
                }
                node.position += force * (1 - sharedMaterial.GetResistance(node.normalizedLength));

                // Stiffness (shape keeper)
                Vector3 parentOffset = node.parent.position - node.parent.transform.position;
                Vector3 expectedPos  = node.parent.transform.TransformPoint(node.originalLocalPosition) + parentOffset;
                node.position = Vector3.Lerp(node.position, expectedPos, sharedMaterial.GetStiffness(node.normalizedLength));

                // Collision
                if (node.radius > 0)
                {
                    foreach (EZPhysicsBoneColliderBase collider in EZPhysicsBoneColliderBase.EnabledColliders)
                    {
                        if (node.transform != collider.transform && collisionLayers.Contains(collider.gameObject.layer))
                        {
                            collider.Collide(ref node.position, node.radius);
                        }
                    }
                    foreach (Collider collider in extraColliders)
                    {
                        if (node.transform != collider.transform && collider.enabled)
                        {
                            EZPhysicsBoneUtility.PointOutsideCollider(ref node.position, collider, node.radius);
                        }
                    }
                }

                // Slackness (length keeper)
                float   slackness    = sharedMaterial.GetSlackness(node.normalizedLength);
                Vector3 nodeDir      = (node.position - node.parent.position).normalized;
                Vector3 lengthKeeper = node.parent.position + nodeDir * node.nodeLength;
                node.position = Vector3.Lerp(lengthKeeper, node.position, slackness);

                node.speed = (node.position - lastPosition) / deltaTime;
            }
            else
            {
                node.position = node.transform.position;
            }

            for (int i = 0; i < node.children.Count; i++)
            {
                UpdateNode(node.children[i], deltaTime);
            }
        }