예제 #1
0
    private bool canPlaceObject(float halfSize, Vector3 pos)
    {
        //Make sure it does not overlap with any thing to avoid
        for (int i = 0; i < thingsToAvoid.Length; i++)
        {
            float dist = Vector3.Distance(thingsToAvoid[i].position, pos);

            if (dist < halfSize + thingsToAvoidRadius[i])
            {
                return(false);
            }
        }

        //Make sure it does not overlap with any existing object
        foreach (Rigidbody o in objs)
        {
            float dist = Vector3.Distance(o.position, pos);

            float oRadius = SteeringBasics.getBoundingRadius(o.transform);

            if (dist < oRadius + spaceBetweenObjects + halfSize)
            {
                return(false);
            }
        }

        return(true);
    }
예제 #2
0
    public Vector2 getSteering(ICollection <Rigidbody2D> targets)
    {
        Vector2 acceleration = Vector2.zero;

        foreach (Rigidbody2D r in targets)
        {
            /* Get the direction and distance from the target */
            Vector2 direction = (Vector2)transform.position - r.position;
            float   dist      = direction.magnitude;

            if (dist < maxSepDist)
            {
                float targetRadius = SteeringBasics.getBoundingRadius(r.transform);

                /* Calculate the separation strength (can be changed to use inverse square law rather than linear) */
                var strength = sepMaxAcceleration * (maxSepDist - dist) / (maxSepDist - boundingRadius - targetRadius);

                /* Added separation acceleration to the existing steering */
                direction.Normalize();
                acceleration += direction * strength;
            }
        }

        return(acceleration);
    }
예제 #3
0
    // Use this for initialization
    void Start()
    {
        //Find the size of the map
        float z = -1 * Camera.main.transform.position.z;

        bottomLeft = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, z));
        Vector3 topRight = Camera.main.ViewportToWorldPoint(new Vector3(1, 1, z));

        widthHeight = topRight - bottomLeft;

        //Find the radius' of the things to avoid
        thingsToAvoidRadius = new float[thingsToAvoid.Length];

        for (int i = 0; i < thingsToAvoid.Length; i++)
        {
            thingsToAvoidRadius[i] = SteeringBasics.getBoundingRadius(thingsToAvoid[i].transform);
        }

        //Create the create the objects
        for (int i = 0; i < numberOfObjects; i++)
        {
            //Try to place the objects multiple times before giving up
            for (int j = 0; j < 10; j++)
            {
                if (tryToCreateObject())
                {
                    break;
                }
            }
        }
    }
예제 #4
0
    private Vector3 getHidingPosition(Rigidbody2D obstacle, Rigidbody2D target)
    {
        float distAway = SteeringBasics.getBoundingRadius(obstacle.transform) + distanceFromBoundary;

        Vector2 dir = obstacle.position - target.position;

        dir.Normalize();

        return(obstacle.position + dir * distAway);
    }
예제 #5
0
 // Use this for initialization
 void Start()
 {
     boundingRadius = SteeringBasics.getBoundingRadius(transform);
 }
예제 #6
0
    public Vector2 getSteering(ICollection <Rigidbody2D> targets)
    {
        Vector2 acceleration = Vector2.zero;

        /* 1. Find the target that the character will collide with first */

        /* The first collision time */
        float shortestTime = float.PositiveInfinity;

        /* The first target that will collide and other data that
         * we will need and can avoid recalculating */
        Rigidbody2D firstTarget = null;
        //float firstMinSeparation = 0, firstDistance = 0;
        float   firstMinSeparation = 0, firstDistance = 0, firstRadius = 0;
        Vector2 firstRelativePos = Vector2.zero, firstRelativeVel = Vector2.zero;

        foreach (Rigidbody2D r in targets)
        {
            /* Calculate the time to collision */
            Vector2 relativePos   = (Vector2)transform.position - r.position;
            Vector2 relativeVel   = rb.velocity - r.velocity;
            float   distance      = relativePos.magnitude;
            float   relativeSpeed = relativeVel.magnitude;

            if (relativeSpeed == 0)
            {
                continue;
            }

            float timeToCollision = -1 * Vector2.Dot(relativePos, relativeVel) / (relativeSpeed * relativeSpeed);

            /* Check if they will collide at all */
            Vector2 separation    = relativePos + relativeVel * timeToCollision;
            float   minSeparation = separation.magnitude;

            float targetRadius = SteeringBasics.getBoundingRadius(r.transform);

            if (minSeparation > characterRadius + targetRadius)
            //if (minSeparation > 2 * agentRadius)
            {
                continue;
            }

            /* Check if its the shortest */
            if (timeToCollision > 0 && timeToCollision < shortestTime)
            {
                shortestTime       = timeToCollision;
                firstTarget        = r;
                firstMinSeparation = minSeparation;
                firstDistance      = distance;
                firstRelativePos   = relativePos;
                firstRelativeVel   = relativeVel;
                firstRadius        = targetRadius;
            }
        }

        /* 2. Calculate the steering */

        /* If we have no target then exit */
        if (firstTarget == null)
        {
            return(acceleration);
        }

        /* If we are going to collide with no separation or if we are already colliding then
         * steer based on current position */
        if (firstMinSeparation <= 0 || firstDistance < characterRadius + firstRadius)
        //if (firstMinSeparation <= 0 || firstDistance < 2 * agentRadius)
        {
            acceleration = (Vector2)transform.position - firstTarget.position;
        }
        /* Else calculate the future relative position */
        else
        {
            acceleration = firstRelativePos + firstRelativeVel * shortestTime;
        }

        /* Avoid the target */
        acceleration.Normalize();
        acceleration *= maxAcceleration;

        return(acceleration);
    }
예제 #7
0
    // Use this for initialization
    void Start()
    {
        characterRadius = SteeringBasics.getBoundingRadius(transform);

        rb = GetComponent <Rigidbody2D>();
    }