Пример #1
0
 /// <summary>
 /// Applies KinematicSteering velocity to a RigidBody and rotates an object to look in the direction it's moving
 /// </summary>
 /// <param name="steering">Steering.</param>
 public void setOrientations(KinematicSteering steering, Vector3 goal)
 {
     if (pathBlocked)
     {
         currentTarget = tempTarget;
     }
     else
     {
         currentTarget = goal;
     }
     this.GetComponent <Rigidbody> ().velocity = steering.velocity;
     //Checks to see if magnitude of vector is > 1.6f, because if it's not, the player will turn to look up given a sufficiently small radius.
     //x of each Vector3 is 0 so that the sprite will not constantly look at the floor
     if (steering.velocity.magnitude > 1.6f)
     {
         //If this is not the "Leader", SetLookRotation in the directrion of the Leader's forward vector
         if (!tag.Equals("Leader"))
         {
             //NOTE: might have to change the way LookRotation is working if entity must always face movement direction
             steering.rotation.SetLookRotation(obj.transform.forward);
             //if (pathBlocked)
             //	steering.rotation.SetLookRotation (steering.velocity);
             //else
             //	steering.rotation.SetLookRotation (obj.transform.forward);
         }
         else
         {
             steering.rotation.SetLookRotation((currentTarget - transform.position).normalized);
         }
         this.transform.rotation = Quaternion.Slerp
                                       (Quaternion.Euler(new Vector3(0f, this.transform.rotation.eulerAngles.y, 0f)),
                                       Quaternion.Euler(new Vector3(0f, steering.rotation.eulerAngles.y + rotationOffset, 0f)), Time.deltaTime * turnSpeed);
     }
 }
Пример #2
0
    // Update is called once per frame
    void Update()
    {
        ds = new DynoSteering();

        // Decide on behavior
        //seeking_output = seek.updateSteering();
        seeking_output = arrive.getSteering();
        //seeking_output = seek.getSteering();
        char_kinematic.setVelocity(seeking_output.velc);

        // Manually set orientation for now
        if (dynoAlign && dynoAlign.enabled)
        {
            ds = dynoAlign.getSteering();
        }
        else
        {
            float new_orient = char_kinematic.getNewOrientation(seeking_output.velc);
            char_kinematic.setOrientation(new_orient);
            char_kinematic.setRotation(0f);
        }

        // Update Kinematic Steering
        kso = char_kinematic.updateSteering(ds, Time.deltaTime);

        transform.position = new Vector3(kso.position.x, transform.position.y, kso.position.z);
        transform.rotation = Quaternion.Euler(0f, kso.orientation * Mathf.Rad2Deg, 0f);

        if (recordLogs && logWriter && logWriter.enabled)
        {
            logWriter.Write(char_kinematic.getVelocity().magnitude.ToString());
        }
    }
Пример #3
0
    // Update is called once per frame
    public KinematicSteering getSteering()
    {
        ks   = new KinematicSteering();
        goal = goalObject.getGoal();
        //steering = new Steering();
        Vector3 new_velc = goal.position - transform.position;

        if (new_velc.magnitude < radius_of_satisfaction)
        {
            new_velc = new Vector3(0f, 0f, 0f);
            ks.velc  = new_velc;
            return(ks);
        }

        new_velc = new_velc / time_to_target;

        // clip speed
        if (new_velc.magnitude > sp.MAXSPEED)
        {
            new_velc.Normalize();
            new_velc = new_velc * sp.MAXSPEED;
        }

        ks.velc = new_velc;

        return(ks);
    }
Пример #4
0
    // Update is called once per frame
    void Update()
    {
        ks = new KinematicSteering();

        ds        = new DynoSteering();
        ds.torque = align.getSteering().torque;

        // Decide on behavior
        //seeking_output = seek.updateSteering();
        seeking_output = arrive.getSteering();
        //seeking_output = seek.getSteering();
        char_kinematic.setVelocity(seeking_output.velc);

        // Manually set orientation for now
        //float new_orient = char_kinematic.getNewOrientation(seeking_output.velc);
        //char_kinematic.setOrientation(new_orient);
        //char_kinematic.setRotation(0f);

        // Update Kinematic Steering
        kso = char_kinematic.updateSteering(ds, Time.deltaTime);

        transform.position = new Vector3(kso.position.x, transform.position.y, kso.position.z);
        transform.rotation = Quaternion.Euler(0f, kso.orientation * Mathf.Rad2Deg, 0f);

        //Logger.instance.WriteToFileKinematic("speed: " + char_kinematic.getVelocity().magnitude + " time: " + Time.time);
    }
 // Update is called once per frame
 public KinematicSteering getSteering()
 {
     goal          = goalObject.getGoal();
     steering      = new KinematicSteering();
     steering.velc = goal.position - this.transform.position;
     steering.velc.Normalize();
     steering.velc = steering.velc * sp.MAXSPEED;
     return(steering);
 }
Пример #6
0
 void Start()
 {
     timer = .1f;
     //Radius is set in code for ease across all entities
     radius   = 0f;
     steering = getSteering(steering, target);
     if (steering != null)
     {
         setOrientations(steering, target);
     }
 }
Пример #7
0
    // Update is called once per frame
    void Update()
    {
        ks = new KinematicSteering();
        ds = new DynoSteering();

        // Decide on behavior
        //seeking_output = seek.updateSteering();
        //seeking_output = seek.getSteering();


        //seeking_output = arrive.getSteering();
        //char_kinematic.setVelocity(seeking_output.velc);


        //// Manually set orientation for now
        //float new_orient = char_kinematic.getNewOrientation(seeking_output.velc);
        //char_kinematic.setOrientation(new_orient);
        //char_kinematic.setRotation(0f);


        Vector3 acceleration = wander.getSteering();

        ds.force = acceleration;

        // Update Kinematic Steering
        kso = char_kinematic.updateSteering(ds, Time.deltaTime);

        transform.position = new Vector3(kso.position.x, transform.position.y, kso.position.z);


        Vector2 direction = new Vector2(char_kinematic.getVelocity().x, char_kinematic.getVelocity().z);
        float   turnSpeed = 20.0f;

        direction.Normalize();

        // If we have a non-zero direction then look towards that direciton otherwise do nothing
        if (direction.sqrMagnitude > 0.001f)
        {
            float toRotation = (Mathf.Atan2(direction.x, direction.y) * Mathf.Rad2Deg);
            float rotation   = Mathf.LerpAngle(transform.rotation.eulerAngles.y, toRotation, Time.deltaTime * turnSpeed);

            transform.rotation = Quaternion.Euler(0, rotation, 0);
        }

//        transform.rotation = Quaternion.Euler(0f, kso.orientation * Mathf.Rad2Deg, 0f);
    }
Пример #8
0
    private void kinematicSeekBehavior()
    {
        ds = new DynoSteering();

        // Decide on behavior
        seeking_output = seek.getSteering();
        char_kinematic.setVelocity(seeking_output.velc);
        // Manually set orientation for now
        float new_orient = char_kinematic.getNewOrientation(seeking_output.velc);

        char_kinematic.setOrientation(new_orient);
        char_kinematic.setRotation(0f);

        // Update Kinematic Steering
        kso = char_kinematic.updateSteering(ds, Time.deltaTime);

        transform.position = new Vector3(kso.position.x, transform.position.y, kso.position.z);
        transform.rotation = Quaternion.Euler(0f, kso.orientation * Mathf.Rad2Deg, 0f);
    }
Пример #9
0
    /// <summary>
    /// Gets the steering. Sets steering velocity and steering rotation.
    /// </summary>
    /// <returns>The steering.</returns>
    public KinematicSteering getSteering(KinematicSteering steering, Vector3 goal)
    {
        if (steering == null)
        {
            steering = new KinematicSteering();
        }

        //If the path is not blocked, continue pathing towards the intended target.
        if (!pathBlocked)
        {
            steering.velocity = goal - this.transform.position;
        }
        else
        {
            //tempTarget is assigned in avoidObstacles and is relative to a detected obstacle.
            steering.velocity = tempTarget - this.transform.position;
        }

        //If the player has hit the satisfaction radius, stop the player and return steering.
        if (steering.velocity.magnitude <= radius)
        {
            steering.velocity = Vector3.zero;
            setOrientations(steering, target);
            return(steering);
        }

        /*if (this.tag == "Leader")
         *      steering.velocity = steering.velocity.normalized * 15;*/

        steering.velocity /= timeToTarget;

        //Limit player speed moving between two points.
        if (steering.velocity.magnitude > maxSpeed)
        {
            steering.velocity.Normalize();
            steering.velocity *= maxSpeed;
        }

        return(steering);
    }
Пример #10
0
        // Update is called once per frame
        void Update()
        {
            ks = new KinematicSteering();
            ds = new DynoSteering();

            // Decide on behavior

            //seeking_output = seek.updateSteering();
            //******seeking_output = arrive.getSteering();
            seeking_output = arrive.getSteering();
            ds_torque      = align.getSteering();
            //******
            //seeking_output = seek.getSteering();
            char_kinematic.setVelocity(seeking_output.velc);

            // Manually set orientation for now
            /*Replace these three lines with a dynamic rotation*/
            //float new_orient = char_kinematic.getNewOrientation(seeking_output.velc);
            //char_kinematic.setOrientation(new_orient);
            //char_kinematic.setRotation(0f);

            ds.torque = ds_torque.torque;

            // Update Kinematic Steering
            kso = char_kinematic.updateSteering(ds, Time.deltaTime);

            transform.position = new Vector3(kso.position.x, transform.position.y, kso.position.z);
            transform.rotation = Quaternion.Euler(0f, kso.orientation * Mathf.Rad2Deg, 0f);


            /*record and write out speed on timer*/
            string t = (Time.time - StartTime).ToString();

            using (System.IO.StreamWriter file =
                       new System.IO.StreamWriter(@"C:\Grad School\AI\A1\" + FileName, true))
            {
                file.WriteLine(t + " | " + char_kinematic.getVelocity().ToString());
            }
        }
Пример #11
0
    // Update is called once per frame
    void Update()
    {
        if (isWandering)
        {
            //CheckForWallAndTakeDecision();
        }

        if (isWandering)
        {
            GetWanderGoal();
            ds_force = arrive.getSteering();
            ds       = align.getSteering();
            ds.force = ds_force.force;
        }
        else
        {
            KinematicSteering ks = kinematicArrive.getSteering();

            char_RigidBody.setVelocity(ks.velc);

            //instantly set rotation
            float new_orient = char_RigidBody.getNewOrientation(ds_force.force);
            char_RigidBody.setOrientation(new_orient);
            char_RigidBody.setRotation(0f);
        }

        // Update Kinematic Steering
        kso = char_RigidBody.updateSteering(ds, Time.deltaTime);
        //Debug.Log(kso.position);
        transform.position = new Vector3(kso.position.x, transform.position.y, kso.position.z);
        float rotation = kso.orientation * Mathf.Rad2Deg;

        transform.rotation = Quaternion.Euler(0f, rotation, 0f);

        if (!isWandering && (arrive.HasArrived() || kinematicArrive.HasArrived()))
        {
            isWandering = true;
        }
    }
Пример #12
0
 void Update()
 {
     mouseClickLocation = GameController.instance.getMouseVector();
     //The GameObject tagged "Leader" is the GameObject which all other entities follow with their steering.
     if (this.tag.Equals("Leader"))
     {
         return;
         //target = GameController.instance.getMouseVector ();
     }
     else
     {
         //If this is not the leader, then calculate the offset position relative to the leader.
         //obj is a reference to the "Leader" GameObject
         offsetPosition   = new Vector3(obj.transform.forward.x, 0f, obj.transform.forward.z) * -distance;
         offsetPosition   = Quaternion.AngleAxis(angle, obj.transform.up) * offsetPosition;
         offsetPosition.y = 0f;
         target           = new Vector3(obj.transform.position.x + offsetPosition.x, 2f,
                                        obj.transform.position.z + offsetPosition.z);
         eventualLocation = new Vector3(mouseClickLocation.x + offsetPosition.x, 2f,
                                        mouseClickLocation.z + offsetPosition.z);
         //Checks if the target is in a pathable location (not an obstacle)
         checkTargetPathable(eventualLocation);
     }
     steering = getSteering(steering, target);
     setOrientations(steering, target);
     //If the path is blocked by an obstacle, the protocol in OnCollisionStay will trigger and function for
     //.35f seconds at a time while the path remains blocked.
     if (pathBlocked)
     {
         timer -= Time.deltaTime;
         if (timer <= 0f)
         {
             timer       = .1f;
             pathBlocked = false;
         }
     }
 }
Пример #13
0
        private void kinematicSeekBehavior()
        {
            ds = new DynoSteering();

            // Decide on behavior
            //******
            seeking_output = seek.getSteering();
            //******seeking_output = align.getSteering();
            //******
            char_kinematic.setVelocity(seeking_output.velc);
            // Manually set orientation for now
            /*Replace these three lines with a dynamic rotation*/
            //float new_orient = char_kinematic.getNewOrientation(seeking_output.velc);
            //char_kinematic.setOrientation(new_orient);
            //char_kinematic.setRotation(0f);

            ds.torque = ds_torque.torque;

            // Update Kinematic Steering
            kso = char_kinematic.updateSteering(ds, Time.deltaTime);

            transform.position = new Vector3(kso.position.x, transform.position.y, kso.position.z);
            transform.rotation = Quaternion.Euler(0f, kso.orientation * Mathf.Rad2Deg, 0f);
        }