Ejemplo 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);
        }
    }
Ejemplo n.º 2
0
    // Update is called once per frame
    void Update()
    {
        Vector3 force        = calculateForce();
        Vector3 acceleration = new Vector3(force.x / mass, force.y / mass, force.z / mass);

        velocity += acceleration * Time.deltaTime;
        transform.Translate(velocity * Time.deltaTime);

        if (usesRotation)
        {
        }

        Object[] obj = GameObject.FindObjectsOfType(typeof(GameObject));
        if (this.GetComponent <sphereCollider>() != null)
        {
            foreach (GameObject o in obj)
            {
                if (o.GetComponent <sphereCollider>() != null)
                {
                    sphereCollider col    = o.GetComponent <sphereCollider>();
                    sphereCollider bounds = this.GetComponent <sphereCollider>();
                    col.sphereCollision(bounds, this.GetComponent <rigidBody>(), this.transform);
                }
                else if (o.GetComponent <planeCollider>() != null)
                {
                    planeCollider  col    = o.GetComponent <planeCollider>();
                    sphereCollider bounds = this.GetComponent <sphereCollider>();
                    col.sphereCollision(bounds, this.GetComponent <rigidBody>(), this.transform);
                }
                else if (o.GetComponent <AABB>() != null)
                {
                    AABB           col    = o.GetComponent <AABB>();
                    sphereCollider bounds = this.GetComponent <sphereCollider>();
                    col.sphereCollision(bounds, this.GetComponent <rigidBody>(), this.transform);
                }
            }
        }
        else if (this.GetComponent <AABB>() != null)
        {
            foreach (GameObject o in obj)
            {
                if (o.GetComponent <planeCollider>() != null)
                {
                    planeCollider col    = o.GetComponent <planeCollider>();
                    AABB          bounds = this.GetComponent <AABB>();
                    col.AABBCollision(bounds, this.GetComponent <rigidBody>(), this.transform);
                }
            }
        }
    }
Ejemplo n.º 3
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);
            }
        }
    }
Ejemplo n.º 4
0
    public void AABBCollision(AABB boundingBox, rigidBody body, Transform transform)
    {
        normal = this.transform.TransformDirection(Vector3.up);


        Vector3 E = (new Vector3(boundingBox.transform.position.x + boundingBox.width / 2,
                                 boundingBox.transform.position.y + boundingBox.height / 2,
                                 boundingBox.transform.position.z + boundingBox.depth / 2) - new Vector3(boundingBox.transform.position.x - boundingBox.width / 2,
                                                                                                         boundingBox.transform.position.y - boundingBox.height / 2,
                                                                                                         boundingBox.transform.position.z - boundingBox.depth / 2)) / 2;

        float fradius = Mathf.Abs(normal.x * E.x) + Mathf.Abs(normal.y * E.y) + Mathf.Abs(normal.z * E.z);

        sphereCollider newSphere = new sphereCollider
        {
            radius = fradius
        };

        sphereCollision(newSphere, body, transform);
    }
Ejemplo n.º 5
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;
        }
    }