Example #1
0
        public override void ApplyForcesToActor(ObiActor actor)
        {
            Matrix4x4 l2sTransform;

            if (actor.Solver.simulateInLocalSpace)
            {
                l2sTransform = actor.Solver.transform.worldToLocalMatrix * transform.localToWorldMatrix;
            }
            else
            {
                l2sTransform = transform.localToWorldMatrix;
            }

            Vector4 directionalForce = l2sTransform.MultiplyVector(Vector3.forward * (intensity + GetTurbulence(turbulence)));

            float sqrRadius = radius * radius;

            // Allocate forces array:
            Vector4[] forces = new Vector4[actor.particleIndices.Length];
            Vector4   center = new Vector4(transform.position.x, transform.position.y, transform.position.z);

            // Calculate force intensity for each actor particle:
            for (int i = 0; i < forces.Length; ++i)
            {
                Vector4 distanceVector = actor.Solver.renderablePositions[actor.particleIndices[i]] - center;

                float sqrMag  = distanceVector.sqrMagnitude;
                float falloff = Mathf.Clamp01((sqrRadius - sqrMag) / sqrRadius);

                if (radial)
                {
                    forces[i] = distanceVector / (Mathf.Sqrt(sqrMag) + float.Epsilon) * falloff * intensity;
                }
                else
                {
                    forces[i] = directionalForce * falloff;
                }
            }

            if (actor.UsesCustomExternalForces)
            {
                Oni.AddParticleWinds(actor.Solver.OniSolver, forces, actor.particleIndices, actor.particleIndices.Length);
            }
            else
            {
                Oni.AddParticleExternalForces(actor.Solver.OniSolver, forces, actor.particleIndices, actor.particleIndices.Length);
            }
        }