// 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);
    }
    //funcion que realiza seeking de un punto si le pasas 1 y flee si le pasas
    //0
    public KinematicSteeringOutput getSteering2(Vector3 targetPosition, int seek_or_flee)
    {
        //velocidades de salida
        KinematicSteeringOutput steering = new KinematicSteeringOutput();

        if (seek_or_flee == 1)
        {
            steering.velocity = targetPosition - character.transform.position;
        }
        else
        {
            steering.velocity = character.transform.position - targetPosition;
        }

        steering.velocity.Normalize();

        steering.velocity *= maxspeed;


        character.GetNewOrietation(steering.velocity);

        steering.rotation = 0f;

        return(steering);
    }
Beispiel #3
0
    KinematicSteeringOutput getSteering()
    {
        KinematicSteeringOutput result = new KinematicSteeringOutput();

        // Get direction to target
        result.velocity = target.transform.position - character.transform.position;

        // Check if within radius
        if (result.velocity.magnitude < radius)
        {
            //request no steering
            return(null);
        }

        // move to target
        result.velocity /= timeToTarget;

        // If too fast, maxSpeed
        if (result.velocity.magnitude > maxSpeed)
        {
            result.velocity.Normalize();
            result.velocity *= maxSpeed;
        }

        // Face in the direction we want to move.
        character.newOrientation(character.transform.rotation.eulerAngles.z, result.velocity);

        result.rotation = 0;
        return(result);
    }
Beispiel #4
0
    // Calculate steering for a seeking AI
    KinematicSteeringOutput GetSteering(GameObject target)
    {
        // Create structure for output
        KinematicSteeringOutput steering = new KinematicSteeringOutput();

        // Get direction to the target
        steering.vel = target.transform.position - transform.position;

        // Check if we're within radius
        if (steering.vel.magnitude < radius)
        {
            // Return no steering
            KinematicSteeringOutput noVel = new KinematicSteeringOutput();
            noVel.vel = Vector3.zero;
            noVel.rot = steering.rot;
            return(noVel);
        }

        // We need to move to the target, we'd like to get there in timeToTarget seconds
        steering.vel = steering.vel / timeToTarget;

        // If it's too fast then set it to max speed
        if (steering.vel.magnitude > maxSpeed)
        {
            steering.vel.Normalize();
            steering.vel = steering.vel * maxSpeed;
        }

        // Face the direction that the character is moving
        steering.rot = GetNewOrientation(transform.rotation.eulerAngles.z, steering.vel);

        return(steering);
    }
    public KinematicSteeringOutput GetSteering()
    {
        KinematicSteeringOutput steering = new KinematicSteeringOutput();

        steering.velocity = target.position - character.position;
        bool shouldStop = steering.velocity.magnitude < goalRadius;
        if (shouldStop)
        {
            steering.velocity = Vector3.zero;
            steering.rotation = Angle.WithDeg(0);
            return steering;
        }

        // get to the target in timeToTarget seconds
        steering.velocity /= timeToTarget;

        // clip to max speed
        if (steering.velocity.magnitude > maxSpeed)
        {
            steering.velocity.Normalize();
            steering.velocity *= maxSpeed;
        }

        steering.rotation = Angle.WithDeg(0);
        return steering;
    }
Beispiel #6
0
    //funcion que realiza seeking de un punto y se detiene si lo alcanza en un radio
    //dado
    public KinematicSteeringOutput getSteering()
    {
        //velocidades de salida
        KinematicSteeringOutput steering = new KinematicSteeringOutput();


        steering.velocity = target.transform.position - character.transform.position;

        if (steering.velocity.magnitude < radius)//si ya estamos en el radio
        {
            steering.rotation = 0f;
            steering.velocity = Vector3.zero;
            return(steering);
        }

        //tenemos que ajustar la velocidad para que
        //cada vez que este mas cerca vaya mas lento
        steering.velocity /= timeToTarget;

        if (steering.velocity.magnitude > maxspeed)
        {
            steering.velocity.Normalize();
            steering.velocity *= maxspeed;
        }

        character.GetNewOrietation(steering.velocity);

        steering.rotation = 0f;
        return(steering);
    }
Beispiel #7
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());
        }
    }
Beispiel #8
0
    public KinematicSteeringOutput GetSteering()
    {
        KinematicSteeringOutput output = new KinematicSteeringOutput();

        // Get the direction to the target.
        Vector3 direction = Character.Target.transform.position - transform.position;

        direction.y = 0;

        float targetRotation = Quaternion.LookRotation(direction, Vector3.up).eulerAngles.y;

        output.Rotation = AngleMapper.MapDegreesMidpointZero(targetRotation);

        // Check if we’re outside radius.
        if (direction.magnitude > SatisfactionRadius)
        {
            output.Velocity = direction.normalized * Character.MaxSpeed;
            return(output); // If so, we're done
        }
        else if (direction.magnitude > InnerStoppingRadius)
        {
            // Otherwise, we use t2t.
            float adjustedSpeed = Mathf.Min(Character.MaxSpeed, direction.magnitude / TimeToTarget);
            output.Velocity = direction.normalized * adjustedSpeed;
        }
        else
        {
            output.Velocity = Vector3.zero; // Stop
        }

        return(output);
    }
    //update loop to update position and orientation from other functions
    void Update()
    {
        KinematicSteeringOutput blackMagic = getSteering();

        enemy.transform.position    += blackMagic.velocityTrans * Time.deltaTime;
        enemy.transform.eulerAngles += blackMagic.velocityAng;
    }
Beispiel #10
0
    // Update is called once per frame
    void Update()
    {
        Vector3 pos = new Vector3();

        pos      = myKinematic.Position;
        steering = myAI.getSteering(this.myKinematic);
        myKinematic.update(steering, Time.deltaTime);
        pos = myKinematic.Position;
        //check boundaries
        if (pos.x > 25.0f)
        {
            pos.x = -25.0f;
        }
        if (pos.x < -25.0f)
        {
            pos.x = 25.0f;
        }

        if (pos.z > 25.0f)
        {
            pos.z = -25.0f;
        }
        if (pos.z < -25.0f)
        {
            pos.z = 25.0f;
        }
        myKinematic.Position       = pos;
        this.transform.position    = pos;
        this.transform.eulerAngles = new Vector3(0f, myKinematic.Orientation * 180f / Mathf.PI, 0f);
        //Debug.Log ("BLOCK POSITION: " + pos);
    }
Beispiel #11
0
    //update loop to update position and orientation from other functions
    void FixedUpdate()
    {
        KinematicSteeringOutput blackMagic = getSteering();

        enemy.transform.position   += blackMagic.velocityTrans * Time.deltaTime; //updates translational pos
        enemy.transform.eulerAngles = blackMagic.velocityAng;                    //updates rotational pos
    }
Beispiel #12
0
    //creates object with velocity and orientation, then canlculates and returns new orientation and velocity
    KinematicSteeringOutput getSteering()
    {
        KinematicSteeringOutput result = new KinematicSteeringOutput();
        float angleDeg;

        result.velocityTrans = player.position - enemy.position; //calc dif in position

        if (result.velocityTrans.magnitude < radius)
        {
            angleDeg           = newOrientation(enemy.transform.eulerAngles.y, result.velocityTrans); //call orientation to get orientation
            angleDeg          *= Mathf.Rad2Deg;                                                       //turn rad to deg
            result.velocityAng = new Vector3(0, angleDeg, 0);

            result.velocityTrans = Vector3.zero;

            return(result);
        }

        result.velocityTrans /= timeToTarget;

        if (result.velocityTrans.magnitude > maxSpeed)
        {
            result.velocityTrans.Normalize(); //normalize previous difference
            result.velocityTrans *= maxSpeed; //multiply speed by vector velocity
        }

        angleDeg  = newOrientation(enemy.transform.eulerAngles.y, result.velocityTrans); //call orientation to get orientation
        angleDeg *= Mathf.Rad2Deg;                                                       //turn rad to deg

        result.velocityAng = new Vector3(0, angleDeg, 0);                                //set orientation to vector3

        return(result);
    }
Beispiel #13
0
    // Update is called once per frame
    public KinematicSteeringOutput updateSteering(DynoSteering ds, float time)
    {
        position = this.transform.position;

        steering = new KinematicSteeringOutput();

        // make Updates
        position    += velc * time;
        orientation += rotation * time;

        velc        += ds.force * time;
        orientation += ds.torque * time;

        if (velc.magnitude > sp.MAXSPEED)
        {
            velc.Normalize();
            velc = velc * sp.MAXSPEED;
        }

        steering.position    = position;
        steering.orientation = orientation;



        return(steering);
    }
    protected override Vector3 getVelocity(KinematicSteeringOutput steering)
    {
        Vector3 vec = new Vector3(Mathf.Sin(Character.Orientation), 0f, Mathf.Cos(Character.Orientation));

        steering.Velocity = vec;
        return(base.Maxspeed * steering.Velocity.normalized);
    }
Beispiel #15
0
    void Update()
    {
        KinematicSteeringOutput newSteering = getSteering();

        character.position += newSteering.velocity * Time.deltaTime;

        character.eulerAngles = newSteering.angularVelocity;
    }
Beispiel #16
0
    public KinematicSteeringOutput GetSteering()
    {
        KinematicSteeringOutput output = new KinematicSteeringOutput
        {
            Velocity = Character.MaxSpeed * transform.forward,                                          // Get velocity from the vector form of the orientation.
            Rotation = Character.transform.rotation.eulerAngles.y + RandomBinomial() * MaxRotationSpeed // Change our orientation randomly.
        };

        return(output);
    }
Beispiel #17
0
 public KinematicSteeringOutput getSteering(Kinematic self)
 {
     if (this.target == null)
     {
         //if there is no target, set it to yourself
         this.target = self;
     }
     KSO = AICore.getSteering(self, this.target);
     return(this.KSO);
 }
Beispiel #18
0
    public float maxRotation = 10;              // Holds the maximum rotation that the character can have

    void FixedUpdate()
    {
        // Creat a variable for the steering of the character
        KinematicSteeringOutput character = character = GetSteering();

        // Update position of character
        transform.position = transform.position + character.vel * Time.fixedDeltaTime;

        // Update orientation of character
        transform.eulerAngles = new Vector3(0, 0, character.rot);
    }
    public KinematicSteeringOutput GetSteering()
    {
        KinematicSteeringOutput steering = new KinematicSteeringOutput();

        steering.velocity = maxSpeed * character.orientation.vector;

        // change orientation randomly
        steering.rotation = Angle.WithDeg(RandomExtensions.GetBinomialValue() * maxRotationDegrees);

        return steering;
    }
    public void updateK(KinematicSteeringOutput steering)
    {
        // Update the position and orientation.
        transform.position += steering.velocity * Time.deltaTime;
        vz.z = transform.rotation.eulerAngles.z + steering.rotation;

        // set object rotation
        transform.rotation = Quaternion.Euler(vz);

        velocity = steering.velocity;
    }
 public virtual float getNewOrientation(KinematicSteeringOutput steering)
 {
     if (steering.Velocity.magnitude > 0f)
     {
         return((float)Mathf.Atan2(steering.Velocity.x, steering.Velocity.z));
     }
     else
     {
         return(this.character.Orientation);
     }
 }
    //creates object with velocity and orientation, then canlculates and returns new orientation and velocity
    KinematicSteeringOutput getSteering()
    {
        KinematicSteeringOutput result = new KinematicSteeringOutput();


        result.velocityTrans = maxSpeed * new Vector3(Mathf.Cos(enemy.eulerAngles.y), 0, Mathf.Sin(enemy.eulerAngles.y));

        result.velocityAng = new Vector3(0, randomBinomial() * maxRotation, 0);

        return(result);
    }
Beispiel #23
0
    public KinematicSteeringOutput GetSteering()
    {
        KinematicSteeringOutput result = new KinematicSteeringOutput();

        float angle;               // Angle to determine the transform's orientation
        float setSpeed = maxSpeed; // To change velocity as approaching target

        // Get the direction to (or away from) the target
        if (seekOrFlee == false)
        {
            result.velocity = target.position - transform.position;
        }
        else
        {
            result.velocity = transform.position - target.position;
        }


        // Check if target is in search radius
        if (result.velocity.magnitude > searchRadius)
        {
            return(null);
        }

        // Check if we're close enough to slow down
        if (result.velocity.magnitude < 10)
        {
            setSpeed /= 10;
        }
        else if (result.velocity.magnitude < 5)
        {
            setSpeed /= 5;
        }
        else
        {
            setSpeed = maxSpeed; // otherwise remain at maxSpeed
        }

        // Beat the clock!
        result.velocity /= timeToTarget;

        // Velocity is along this direction, at full speed
        result.velocity.Normalize();
        result.velocity *= setSpeed;

        // Get angle to set character to
        angle  = NewOrientation(transform.eulerAngles.y, result.velocity);
        angle *= Mathf.Rad2Deg;

        result.rotation = new Vector3(0, angle, 0);

        return(result);
    }
Beispiel #24
0
    public float timeToTarget = 0.25f; // time to target constant

    void Update()
    {
        // Get new velocity and rotation based on target position
        KinematicSteeringOutput newSteering = GetSteering();

        // Apply steering to velocity and rotation
        if (newSteering != null)
        {
            transform.position   += newSteering.velocity * Time.deltaTime;
            transform.eulerAngles = newSteering.rotation;
        }
    }
        public void TestZeroSteer()
        {
            KinematicSteeringOutput ks = new KinematicSteeringOutput();
            ks.velocity = Vector3.zero;
            ks.rotation = Angle.WithDeg(0);

            Transform t = new GameObject().transform;
            ApplySteer(ks, t);

            Assert.True(t.position == Vector3.zero);
            Assert.True(t.eulerAngles == Vector3.zero);
        }
Beispiel #26
0
    KinematicSteeringOutput GetSteering()
    {
        KinematicSteeringOutput result = new KinematicSteeringOutput();

        // Get velocity from vector form of orientation
        result.velocity = maxSpeed * AsVector(character.transform.rotation.eulerAngles.z);

        // Change orientation randomly
        result.rotation = randomBinomial() * maxRotation;

        //result.rotation = 0;
        return(result);
    }
    //funcion que realiza seeking de un punto y se detiene si lo alcanza en un radio
    //dado
    public KinematicSteeringOutput getSteering()
    {
        //velocidades de salida
        KinematicSteeringOutput steering = new KinematicSteeringOutput();

        //sacamos la nueva velocidad en base a la orientacion
        steering.velocity = maxspeed * character.OrientationToVector();

        //actualizamos la rotacion al azar
        steering.rotation = (UnityEngine.Random.Range(-1.0f, 1.0f)) * maxrotation;

        return(steering);
    }
    public KinematicSteeringOutput GetSteering()
    {
        KinematicSteeringOutput steering = new KinematicSteeringOutput();

        // determine new velocity
        steering.velocity = target.position - character.position;
        steering.velocity.Normalize();
        steering.velocity *= maxSpeed;

        steering.rotation = Angle.WithDeg(0);

        return steering;
    }
Beispiel #29
0
    // Calculate steering for a seeking AI
    KinematicSteeringOutput GetSteering()
    {
        // Create structure for output
        KinematicSteeringOutput steering = new KinematicSteeringOutput();

        // Move in the direction of orientation
        steering.vel = new Vector3(-Mathf.Sin(transform.eulerAngles.z * ((Mathf.PI) / 180)) * maxSpeed, Mathf.Cos(transform.eulerAngles.z * ((Mathf.PI) / 180)) * maxSpeed, 0);

        // Get random orientation
        steering.rot = steering.rot + Random.Range(-1.0f, 1.0f) * maxRotation;

        return(steering);
    }
    //creates object with velocity and orientation, then canlculates and returns new orientation and velocity
    KinematicSteeringOutput getSteering()
    {
        KinematicSteeringOutput result = new KinematicSteeringOutput();

        result.velocityTrans = maxSpeed * enemy.eulerAngles;
        Debug.Log(result.velocityTrans);
        //float angleDeg = randomBinomial() * maxRotation;
        //Debug.Log(randomBinomial());
        //angleDeg *= Mathf.Rad2Deg; //turn rad to deg
        result.velocityAng = new Vector3(0, randomBinomial() * maxRotation, 0);
        //Debug.Log(result.velocityAng);
        //Debug.Log(result.velocityAng);

        return(result);
    }
    //update loop to update position and orientation from other functions
    void FixedUpdate()
    {
        if (i % 1000 == 0)
        {
            do
            {
                list   = GameObject.FindGameObjectsWithTag("Enemy");
                player = list[Random.Range(0, list.Length)].transform;
            } while (player.name == this.name);
        }
        KinematicSteeringOutput blackMagic = getSteering();

        enemy.transform.position   += blackMagic.velocityTrans * Time.deltaTime; //updates translational pos
        enemy.transform.eulerAngles = blackMagic.velocityAng;                    //updates rotational pos
    }
    public KinematicSteeringOutput Arrive()
    {
        KinematicSteeringOutput steering = new KinematicSteeringOutput();

        Vector3 direction = target - character.GetComponent <Character>().staticInfo.position;

        float distance = direction.magnitude;

        float targetSpeed;

        if (distance < targetRadius)
        {
            steering.velocity = new Vector3(0, 0, 0);
            return(steering);
        }

        if (distance > slowRadius)
        {
            targetSpeed = maxSpeed;
        }

        else
        {
            targetSpeed = maxSpeed * distance / slowRadius;
        }

        Vector3 targetVelocity = direction;

        targetVelocity.Normalize();

        targetVelocity *= targetSpeed;


        character.GetComponent <Character>().steering.linear  = targetVelocity - character.GetComponent <Character>().staticInfo.velocity;
        character.GetComponent <Character>().steering.linear /= timeToTarget;

        if (character.GetComponent <Character>().steering.linear.magnitude > maxAcceleration)
        {
            character.GetComponent <Character>().steering.linear.Normalize();
            character.GetComponent <Character>().steering.linear *= maxAcceleration;
        }

        character.GetComponent <Character>().steering.angular = 0;
        steering.velocity = targetVelocity;


        return(steering);
    }
Beispiel #33
0
    // Update is called once per frame
    void Update()
    {
        // Decide on behavior
        ds_force  = arrive.getSteering();
        ds_torque = align.getSteering();

        ds        = new DynoSteering();
        ds.force  = ds_force.force;
        ds.torque = ds_torque.torque;

        // 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);
        transform.rotation = Quaternion.Euler(0f, kso.orientation * Mathf.Rad2Deg, 0f);
    }
Beispiel #34
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);
    }
 protected static void FaceMovementDirection(KinematicSteeringOutput ksOut, Transform trans)
 {
     Angle currentOrientation = Angle.WithEulerAngles(trans.eulerAngles);
     Angle newOrientation = SteeringStructFunctions.GetNewOrientation(currentOrientation, ksOut.velocity);
     trans.eulerAngles = newOrientation.eulerAngles;
 }
    protected static void ApplySteer(KinematicSteeringOutput ksOut, Transform trans)
    {
        trans.Translate(ksOut.velocity * Time.deltaTime, Space.World);

        trans.Rotate( ksOut.rotation.eulerAngles * Time.deltaTime, Space.World);
    }