Ejemplo n.º 1
0
    public bool IsColliding(SoftBodyParticleData other)
    {
        Vector3 s = position - other.position; // vector between the centers of each sphere
        Vector3 v = velocity - other.velocity; // relative velocity between spheres
        float   r = size + other.size;
        float   t = 0;
        float   c = Vector3.Dot(s, s) - r * r; // if negative, they overlap

        if (c < 0.0f)                          // if true, they already overlap
        {
            t = .0f;
            return(true);
        }

        float a = Vector3.Dot(v, v);

        float b = Vector3.Dot(v, s);

        if (b >= 0.0f)
        {
            return(false); // does not move towards each other
        }
        float d = b * b - a * c;

        if (d < 0.0f)
        {
            return(false); // no real roots ... no collision
        }
        t = (-b - Mathf.Sqrt(d)) / a;

        return(true);
    }
Ejemplo n.º 2
0
    private void Start()
    {
        int size = 128;


        particlesData = new NativeArray <SoftBodyParticleData>(size, Allocator.Persistent);

        for (int i = 0; i < size; i++)
        {
            var d = new SoftBodyParticleData();
            d.size           = Random.Range(.5f, 1f);
            d.mass           = d.size * 5;
            d.position       = new Vector3(0, 5, 0) + new Vector3(0, i, 0);
            particlesData[i] = d;
        }

        particles       = new ParticleSystem.Particle[particlesData.Length];
        particlesNative = new NativeArray <ParticleSystem.Particle>(particles, Allocator.Persistent);
    }
Ejemplo n.º 3
0
    public void Resolve(SoftBodyParticleData coll)
    {
        float   m1, m2, x1, x2;
        var     x = (position - coll.position).normalized;
        Vector3 v1, v2, v1x, v2x, v1y, v2y;

        v1  = velocity;
        x1  = Vector3.Dot(x, v1);
        v1x = x * x1;
        v1y = v1 - v1x;
        m1  = mass;

        x   = x * -1;
        v2  = coll.velocity;
        x2  = Vector3.Dot(x, v2);
        v2x = x * x2;
        v2y = v2 - v2x;
        m2  = coll.mass;

        velocity      = v1x * (m1 - m2) / (m1 + m2) + v2x * (2 * m2) / (m1 + m2) + v1y;
        coll.velocity = v1x * (2 * m1) / (m1 + m2) + v2x * (m2 - m1) / (m1 + m2) + v2y;
    }