Exemplo n.º 1
0
    public void sphereCollision(sphereCollider sphere, rigidBody body, Transform transform)
    {
        normal = this.transform.TransformDirection(Vector3.up);
        float distance = Vector3.Dot((transform.position - this.transform.position), normal);

        if (distance < sphere.radius)
        {
            body.velocity       = reflect(-body.velocity, normal);
            body.velocity       = body.velocity * (1 - body.GetComponentInParent <material>().elasticity);
            transform.position += normal * (sphere.radius - distance);
        }
    }
Exemplo n.º 2
0
    public void sphereCollision(sphereCollider sphere, rigidBody body, Transform transform)
    {
        Vector3 difference = transform.position - this.transform.position;

        Vector3 closest = clampedVector(transform.position);

        //print(closest);

        //Vector3 closest = this.transform.position + clamped;

        difference = closest - transform.position;
        //print(difference);
        if (difference.magnitude < sphere.radius)
        {
            //print("hit");
            Vector3[] compass = new Vector3[]
            {
                Vector3.up,
                Vector3.down,
                Vector3.left,
                Vector3.right,
                Vector3.forward,
                Vector3.back
            };

            float   max    = 0.0f;
            Vector3 normal = new Vector3(0, 0, 0);

            foreach (Vector3 v in compass)
            {
                float dot = Vector3.Dot(-difference.normalized, v);
                if (dot > max)
                {
                    max    = dot;
                    normal = v;
                }
            }

            print(normal);
            if (normal != new Vector3(0, 0, 0))
            {
                body.velocity       = Vector3.Reflect(body.velocity, normal);
                body.velocity       = body.velocity * (1 - body.GetComponentInParent <material>().elasticity);
                transform.position += normal * (sphere.radius - difference.magnitude);
            }
        }
    }
Exemplo n.º 3
0
    public void sphereCollision(sphereCollider sphere, rigidBody body, Transform transform)
    {
        float distance = Vector3.Distance(transform.position, this.transform.position);

        if (distance < radius + sphere.radius)
        {
            Vector3 normal = Vector3.Normalize(transform.position - this.transform.position);

            //float restitution = this.GetComponent<material>().elasticity + body.GetComponentInParent<material>.elasticity

            rigidBody b = this.GetComponent <rigidBody>();

            float ai = Vector3.Dot(body.velocity, normal);
            float a2 = Vector3.Dot(b.velocity, normal);

            float p = ((2 - (this.GetComponent <material>().elasticity + body.GetComponentInParent <material>().elasticity)) * (ai - a2) / (body.mass + b.mass));

            Vector3 v1 = body.velocity - p * b.mass * normal;
            Vector3 v2 = b.velocity + p * body.mass * normal;

            body.velocity = v1;
            b.velocity    = v2;
        }
    }