Exemple #1
0
 public static void PlayPickUpDiamondSound(this CubeBehaviour target)
 {
     if (CurrentUserInfo.IsSoundEnabled)
     {
         target.GetComponent <AudioSource>().Play();
     }
 }
        private void GivenANewCube()
        {
            CubeBehaviour prefab = Resources.Load <CubeBehaviour>(prefabPath);

            cubeBehaviour = GameObject.Instantiate(prefab);
            meshFilter    = cubeBehaviour.GetComponent <MeshFilter>();
        }
Exemple #3
0
    public static void CheckAABBs(CubeBehaviour a, CubeBehaviour b)
    {
        Contact contactB = new Contact(b);

        if ((a.min.x <= b.max.x && a.max.x >= b.min.x) &&
            (a.min.y <= b.max.y && a.max.y >= b.min.y) &&
            (a.min.z <= b.max.z && a.max.z >= b.min.z))
        {
            // determine the distances between the contact extents
            float[] distances =
            {
                (b.max.x - a.min.x),
                (a.max.x - b.min.x),
                (b.max.y - a.min.y),
                (a.max.y - b.min.y),
                (b.max.z - a.min.z),
                (a.max.z - b.min.z)
            };

            float   penetration = float.MaxValue;
            Vector3 face        = Vector3.zero;

            // check each face to see if it is the one that connected
            for (int i = 0; i < 6; i++)
            {
                if (distances[i] < penetration)
                {
                    // determine the penetration distance
                    penetration = distances[i];
                    face        = faces[i];
                }
            }

            // set the contact properties
            contactB.face        = face;
            contactB.penetration = penetration;


            // check if contact does not exist
            if (!a.contacts.Contains(contactB))
            {
                // remove any contact that matches the name but not other parameters
                for (int i = a.contacts.Count - 1; i > -1; i--)
                {
                    if (a.contacts[i].cube.name.Equals(contactB.cube.name))
                    {
                        a.contacts.RemoveAt(i);
                    }
                }

                if (a is BulletBehaviour bullet)
                {
                    if (b.name != "Player")
                    {
                        bullet.penetration     = penetration;
                        bullet.collisionNormal = contactB.face;
                        Reflect(bullet);
                    }
                }
                else
                {
                    if (contactB.face == Vector3.down)
                    {
                        if (!a.isGrounded)
                        {
                            a.gameObject.GetComponent <RigidBody3D>().Stop();
                            a.isGrounded = true;
                        }
                    }
                    else
                    {
                        if (a.GetComponent <RigidBody3D>().bodyType != BodyType.STATIC)
                        {
                            a.GetComponent <RigidBody3D>().velocity -= face * penetration / 2 * 0.1f;
                        }
                        b.GetComponent <RigidBody3D>().velocity += face * penetration / 2 * 0.1f;
                    }
                }

                // add the new contact
                a.contacts.Add(contactB);
                a.isColliding = true;
            }
        }
        else
        {
            if (a.contacts.Exists(x => x.cube.gameObject.name == b.gameObject.name))
            {
                a.contacts.Remove(a.contacts.Find(x => x.cube.gameObject.name.Equals(b.gameObject.name)));
                a.isColliding = false;

                if (a.gameObject.GetComponent <RigidBody3D>().bodyType == BodyType.DYNAMIC)
                {
                    a.gameObject.GetComponent <RigidBody3D>().isFalling = true;
                    a.gameObject.GetComponent <RigidBody3D>().timer     = 0;
                    a.isGrounded = false;
                }
            }
        }
    }
    public static void CheckAABBs(CubeBehaviour a, CubeBehaviour b)
    {
        //collision manifold
        Contact contactB = new Contact(b);

//Debug.Log("Cube"+a.min.x+" "+b.min.x+" "+a.max.x+" "+b.max.x);
        if ((a.min.x <= b.max.x && a.max.x >= b.min.x) &&
            (a.min.y <= b.max.y && a.max.y >= b.min.y) &&
            (a.min.z <= b.max.z && a.max.z >= b.min.z))
        {
            // determine the distances between the contact extents
            //collision manifold
            float[] distances =
            {
                (b.max.x - a.min.x),
                (a.max.x - b.min.x),
                (b.max.y - a.min.y),
                (a.max.y - b.min.y),
                (b.max.z - a.min.z),
                (a.max.z - b.min.z)
            };

            float   penetration = float.MaxValue;
            Vector3 face        = Vector3.zero;

            // check each face to see if it is the one that connected
            for (int i = 0; i < 6; i++)
            {
                if (distances[i] < penetration)
                {
                    // determine the penetration distance
                    penetration = distances[i];
                    face        = faces[i];
                }
            }

            // set the contact properties
            contactB.face        = face;
            contactB.penetration = penetration;

            //bool NotRemove=true;
            // check if contact does not exist
            if (!a.contacts.Contains(contactB))
            {
                // remove any contact that matches the name but not other parameters
                for (int i = a.contacts.Count - 1; i > -1; i--)
                {
                    if (a.contacts[i].cube.name.Equals(contactB.cube.name))
                    {
                        a.contacts.RemoveAt(i);
                        //NotRemove=false;
                    }
                }
                if (a.name == "Player" && b.rb.bodyType == BodyType.DYNAMIC)
                {
                    // a = F/m
                    Vector3 impulse = new Vector3(a.GetComponent <PlayerBehaviour>().F *a.rb.velocity.normalized.x / b.rb.mass, 0.0f, a.GetComponent <PlayerBehaviour>().F *a.rb.velocity.normalized.z / b.rb.mass);
                    b.transform.position += a.rb.velocity.normalized * contactB.penetration;
                    b.rb.velocity         = impulse * Time.deltaTime;

                    //move the box by player

                    /*  if (a.name == "Player" && b.name=="Box" && b.gameObject.GetComponent<RigidBody3D>().bodyType == BodyType.DYNAMIC)
                     * {
                     *    Debug.Log("collision with player and box");
                     *    if (contactB.face == Vector3.left )
                     *    {
                     *         //Debug.Log("collision with player and box left");
                     *    b.gameObject.transform.position += new Vector3(0, 0, Camera.main.transform.forward.z+1)  * penetration;
                     *    b.gameObject.GetComponent<RigidBody3D>().velocity = new Vector3( 0, 0,Camera.main.transform.forward.z+1) * Time.deltaTime;
                     *    }
                     *    else if (contactB.face == Vector3.right )
                     *    {
                     *    b.gameObject.transform.position += new Vector3(0, 0, Camera.main.transform.forward.z-1) *Time.deltaTime* penetration;
                     *    b.gameObject.GetComponent<RigidBody3D>().velocity = new Vector3(0, 0, Camera.main.transform.forward.z-1) * Time.deltaTime;
                     *    }
                     *
                     *    else if(contactB.face == Vector3.forward)
                     *    {
                     *    b.gameObject.transform.position += new Vector3(Camera.main.transform.forward.x-1, 0, 0) * penetration;
                     *    b.gameObject.GetComponent<RigidBody3D>().velocity = new Vector3(Camera.main.transform.forward.x-1, 0, 0) * Time.deltaTime;
                     *    }
                     *    else if(contactB.face == Vector3.back)
                     *    {
                     *    b.gameObject.transform.position += new Vector3(Camera.main.transform.forward.x+1, 0, 0) * penetration;
                     *    b.gameObject.GetComponent<RigidBody3D>().velocity = new Vector3(Camera.main.transform.forward.x+1, 0, 0) * Time.deltaTime;
                     *    }
                     *
                     * }
                     * else if (b.gameObject.GetComponent<RigidBody3D>().bodyType == BodyType.DYNAMIC && a.name != "Player" && b.name != "Player")
                     * {
                     *
                     *    if (contactB.face == Vector3.forward || contactB.face == Vector3.back || contactB.face == Vector3.left
                     || contactB.face == Vector3.right)
                     ||   {
                     ||
                     ||       b.gameObject.GetComponent<RigidBody3D>().velocity = contactB.face *Time.deltaTime;
                     ||     //  b.gameObject.GetComponent<RigidBody3D>().velocity.y=0;
                     ||   }
                     ||}*/
                }
                else if (a.name == "Player" && b.name == "Stair" && b.rb.bodyType == BodyType.STATIC)
                {
                    //stair collision
                    Debug.Log("stair collision");
                    if (contactB.face == Vector3.forward || contactB.face == Vector3.back || contactB.face == Vector3.left ||
                        contactB.face == Vector3.right)
                    {
                        var velocity = new Vector3(-contactB.face.x, Time.deltaTime, -contactB.face.z);
                        a.gameObject.transform.position += velocity * Time.deltaTime;
                    }
                    if (contactB.face == Vector3.down)
                    {
                        //a.gameObject.transform.position=b.rb.transform.position+new Vector3(0,1,0);
                        Debug.Log("ok");
                    }
                }

                if (contactB.face == Vector3.down)
                {
                    a.gameObject.GetComponent <RigidBody3D>().Stop();
                    a.isGrounded = true;
                }



                // add the new contact
                a.contacts.Add(contactB);
                a.isColliding = true;
            }
        }
        else
        {
            if (a.contacts.Exists(x => x.cube.gameObject.name == b.gameObject.name))
            {
                a.contacts.Remove(a.contacts.Find(x => x.cube.gameObject.name.Equals(b.gameObject.name)));
                a.isColliding = false;

                if (a.gameObject.GetComponent <RigidBody3D>().bodyType == BodyType.DYNAMIC)
                {
                    a.gameObject.GetComponent <RigidBody3D>().isFalling = true;
                    a.isGrounded = false;
                }
            }
        }
    }
    public static void CheckAABBs(CubeBehaviour a, CubeBehaviour b)
    {
        Contact contactB = new Contact(b);

        RigidBody3D aRigidBody = a.GetComponent <RigidBody3D>();
        RigidBody3D bRigidBody = b.GetComponent <RigidBody3D>();

        if ((a.min.x <= b.max.x && a.max.x >= b.min.x) &&
            (a.min.y <= b.max.y && a.max.y >= b.min.y) &&
            (a.min.z <= b.max.z && a.max.z >= b.min.z))
        {
            // determine the distances between the contact extents
            float[] distances =
            {
                (b.max.x - a.min.x),
                (a.max.x - b.min.x),
                (b.max.y - a.min.y),
                (a.max.y - b.min.y),
                (b.max.z - a.min.z),
                (a.max.z - b.min.z)
            };

            float   penetration = float.MaxValue;
            Vector3 face        = Vector3.zero;

            // check each face to see if it is the one that connected
            for (int i = 0; i < 6; i++)
            {
                if (distances[i] < penetration)
                {
                    // determine the penetration distance
                    penetration = distances[i];
                    face        = faces[i];
                }
            }

            // set the contact properties
            contactB.face        = face;
            contactB.penetration = penetration;


            // check if contact does not exist
            if (!a.contacts.Contains(contactB))
            {
                // remove any contact that matches the name but not other parameters
                for (int i = a.contacts.Count - 1; i > -1; i--)
                {
                    if (a.contacts[i].cube.name.Equals(contactB.cube.name))
                    {
                        a.contacts.RemoveAt(i);
                    }
                }


                if (contactB.face == Vector3.down)             // if the cubes are on top of each other
                {
                    if (b.tag == "Floor" && a.tag != "Player") // keep moving if it hits the floor
                    {
                        aRigidBody.velocity.y     = 0;
                        aRigidBody.acceleration.y = 0;
                        a.isGrounded = true;
                    }
                    else
                    {
                        a.gameObject.GetComponent <RigidBody3D>().Stop();
                        a.isGrounded = true;
                    }
                }

                if (a.tag == "Player" && bRigidBody.bodyType == BodyType.DYNAMIC)
                {
                    // keep the cube on the ground
                    //bRigidBody.velocity.y = 0;
                    //bRigidBody.acceleration.y = 0;

                    Vector3 normalizedVelocity = Vector3.Normalize(aRigidBody.velocity);

                    bRigidBody.forceX = a.GetComponent <PlayerBehaviour>().forceMag *normalizedVelocity.x;
                    bRigidBody.forceZ = a.GetComponent <PlayerBehaviour>().forceMag *normalizedVelocity.z;

                    // acceleration = force/mass
                    Vector3 impulse = new Vector3(bRigidBody.forceX / bRigidBody.mass, 0.0f, bRigidBody.forceZ / bRigidBody.mass);

                    Debug.Log("Player to Cube Collision");

                    b.transform.position += Vector3.Normalize(aRigidBody.velocity) * a.GetComponent <PlayerBehaviour>().speed / 5 * contactB.penetration; // resolution
                    // bRigidBody.velocity = Vector3.Normalize(aRigidBody.velocity) * a.GetComponent<PlayerBehaviour>().speed / 10 * Time.deltaTime; // match the velocity to the player
                    bRigidBody.velocity = impulse * Time.deltaTime;
                }

                // add the new contact
                a.contacts.Add(contactB);
                a.isColliding = true;
            }
        }
        else
        {
            if (a.contacts.Exists(x => x.cube.gameObject.name == b.gameObject.name))
            {
                a.contacts.Remove(a.contacts.Find(x => x.cube.gameObject.name.Equals(b.gameObject.name)));
                a.isColliding = false;

                if (a.gameObject.GetComponent <RigidBody3D>().bodyType == BodyType.DYNAMIC)
                {
                    a.gameObject.GetComponent <RigidBody3D>().isFalling = true;
                    a.isGrounded = false;
                }
            }
            if (a.isGrounded && a.tag != "Player")
            {
                aRigidBody.velocity.y     = 0;
                aRigidBody.acceleration.y = 0;
            }
            if (a.contacts.Count == 0 && aRigidBody.isFalling)
            {
                aRigidBody.acceleration.y = -0.001f;
            }
        }
    }
        private void ThenMeshFilterIsAdded()
        {
            MeshFilter filter = cubeBehaviour.GetComponent <MeshFilter>();

            Assert.NotNull(filter);
        }