Collide() публичный Метод

public Collide ( Vector3, &particlePosition, float particleRadius ) : void
particlePosition Vector3,
particleRadius float
Результат void
Пример #1
0
    void UpdateParticles2(float timeVar)
    {
        Plane movePlane = new Plane();

        for (int i = 1; i < m_Particles.Count; ++i)
        {
            Particle p  = m_Particles[i];
            Particle p0 = m_Particles[p.m_ParentIndex];

            float restLen;
            if (p.m_Transform != null)
            {
                restLen = (p0.m_Transform.position - p.m_Transform.position).magnitude;
            }
            else
            {
                restLen = p0.m_Transform.localToWorldMatrix.MultiplyVector(p.m_EndOffset).magnitude;
            }

            // keep shape
            float stiffness = Mathf.Lerp(1.0f, p.m_Stiffness, m_Weight);
            if (stiffness > 0 || p.m_Elasticity > 0)
            {
                Matrix4x4 m0 = p0.m_Transform.localToWorldMatrix;
                m0.SetColumn(3, p0.m_Position);
                Vector3 restPos;
                if (p.m_Transform != null)
                {
                    restPos = m0.MultiplyPoint3x4(p.m_Transform.localPosition);
                }
                else
                {
                    restPos = m0.MultiplyPoint3x4(p.m_EndOffset);
                }

                Vector3 d = restPos - p.m_Position;
                p.m_Position += d * (p.m_Elasticity * timeVar);

                if (stiffness > 0)
                {
                    d = restPos - p.m_Position;
                    float len    = d.magnitude;
                    float maxlen = restLen * (1 - stiffness) * 2;
                    if (len > maxlen)
                    {
                        p.m_Position += d * ((len - maxlen) / len);
                    }
                }
            }

            // collide
            if (m_Colliders != null)
            {
                float particleRadius = p.m_Radius * m_ObjectScale;
                for (int j = 0; j < m_Colliders.Count; ++j)
                {
                    DynamicBoneColliderBase c = m_Colliders[j];
                    if (c != null && c.enabled)
                    {
                        p.m_isCollide |= c.Collide(ref p.m_Position, particleRadius);
                    }
                }
            }

            // freeze axis, project to plane
            if (m_FreezeAxis != FreezeAxis.None)
            {
                switch (m_FreezeAxis)
                {
                case FreezeAxis.X:
                    movePlane.SetNormalAndPosition(p0.m_Transform.right, p0.m_Position);
                    break;

                case FreezeAxis.Y:
                    movePlane.SetNormalAndPosition(p0.m_Transform.up, p0.m_Position);
                    break;

                case FreezeAxis.Z:
                    movePlane.SetNormalAndPosition(p0.m_Transform.forward, p0.m_Position);
                    break;
                }
                p.m_Position -= movePlane.normal * movePlane.GetDistanceToPoint(p.m_Position);
            }

            // keep length
            Vector3 dd   = p0.m_Position - p.m_Position;
            float   leng = dd.magnitude;
            if (leng > 0)
            {
                p.m_Position += dd * ((leng - restLen) / leng);
            }
        }
    }
    private void UpdateDynamicBones(float t)
    {
        if (m_Root == null)
        {
            return;
        }

        m_ObjectScale = Mathf.Abs(transform.lossyScale.x);
        var position = transform.position;

        m_ObjectMove         = position - m_ObjectPrevPosition;
        m_ObjectPrevPosition = position;

        var loop = 1;

        if (m_UpdateRate > 0)
        {
            var dt = 1.0f / m_UpdateRate;
            m_Time += t;
            loop    = 0;

            while (m_Time >= dt)
            {
                m_Time -= dt;
                if (++loop < 3)
                {
                    continue;
                }
                m_Time = 0;
                break;
            }
        }

        if (loop > 0)
        {
            for (var i = 0; i < loop; ++i)
            {
                var force = m_Gravity;
                var fdir  = m_Gravity.normalized;
                var rf    = m_Root.TransformDirection(m_LocalGravity);
                var pf    = fdir * Mathf.Max(Vector3.Dot(rf, fdir), 0); // project current gravity to rest gravity
                force -= pf;                                            // remove projected gravity
                force  = (force + m_Force) * m_ObjectScale;

                foreach (var p in m_Particles)
                {
                    if (p.m_ParentIndex >= 0)
                    {
                        // verlet integration
                        var v     = p.m_Position - p.m_PrevPosition;
                        var rmove = m_ObjectMove * p.m_Inert;
                        p.m_PrevPosition = p.m_Position + rmove;
                        p.m_Position    += v * (1 - p.m_Damping) + force + rmove;
                    }
                    else
                    {
                        p.m_PrevPosition = p.m_Position;
                        p.m_Position     = p.m_Transform.position;
                    }
                }
                var movePlane = new Plane();

                for (var k = 1; k < m_Particles.Count; k++)
                {
                    var p  = m_Particles[k];
                    var p0 = m_Particles[p.m_ParentIndex];

                    var restLen = p.m_Transform != null ? (p0.m_Transform.position - p.m_Transform.position).magnitude : p0.m_Transform.localToWorldMatrix.MultiplyVector(p.m_EndOffset).magnitude;

                    // keep shape
                    var stiffness = Mathf.Lerp(1.0f, p.m_Stiffness, m_Weight);
                    if (stiffness > 0 || p.m_Elasticity > 0)
                    {
                        var m0 = p0.m_Transform.localToWorldMatrix;
                        m0.SetColumn(3, p0.m_Position);
                        Vector3 restPos;
                        if (p.m_Transform != null)
                        {
                            restPos = m0.MultiplyPoint3x4(p.m_Transform.localPosition);
                        }
                        else
                        {
                            restPos = m0.MultiplyPoint3x4(p.m_EndOffset);
                        }

                        Vector3 d = restPos - p.m_Position;
                        p.m_Position += d * p.m_Elasticity;

                        if (stiffness > 0)
                        {
                            d = restPos - p.m_Position;
                            float len    = d.magnitude;
                            float maxlen = restLen * (1 - stiffness) * 2;
                            if (len > maxlen)
                            {
                                p.m_Position += d * ((len - maxlen) / len);
                            }
                        }
                    }

                    // collide
                    if (m_ColliderList != null)
                    {
                        float particleRadius = p.m_Radius * m_ObjectScale;
                        for (int j = 0; j < m_ColliderList.Count; ++j)
                        {
                            DynamicBoneColliderBase c = m_ColliderList[j];
                            if (c != null && c.enabled)
                            {
                                c.Collide(ref p.m_Position, particleRadius);
                            }
                        }
                    }

                    // freeze axis, project to plane
                    if (m_FreezeAxis != FreezeAxis.None)
                    {
                        switch (m_FreezeAxis)
                        {
                        case FreezeAxis.X:
                            movePlane.SetNormalAndPosition(p0.m_Transform.right, p0.m_Position);
                            break;

                        case FreezeAxis.Y:
                            movePlane.SetNormalAndPosition(p0.m_Transform.up, p0.m_Position);
                            break;

                        case FreezeAxis.Z:
                            movePlane.SetNormalAndPosition(p0.m_Transform.forward, p0.m_Position);
                            break;
                        }
                        p.m_Position -= movePlane.normal * movePlane.GetDistanceToPoint(p.m_Position);
                    }

                    // keep length
                    Vector3 dd   = p0.m_Position - p.m_Position;
                    float   leng = dd.magnitude;
                    if (leng > 0)
                    {
                        p.m_Position += dd * ((leng - restLen) / leng);
                    }
                }
                m_ObjectMove = Vector3.zero;
            }
        }
        else
        {
            SkipUpdateParticles();
        }
        for (var i = 1; i < m_Particles.Count; ++i)
        {
            var p  = m_Particles[i];
            var p0 = m_Particles[p.m_ParentIndex];

            if (p0.m_Transform.childCount <= 1)         // do not modify bone orientation if has more then one child
            {
                var v   = p.m_Transform != null ? p.m_Transform.localPosition:p.m_EndOffset;
                var v2  = p.m_Position - p0.m_Position;
                var rot = Quaternion.FromToRotation(p0.m_Transform.TransformDirection(v), v2);
                p0.m_Transform.rotation = rot * p0.m_Transform.rotation;
            }
            if (p.m_Transform != null)
            {
                p.m_Transform.position = p.m_Position;
            }
        }
    }
Пример #3
0
    private void UpdateParticles2()
    {
        Plane plane = (Plane)null;

        for (int index1 = 1; index1 < this.m_Particles.Count; ++index1)
        {
            DynamicBone.Particle particle1 = this.m_Particles[index1];
            DynamicBone.Particle particle2 = this.m_Particles[particle1.m_ParentIndex];
            float magnitude1;
            if (Object.op_Inequality((Object)particle1.m_Transform, (Object)null))
            {
                Vector3 vector3 = Vector3.op_Subtraction(particle2.m_Transform.get_position(), particle1.m_Transform.get_position());
                magnitude1 = ((Vector3) ref vector3).get_magnitude();
            }
            else
            {
                Matrix4x4 localToWorldMatrix = particle2.m_Transform.get_localToWorldMatrix();
                Vector3   vector3            = ((Matrix4x4) ref localToWorldMatrix).MultiplyVector(particle1.m_EndOffset);
                magnitude1 = ((Vector3) ref vector3).get_magnitude();
            }
            float num1 = Mathf.Lerp(1f, particle1.m_Stiffness, this.m_Weight);
            if ((double)num1 > 0.0 || (double)particle1.m_Elasticity > 0.0)
            {
                Matrix4x4 localToWorldMatrix = particle2.m_Transform.get_localToWorldMatrix();
                ((Matrix4x4) ref localToWorldMatrix).SetColumn(3, Vector4.op_Implicit(particle2.m_Position));
                Vector3 vector3_1 = !Object.op_Inequality((Object)particle1.m_Transform, (Object)null) ? ((Matrix4x4) ref localToWorldMatrix).MultiplyPoint3x4(particle1.m_EndOffset) : ((Matrix4x4) ref localToWorldMatrix).MultiplyPoint3x4(particle1.m_Transform.get_localPosition());
                Vector3 vector3_2 = Vector3.op_Subtraction(vector3_1, particle1.m_Position);
                DynamicBone.Particle particle3 = particle1;
                particle3.m_Position = Vector3.op_Addition(particle3.m_Position, Vector3.op_Multiply(vector3_2, particle1.m_Elasticity));
                if ((double)num1 > 0.0)
                {
                    Vector3 vector3_3  = Vector3.op_Subtraction(vector3_1, particle1.m_Position);
                    float   magnitude2 = ((Vector3) ref vector3_3).get_magnitude();
                    float   num2       = (float)((double)magnitude1 * (1.0 - (double)num1) * 2.0);
                    if ((double)magnitude2 > (double)num2)
                    {
                        DynamicBone.Particle particle4 = particle1;
                        particle4.m_Position = Vector3.op_Addition(particle4.m_Position, Vector3.op_Multiply(vector3_3, (magnitude2 - num2) / magnitude2));
                    }
                }
            }
            if (this.m_Colliders != null)
            {
                float particleRadius = particle1.m_Radius * this.m_ObjectScale;
                for (int index2 = 0; index2 < this.m_Colliders.Count; ++index2)
                {
                    DynamicBoneColliderBase collider = this.m_Colliders[index2];
                    if (Object.op_Inequality((Object)collider, (Object)null) && ((Behaviour)collider).get_enabled())
                    {
                        collider.Collide(ref particle1.m_Position, particleRadius);
                    }
                }
            }
            if (this.m_FreezeAxis != DynamicBone.FreezeAxis.None)
            {
                switch (this.m_FreezeAxis)
                {
                case DynamicBone.FreezeAxis.X:
                    ((Plane) ref plane).SetNormalAndPosition(particle2.m_Transform.get_right(), particle2.m_Position);
                    break;

                case DynamicBone.FreezeAxis.Y:
                    ((Plane) ref plane).SetNormalAndPosition(particle2.m_Transform.get_up(), particle2.m_Position);
                    break;

                case DynamicBone.FreezeAxis.Z:
                    ((Plane) ref plane).SetNormalAndPosition(particle2.m_Transform.get_forward(), particle2.m_Position);
                    break;
                }
                DynamicBone.Particle particle3 = particle1;
                particle3.m_Position = Vector3.op_Subtraction(particle3.m_Position, Vector3.op_Multiply(((Plane) ref plane).get_normal(), ((Plane) ref plane).GetDistanceToPoint(particle1.m_Position)));
            }
            Vector3 vector3_4  = Vector3.op_Subtraction(particle2.m_Position, particle1.m_Position);
            float   magnitude3 = ((Vector3) ref vector3_4).get_magnitude();
            if ((double)magnitude3 > 0.0)
            {
                DynamicBone.Particle particle3 = particle1;
                particle3.m_Position = Vector3.op_Addition(particle3.m_Position, Vector3.op_Multiply(vector3_4, (magnitude3 - magnitude1) / magnitude3));
            }
        }
    }
Пример #4
0
    private void UpdateParticles2()
    {
        Plane plane = default(Plane);

        for (int i = 1; i < this.m_Particles.Count; i++)
        {
            DynamicBone.Particle particle  = this.m_Particles[i];
            DynamicBone.Particle particle2 = this.m_Particles[particle.m_ParentIndex];
            float magnitude;
            if (particle.m_Transform != null)
            {
                magnitude = (particle2.m_Transform.position - particle.m_Transform.position).magnitude;
            }
            else
            {
                magnitude = particle2.m_Transform.localToWorldMatrix.MultiplyVector(particle.m_EndOffset).magnitude;
            }
            float num = Mathf.Lerp(1f, particle.m_Stiffness, this.m_Weight);
            if (num > 0f || particle.m_Elasticity > 0f)
            {
                Matrix4x4 localToWorldMatrix = particle2.m_Transform.localToWorldMatrix;
                localToWorldMatrix.SetColumn(3, particle2.m_Position);
                Vector3 a;
                if (particle.m_Transform != null)
                {
                    a = localToWorldMatrix.MultiplyPoint3x4(particle.m_Transform.localPosition);
                }
                else
                {
                    a = localToWorldMatrix.MultiplyPoint3x4(particle.m_EndOffset);
                }
                Vector3 a2 = a - particle.m_Position;
                particle.m_Position += a2 * particle.m_Elasticity;
                if (num > 0f)
                {
                    a2 = a - particle.m_Position;
                    float magnitude2 = a2.magnitude;
                    float num2       = magnitude * (1f - num) * 2f;
                    if (magnitude2 > num2)
                    {
                        particle.m_Position += a2 * ((magnitude2 - num2) / magnitude2);
                    }
                }
            }
            if (this.m_Colliders != null)
            {
                float particleRadius = particle.m_Radius * this.m_ObjectScale;
                for (int j = 0; j < this.m_Colliders.Count; j++)
                {
                    DynamicBoneColliderBase dynamicBoneColliderBase = this.m_Colliders[j];
                    if (dynamicBoneColliderBase != null && dynamicBoneColliderBase.enabled)
                    {
                        dynamicBoneColliderBase.Collide(ref particle.m_Position, particleRadius);
                    }
                }
            }
            if (this.m_FreezeAxis != DynamicBone.FreezeAxis.None)
            {
                switch (this.m_FreezeAxis)
                {
                case DynamicBone.FreezeAxis.X:
                    plane.SetNormalAndPosition(particle2.m_Transform.right, particle2.m_Position);
                    break;

                case DynamicBone.FreezeAxis.Y:
                    plane.SetNormalAndPosition(particle2.m_Transform.up, particle2.m_Position);
                    break;

                case DynamicBone.FreezeAxis.Z:
                    plane.SetNormalAndPosition(particle2.m_Transform.forward, particle2.m_Position);
                    break;
                }
                particle.m_Position -= plane.normal * plane.GetDistanceToPoint(particle.m_Position);
            }
            Vector3 a3         = particle2.m_Position - particle.m_Position;
            float   magnitude3 = a3.magnitude;
            if (magnitude3 > 0f)
            {
                particle.m_Position += a3 * ((magnitude3 - magnitude) / magnitude3);
            }
        }
    }