Exemple #1
0
    public static Vector3 GetAABBClosestPoint(SphereBehaviour sphere, CubeBehaviour box)
    {
        var points = new List <Vector3> {
            new Vector3(box.min.x, box.min.y, box.min.z),
            new Vector3(box.min.x, box.min.y, box.max.z),
            new Vector3(box.max.x, box.min.y, box.max.z),
            new Vector3(box.max.x, box.min.y, box.min.z),
            new Vector3(box.min.x, box.max.y, box.min.z),
            new Vector3(box.min.x, box.max.y, box.max.z),
            new Vector3(box.max.x, box.max.y, box.max.z),
            new Vector3(box.max.x, box.max.y, box.min.z)
        };

        var minDistancePoint = points[0];
        var minDistance      = Mathf.Infinity;

        foreach (var point in points)
        {
            var dist = (point - sphere.transform.position).magnitude;

            if (!(dist < minDistance))
            {
                continue;
            }

            minDistance      = dist;
            minDistancePoint = point;
        }

        return(minDistancePoint);
    }
Exemple #2
0
    public static CollisionManifold getSphereToAABBCollisionManifold(SphereBehaviour sphere, CubeBehaviour box)
    {
        var manifold = new CollisionManifold();

        var closestPoint = GetAABBClosestPoint(sphere, box);
        var direction    = sphere.transform.position - closestPoint;

        manifold.PenetrationDistance = (direction.magnitude - sphere.Radius) / 2;

        if (manifold.PenetrationDistance <= 0.001f && manifold.PenetrationDistance > 0)
        {
            manifold.CollisionType = CollisionType.Colliding;
        }
        else if (manifold.PenetrationDistance < 0)
        {
            manifold.CollisionType = CollisionType.Penetrating;
        }
        else
        {
            manifold.CollisionType = CollisionType.None;
            return(manifold);
        }

        // manifold.Objects.Add(sphere.gameObject);
        // manifold.Objects.Add(box.gameObject);
        manifold.Normal = direction.normalized;

        return(manifold);
    }
Exemple #3
0
    internal bool CheckToDestroy(bool isForward, SphereBehaviour sb)                                                                    //виклик по ланцюгу перевірки на один колір
    {
        if ((sb.TypeSphere != TypeSphere && sb.TypeSphere != TypesSphere.MULCOLOR && TypeSphere != TypesSphere.MULCOLOR) || Health > 0) //перервати виконання, в разі якщо кулі не однакові
        {
            // Debug.Log("3 sb.TypeSphere != TypesSphere.MULCOLOR = " + sb.TypeSphere);
            return(false);
        }

        //Debug.Log("Add. forward="+ isForward +" Type="+TypeSphere);
        BallController.GetBalls(RespIndex).Add(gameObject);
        //Debug.Log("1_destroyList.count=" + BallController.GetBalls(RespIndex).Count);

        if (isForward)
        {
            if (FrontBall != null)
            {
                BallController.lastForwardBallIndex++;
                FrontBall.GetComponent <BallBehaviour>().CheckToDestroy(true, this);
            }
        }
        else if (BackBall != null)
        {
            BackBall.GetComponent <BallBehaviour>().CheckToDestroy(false, this);
        }

        return(true);
    }
Exemple #4
0
    public static CollisionManifold GetSphereToSphereCollisionManifold(SphereBehaviour sphereA, SphereBehaviour sphereB)
    {
        var manifold = new CollisionManifold();

        var direction = sphereA.transform.position - sphereB.transform.position;

        manifold.PenetrationDistance = (direction.magnitude - (sphereA.Radius + sphereB.Radius)) / 2;

        if (manifold.PenetrationDistance <= 0.001f && manifold.PenetrationDistance > 0)
        {
            manifold.CollisionType = CollisionType.Colliding;
        }
        else if (manifold.PenetrationDistance < 0)
        {
            manifold.CollisionType = CollisionType.Penetrating;
        }
        else
        {
            manifold.CollisionType = CollisionType.None;
            return(manifold);
        }

        manifold.Objects.Add(sphereA.gameObject);
        manifold.Objects.Add(sphereB.gameObject);
        manifold.Normal = direction.normalized;

        return(manifold);
    }
    void OnMouseDown()
    {
        /*if (launchSphere == null)
         * {
         *  BallController.RedyToRunNewPlayerBall = true;
         * }
         * else if (launchSphere.tag != "player")
         *  BallController.RedyToRunNewPlayerBall = true;*/

        if (!BallController.RedyToRunNewPlayerBall)
        {
            return;
        }

        //Debug.Log("-=2=-");
        BallController.RedyToRunNewPlayerBall = false;

        if (moveTo == null)
        {
            Debug.Log("не вказаний moveTo об'єкт в редакторі");
            return;
        }

//      moveToPos.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z));
        moveTo.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
        moveTo.position = new Vector3(moveTo.position.x - map.player.transform.position.x, moveTo.position.y - map.player.transform.position.y, 0);

        //Debug.Log("coord X=" + moveTo.position.x);
        //Debug.Log("coord Y=" + moveTo.position.y);
        //Vector3 relativePos = moveTo.position - player.transform.position;
        // transform.rotation = Quaternion.LookRotation(player.transform.forward, relativePos);
        // player.transform.LookAt((new Vector3(moveTo.position.x, moveTo.position.y, player.transform.position.z ));
        player.transform.rotation    = Quaternion.LookRotation(transform.forward, new Vector3(moveTo.position.x, moveTo.position.y, player.transform.position.z));
        newSphere.transform.position = map.pointToRespawnPlayersBall.position;
        //newSphere.tag = "player";
        SphereBehaviour sb = newSphere.GetComponent <SphereBehaviour>();

        sb.Speed = Speed;
        sb.Move(moveTo);
        launchSphere = newSphere;

        //Звук
        if (launchBallSound != null)
        {
            launchBallSound.Play();
        }

        TypesSphere typeSphere = ballCreator.randomType(true, CountColor);

        newSphere = ballCreator.getBall(ballTransform, map.pointToRespawnPlayersBall.position, typeSphere).gameObject;
        sb        = newSphere.GetComponent <SphereBehaviour>();
        //sb.TypeSphere = typeSphere;
        //Debug.Log(sb.TypeSphere);
        // newSphere = null;                   //прибрати
    }
Exemple #6
0
    //Повертає шар певного кольору
    public Transform getBall(Transform ball,           //який об'єкт створювати
                             Vector3 placeToCreate,    //в якій позиції
                             TypesSphere typeBall)     //якого кольору(чи іншого типу для некольорового)
    {
        Transform result = Transform.Instantiate(ball, placeToCreate, Quaternion.identity);

        SphereBehaviour sb = result.gameObject.GetComponent <SphereBehaviour>();

        sb.TypeSphere = typeBall;

        result.GetComponent <Renderer>().material.color = GetColorByType(typeBall);
        return(result);
    }
Exemple #7
0
    public static float AABBToSphereDistanceSq(SphereBehaviour sphere, CubeBehaviour box)
    {
        var dx = Mathf.Max(box.min.x - sphere.transform.position.x, 0);

        dx = Mathf.Max(dx, sphere.transform.position.x - box.max.x);
        var dy = Mathf.Max(box.min.y - sphere.transform.position.y, 0);

        dy = Mathf.Max(dy, sphere.transform.position.y - box.max.y);
        var dz = Mathf.Max(box.min.z - sphere.transform.position.z, 0);

        dz = Mathf.Max(dz, sphere.transform.position.z - box.max.z);

        return(dx * dx + dy * dy + dz * dz);
    }
Exemple #8
0
 void OpenInfo(SphereBehaviour desiredInfo)
 {
     foreach (SphereBehaviour info in infos)
     {
         if (info == desiredInfo)
         {
             info.OpenInfo();
         }
         else
         {
             info.CloseInfo();
         }
     }
 }
    private void OnTriggerEnter2D(Collider2D collBall)
    {
        if (collBall.gameObject.tag != "ball" && collBall.gameObject.tag != "newBall")      //перервати, якщо колізія не з кулею
        {
            return;
        }

        SphereBehaviour collBallBehaviour = collBall.gameObject.GetComponent <SphereBehaviour>();

        if (collBallBehaviour.FrontBall == null)
        {
            BallController.RedyToRunNewPlayerBall = true;
            if (BallController.blockPlayer)
            {
                BallController.blockPlayer = false;
            }
        }


        if (countToDestroy < 1)
        {
            scoreMessage?.Invoke(score);
            BallController.RedyToRunNewPlayerBall = true;
            // BallController.blockPlayer = false;
            //BallController.BallsLists[5].Add(gameObject);
            Destroy(gameObject);
            return;
        }

        if (isFirstColl)
        {
            pathPoints     = collBallBehaviour.pathPoints;
            destPointIndex = collBallBehaviour.destPointIndex;
            Speed          = 20.0f;
            Move(pathPoints, 1);
            isFirstColl = false;
            gameObject.GetComponent <AudioSource>()?.Play();
            //Debug.Log("isFirstColl");
        }

        Debug.Log("countToDestroy=" + countToDestroy);
        countToDestroy--;
        BallController.BallsLists[5].Add(collBall.gameObject);
        //Destroy(collBall.gameObject);
        score += 2;
    }
    public static void CheckAABBSphere(SphereBehaviour a, CubeBehaviour b)
    {
        var x        = Math.Max(b.min.x, Math.Min(a.transform.position.x, b.max.x));
        var y        = Math.Max(b.min.y, Math.Min(a.transform.position.y, b.max.y));
        var z        = Math.Max(b.min.z, Math.Min(a.transform.position.z, b.max.z));
        var distance = Math.Sqrt((x - a.transform.position.x) * (x - a.transform.position.x) +
                                 (y - a.transform.position.y) * (y - a.transform.position.y) +
                                 (z - a.transform.position.z) * (z - a.transform.position.z));
        Vector3 norm;

        norm.x = x;
        norm.y = y;
        norm.z = z;
        norm.Normalize();
        if (distance < a.size.x / 2)
        {
            if (!a.contacts.Contains(b))
            {
                a.contacts.Add(b);
                a.isColliding = true;
                a.newVelocity = a.velocity - Vector3.Scale(((0.5f * a.mass * Vector3.Scale(a.velocity, a.velocity)) / a.mass), norm);

                Vector3 newBulletPosition = a.transform.position + (a.newVelocity * Time.deltaTime);

                a.transform.position = newBulletPosition;
            }
            else
            {
                a.contacts.Remove(b);
                a.isColliding = false;
            }
            if (!b.bulletContact.Contains(a))
            {
                b.bulletContact.Add(a);
                b.isColliding = true;
                b.newVelocity = b.velocity - Vector3.Scale(((0.5f * b.mass * Vector3.Scale(b.velocity, b.velocity)) / b.mass), norm);
                Vector3 newCubePosition = b.transform.position + (b.newVelocity * Time.deltaTime);
                b.transform.position = newCubePosition;
            }
            else
            {
                b.bulletContact.Remove(a);
                b.isColliding = false;
            }
        }
    }
Exemple #11
0
    public static bool CheckAABBSphereBox(CubeBehaviour box, SphereBehaviour sphere)
    {
        var x = Mathf.Max(box.min.x, Mathf.Min(sphere.transform.position.x, box.max.x));
        var y = Mathf.Max(box.min.y, Mathf.Min(sphere.transform.position.y, box.max.y));
        var z = Mathf.Max(box.min.z, Mathf.Min(sphere.transform.position.z, box.max.z));

        var distance = Mathf.Sqrt((x - sphere.transform.position.x) * (x - sphere.transform.position.x) +
                                  (y - sphere.transform.position.y) * (y - sphere.transform.position.y) +
                                  (z - sphere.transform.position.z) * (z - sphere.transform.position.z));

        if (distance < sphere.Radius)
        {
            box.isColliding    = true;
            sphere.isColliding = true;
            return(true);
        }
        return(false);
    }
Exemple #12
0
    public static void CheckSphereAABB(SphereBehaviour sphere, CubeBehaviour box)
    {
        var x = Math.Max(box.min.x, Math.Min(sphere.transform.position.x, box.max.x));
        var y = Math.Max(box.min.y, Math.Min(sphere.transform.position.y, box.max.y));
        var z = Math.Max(box.min.z, Math.Min(sphere.transform.position.z, box.max.z));

        var distance = Math.Sqrt((x - sphere.transform.position.x) * (x - sphere.transform.position.x) +
                                 (y - sphere.transform.position.y) * (y - sphere.transform.position.y) +
                                 (z - sphere.transform.position.z) * (z - sphere.transform.position.z));

        if (distance >= sphere.Radius)
        {
            return;
        }

        var manifold = getSphereToAABBCollisionManifold(sphere, box);

        sphere.isColliding = true;
        box.isColliding    = true;

        var relativeVelocity = box.velocity - sphere.velocity;
        var relVelMagnitude  = Vector3.Dot(relativeVelocity, manifold.Normal);
        var bounciness       = Mathf.Min(box.bounciness, sphere.bounciness);
        // var scaledVelocity = (-bounciness) * relVelMagnitude;

        // print(String.Format("box mass {0} sphere mass {1}", box.mass, sphere.mass));

        var impulseMagnitude = -(1 + bounciness) * relVelMagnitude / (1 / sphere.mass + 1 / box.mass);

        sphere.velocity -= (impulseMagnitude / sphere.mass) * manifold.Normal;
        box.velocity    += (impulseMagnitude / box.mass) * manifold.Normal;

        var tangentVector     = relativeVelocity - (relVelMagnitude) * manifold.Normal;
        var frictionMagnitude = -(1 + bounciness) * Vector3.Dot(relativeVelocity, tangentVector) / (1 / sphere.mass + 1 / box.mass);
        var friction          = Mathf.Sqrt(sphere.friction * box.friction);

        frictionMagnitude = Mathf.Max(frictionMagnitude, -impulseMagnitude * friction);
        frictionMagnitude = Mathf.Min(frictionMagnitude, impulseMagnitude * friction);

        sphere.velocity -= (frictionMagnitude / sphere.mass) * manifold.Normal;
        box.velocity    += (frictionMagnitude / box.mass) * manifold.Normal;
    }
Exemple #13
0
 public static void UpdateVariable(UpdatableVar var)
 {
     SphereBehaviour.UpdateVariable(var);
 }
Exemple #14
0
 // Use this for initialization
 void Start()
 {
     sphereBehaviour = GetComponent <SphereBehaviour>();
     light           = GetComponent <Light>();
 }
Exemple #15
0
    public static bool AABBToSphereIntersect(SphereBehaviour sphere, CubeBehaviour box)
    {
        var dSq = AABBToSphereDistanceSq(sphere, box);

        return(dSq <= sphere.Radius * sphere.Radius);
    }
 public void Reset(SphereBehaviour sphere)
 {
     sphere.velocity           = Vector3.zero;
     sphere.isUsed             = false;
     sphere.transform.position = new Vector3(0, 0, -100);
 }