Exemplo n.º 1
0
    private void Flee(YellowBird bird, Vector2 target)
    {
        Vector2 accel    = (Vector2)bird.transform.position - target;
        float   strength = Mathf.Min(max_accel / accel.magnitude, max_accel);

        bird.GetComponent <Rigidbody2D>().AddForce(accel.normalized * strength);
    }
Exemplo n.º 2
0
    void Separate(YellowBird bird, YellowBird other_bird)
    {
        Vector2 direction = bird.transform.position - other_bird.transform.position;
        float   distance  = direction.magnitude;
        float   strength  = Mathf.Min(k / distance * distance, max_accel);

        bird.GetComponent <Rigidbody2D>().AddForce(direction.normalized * strength);
    }
Exemplo n.º 3
0
    public void CollisionPredict(YellowBird bird)
    {
        GameObject[] allObjects = UnityEngine.Object.FindObjectsOfType <GameObject>();
        float        min_dist   = Mathf.Infinity;
        Vector2      min_bird   = new Vector2();
        Vector2      min_go     = new Vector2();

        foreach (GameObject go in allObjects)
        {
            if (go.tag == "RedBird")
            {
                Vector2 dp = go.transform.position - bird.transform.position;

                if (dp.magnitude > consider_dist)
                {
                    continue;
                }

                Vector2 dv = go.GetComponent <Rigidbody2D>().velocity - bird.GetComponent <Rigidbody2D>().velocity;

                float t_2_closest = -1 * (Vector2.Dot(dp, dv) / (dv.magnitude * dv.magnitude));


                Vector2 p_bird_close = (Vector2)bird.transform.position + bird.GetComponent <Rigidbody2D>().velocity *t_2_closest;
                Vector2 p_go_close   = (Vector2)go.transform.position + go.GetComponent <Rigidbody2D>().velocity *t_2_closest;

                if (Vector2.Distance(p_bird_close, p_go_close) < min_dist)
                {
                    min_dist = Vector2.Distance(p_bird_close, p_go_close);
                    min_bird = p_bird_close;
                    min_go   = p_go_close;
                }
            }
        }

        if (Vector2.Distance(min_bird, min_go) < dist_tol)
        {
            //print("Yellow Bird" + bird.name + " is evading " + go.name);

            Vector2 accel    = min_bird - min_go;
            float   strength = Mathf.Min(max_accel / accel.magnitude, max_accel);
            bird.GetComponent <Rigidbody2D>().AddForce(accel.normalized * strength);
        }
    }
Exemplo n.º 4
0
    private void AlignToMovement(YellowBird bird)
    {
        Vector2 direction = (Vector2)bird.GetComponent <Rigidbody2D>().velocity.normalized;
        float   angle     = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
        float   rot_speed;

        if (Mathf.Abs(angle) > slowRadius)
        {
            rot_speed = max_rot_speed;
        }
        else
        {
            rot_speed = angle / slowRadius;
        }

        bird.transform.localRotation = Quaternion.Slerp(bird.transform.localRotation, Quaternion.AngleAxis(angle, Vector3.forward), Time.deltaTime * rot_speed);
    }