Beispiel #1
0
        protected override void FixedUpdate()
        {
            base.FixedUpdate();

            min = _center - sizeBox;
            max = _center + sizeBox;
        }
Beispiel #2
0
        public static Vector3 Cut(Vector3 distance, Vector3 min, Vector3 max)
        {
            float x, y, z;

            x = distance.X;
            y = distance.Y;
            z = distance.Z;

            if (distance.X > max.X)
            {
                x = max.X;
            }
            else if (distance.X < min.X)
            {
                x = min.X;
            }
            if (distance.Y > max.Y)
            {
                y = max.Y;
            }
            else if (distance.Y < min.Y)
            {
                y = min.Y;
            }
            if (distance.Z > max.Z)
            {
                z = max.Z;
            }
            else if (distance.Z < min.Z)
            {
                z = min.Z;
            }

            return(new Vector3(x, y, z));
        }
Beispiel #3
0
 public void AddForce(Vector3 force)
 {
     if (!IsKinematic)
     {
         velocity += ((force / Mass) + Gravity * (UseGravity ? 1 : 0)) * fixedDeltaTime;
     }
 }
Beispiel #4
0
        private void Update()
        {
            velocity += Gravity * fixedDeltaTime * (UseGravity ? 1 : 0);

            if (!IsKinematic)
            {
                velocity *= (1 - (Drag * fixedDeltaTime));
            }

            velocity = (Vector3.Right * velocity.X * (X ? 0 : 1)) + (Vector3.Up * velocity.Y * (Y ? 0 : 1)) + (Vector3.Forward * velocity.Z * (Z ? 0 : 1));

            if (CheckCollision(Vector3.Up * velocity.Y * deltaTime) != null)
            {
                if (collider.GetPhysicsMaterial() != null)
                {
                    velocity = collider.GetPhysicsMaterial().CalculateFriction(velocity, Vector3.Zero, Vector3.Magnitude(Gravity) * Mass, Mass);
                }

                if (CheckCollision(Vector3.Up * velocity.Y * deltaTime).GetCenter().Y + CheckCollision(Vector3.Up * velocity.Y * deltaTime).GetSize().Y <= collider.GetCenter().Y)
                {
                    velocity.Y = 0;
                }
            }

            position += velocity * deltaTime;
            pos       = position.ToUnity();
            this.transform.position = pos;
        }
Beispiel #5
0
        protected override void Start()
        {
            base.Start();

            sizeBox = transform.localScale.ToPhysics() / 2 * size.x;
            colliders.Add(this);
            type = 1;
        }
Beispiel #6
0
        public Collider CheckCollision(Vector3 position)
        {
            if (collider != null)
            {
                var aux = collider.center;
                collider.center = position;

                for (int i = 0; i < Collider.colliders.Count; i++)
                {
                    if (Collider.CheckCollision(collider, Collider.colliders[i]) != null)
                    {
                        return(Collider.CheckCollision(collider, Collider.colliders[i]));
                    }
                }

                collider.center = aux;
            }

            return(null);
        }
Beispiel #7
0
        public static Collider CheckCollision(Collider actual, Collider other)
        {
            if (actual == other)
            {
                return(null);
            }

            if (actual.GetColliderType() == 0 && other.GetColliderType() == 0)
            {
                if (Vector3.Magnitude(actual._center - other._center) <= (actual.GetComponent <SphereCollider>().GetRadius() + other.GetComponent <SphereCollider>().GetRadius()))
                {
                    return(other);
                }
            }
            else if (actual.GetColliderType() == 0 && other.GetColliderType() == 1)
            {
                var boxHalf    = other.GetComponent <BoxCollider>().GetSize() / 2.0f;
                var distance   = actual.GetComponent <SphereCollider>().GetCenter() - other.GetComponent <BoxCollider>().GetCenter();
                var cut        = Cut(distance, -boxHalf, boxHalf);
                var next       = other.GetComponent <BoxCollider>().GetCenter() + cut;
                var difference = next - actual.GetComponent <SphereCollider>().GetCenter();

                if (Vector3.Magnitude(difference) <= actual.GetComponent <SphereCollider>().GetRadius())
                {
                    return(other);
                }
            }
            else if (actual.GetColliderType() == 1 && other.GetColliderType() == 0)
            {
                var boxHalf    = actual.GetComponent <BoxCollider>().GetSize() / 2.0f;
                var distance   = other.GetComponent <SphereCollider>().GetCenter() - actual.GetComponent <BoxCollider>().GetCenter();
                var cut        = Cut(distance, -boxHalf, boxHalf);
                var next       = actual.GetComponent <BoxCollider>().GetCenter() + cut;
                var difference = next - other.GetComponent <SphereCollider>().GetCenter();

                if (Vector3.Magnitude(difference) <= other.GetComponent <SphereCollider>().GetRadius())
                {
                    return(other);
                }
            }
            else
            {
                var actualMin = actual.GetComponent <BoxCollider>().GetMin();
                var actualMax = actual.GetComponent <BoxCollider>().GetMax();
                var otherMin  = other.GetComponent <BoxCollider>().GetMin();
                var otherMax  = other.GetComponent <BoxCollider>().GetMax();

                if (actualMin.X >= otherMin.X && actualMin.Y >= otherMin.Y && actualMin.Z >= otherMin.Z && actualMin.X <= otherMax.X && actualMin.Y <= otherMax.Y && actualMin.Z <= otherMax.Z)
                {
                    print("Collision of type Box with Box:" + actual.gameObject.name + " " + other.gameObject.name);
                    return(other);
                }
                else if (actualMax.X >= otherMin.X && actualMax.Y >= otherMin.Y && actualMax.Z >= otherMax.Z && actualMax.X <= otherMax.X && actualMax.Y <= otherMax.Y && actualMax.Z <= otherMax.Z)
                {
                    print("Collision of type Box with Box:" + actual.gameObject.name + " " + other.gameObject.name);
                    return(other);
                }
            }

            return(null);
        }
Beispiel #8
0
 protected virtual void FixedUpdate()
 {
     position = transform.position.ToPhysics();
     _center  = position + center;
 }
Beispiel #9
0
 protected virtual void Start()
 {
     position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
     _center  = position + center;
 }
Beispiel #10
0
 public void SetVelocity(Vector3 velocity)
 {
     this.velocity = velocity;
 }