Пример #1
0
        override public SteeringOutput GetSteering()
        {
            float     shortestTime          = Mathf.Infinity;
            Kinematic firstTarget           = null;
            float     firstMinSeparation    = 0;
            float     firstDistance         = 0;
            Vector3   firstRelativePosition = Vector3.zero;
            Vector3   firstRelativeVelocity = Vector3.zero;

            foreach (Kinematic t in targets)
            {
                Vector3 relativePosition = character.position - t.position;
                Vector3 relativeVelocity = t.velocity - character.velocity;
                float   relativeSpeed    = relativeVelocity.magnitude;
                float   timeToCollision  = Vector3.Dot(relativePosition, relativeVelocity) / (relativeSpeed * relativeSpeed);

                float distance      = relativePosition.magnitude;
                float minSeparation = distance - relativeSpeed * timeToCollision;
                if (minSeparation > 2 * radius)
                {
                    continue;
                }

                if (timeToCollision > 0 && timeToCollision < shortestTime)
                {
                    shortestTime          = timeToCollision;
                    firstTarget           = t;
                    firstMinSeparation    = minSeparation;
                    firstDistance         = distance;
                    firstRelativePosition = relativePosition;
                    firstRelativeVelocity = relativeVelocity;
                }
            }

            if (firstTarget == null)
            {
                return(null);
            }

            Vector3 position;

            if (firstMinSeparation <= 0 || firstDistance < 2 * radius)
            {
                position = character.position - firstTarget.position;
            }
            else
            {
                position = firstRelativePosition + firstRelativeVelocity * shortestTime;
            }

            SteeringOutput output = new SteeringOutput();

            output.linear  = position.normalized * maxAcceleration;
            output.angular = 0;
            return(output);
        }
Пример #2
0
        public static Kinematic RigidbodyToKinematic(Rigidbody rigidbody)
        {
            Kinematic kinematic = new Kinematic();

            kinematic.position    = rigidbody.position;
            kinematic.orientation = rigidbody.rotation.eulerAngles.y;
            kinematic.velocity    = rigidbody.velocity;
            kinematic.rotation    = rigidbody.angularVelocity.y;
            return(kinematic);
        }
Пример #3
0
 public virtual void Initialize(Kinematic target, Kinematic character)
 {
     this.target    = target;
     this.character = character;
 }