Exemple #1
0
        // Compute the steering contribution of this SteeringBehaviour
        public override Vector2 ComputeSteering(SteeringMovement movement)
        {
            Vector2 targetPosition = seekTarget.transform.position;
            Vector2 myPosition     = movement.transform.position;

            Vector2 desiredDirection = (targetPosition - myPosition).normalized;

            desiredDirection = -desiredDirection; // we want to get away

            float desiredSpeed = 0;
            float distance     = (targetPosition - myPosition).magnitude;

            if (distance >= distanceThreshold)
            {
                desiredSpeed = 0;
            }
            else
            {
                desiredSpeed = 1 - distance / distanceThreshold;
            }
            desiredSpeed *= movement.maxSpeed;

            Vector2 desiredVelocity = desiredDirection * desiredSpeed;

            Vector2 desiredAcceleration = desiredVelocity - movement.state.currentVelocity;

            desiredAcceleration = Vector2.ClampMagnitude(desiredAcceleration, movement.maxAcceleration);

            return(desiredAcceleration);
        }
Exemple #2
0
        void Start()
        {
            movement = GetComponent <SteeringMovement>();

            // Setup steering (decision making)
            weightedSteeringBehaviourList = new List <WeightedSteeringBehaviour>();

            {
                var sb = new SeekSteeringBehaviour();
                sb.seekTarget = FindObjectOfType <Player>().transform;
                WeightedSteeringBehaviour wsb = new WeightedSteeringBehaviour();
                wsb.steeringBehaviour = sb;
                wsb.weight            = 2;
                weightedSteeringBehaviourList.Add(wsb);
                seekWSb = wsb;
            }

            {
                var sb = new AvoidSteeringBehaviour();
                sb.seekTarget = FindObjectOfType <Player>().transform;
                WeightedSteeringBehaviour wsb = new WeightedSteeringBehaviour();
                wsb.steeringBehaviour = sb;
                wsb.weight            = 1;
                weightedSteeringBehaviourList.Add(wsb);
            }

            {
                var sb = new SeparationSteeringBehaviour();
                WeightedSteeringBehaviour wsb = new WeightedSteeringBehaviour();
                wsb.steeringBehaviour = sb;
                wsb.weight            = 20;
                weightedSteeringBehaviourList.Add(wsb);
            }

            {
                var sb = new ChoesionSteeringBehaviour();
                WeightedSteeringBehaviour wsb = new WeightedSteeringBehaviour();
                wsb.steeringBehaviour = sb;
                wsb.weight            = 1;
                weightedSteeringBehaviourList.Add(wsb);
            }

            {
                var sb = new AlignmentSteeringBehaviour();
                WeightedSteeringBehaviour wsb = new WeightedSteeringBehaviour();
                wsb.steeringBehaviour = sb;
                wsb.weight            = 50;
                weightedSteeringBehaviourList.Add(wsb);
            }
        }
Exemple #3
0
        // Compute the steering contribution of this SteeringBehaviour
        public override Vector2 ComputeSteering(SteeringMovement movement)
        {
            Vector2 targetPosition = seekTarget.transform.position;
            Vector2 myPosition     = movement.transform.position;

            Vector2 desiredDirection = (targetPosition - myPosition).normalized;
            Vector2 desiredVelocity  = desiredDirection * movement.maxSpeed;

            Vector2 desiredAcceleration = desiredVelocity - movement.state.currentVelocity;

            desiredAcceleration = Vector2.ClampMagnitude(desiredAcceleration, movement.maxAcceleration);

            return(desiredAcceleration);
        }
Exemple #4
0
        public override Vector2 ComputeSteering(SteeringMovement movement)
        {
            var allBots = GameObject.FindObjectsOfType <AIBot_Steering>();

            Vector2 myPosition = movement.transform.position;

            // Compute an average velocity from all bots
            Vector2 avarageVelocity   = Vector2.zero;
            int     insideCircleCount = 0;

            foreach (var bot in allBots)
            {
                Vector2 targetPosition = bot.transform.position;


                Vector2 toTargetDir = (targetPosition - myPosition).normalized;
                Vector2 currentDir  = movement.state.currentVelocity.normalized;

                if (Mathf.Abs(Vector2.Angle(toTargetDir, currentDir)) > 30)
                {
                    continue;
                }


                float botDistance = (targetPosition - myPosition).magnitude;
                if (botDistance <= distanceThreshold && botDistance > 0)
                {
                    avarageVelocity   += bot.movement.state.currentVelocity;
                    insideCircleCount += 1;
                }
            }
            if (insideCircleCount == 0)
            {
                return(Vector2.zero);
            }
            avarageVelocity /= insideCircleCount;

            Vector2 desiredVelocity = avarageVelocity;

            Vector2 desiredAcceleration = desiredVelocity - movement.state.currentVelocity;

            desiredAcceleration = Vector2.ClampMagnitude(desiredAcceleration, movement.maxAcceleration);

            // Debug.DrawLine(movement.transform.position, movement.transform.position + (Vector3)desiredVelocity, Color.cyan);

            return(desiredAcceleration);
        }
Exemple #5
0
        public override Vector2 ComputeSteering(SteeringMovement movement)
        {
            var allBots = GameObject.FindObjectsOfType <AIBot_Steering>();

            // Compute a total desired velocity based on the positions of other bots
            Vector2 totalDesiredVelocity = Vector2.zero;

            foreach (var bot in allBots)
            {
                Vector2 targetPosition   = bot.transform.position;
                Vector2 myPosition       = movement.transform.position;
                Vector2 desiredDirection = (targetPosition - myPosition).normalized;
                desiredDirection = -desiredDirection; // we want to get away
                float desiredSpeed = 0;
                float distance     = (targetPosition - myPosition).magnitude;
                if (distance >= distanceThreshold)
                {
                    desiredSpeed = 0;
                }
                else
                {
                    desiredSpeed = 1 - distance / distanceThreshold;
                }

                Vector2 desiredVelocity = desiredDirection * desiredSpeed;
                totalDesiredVelocity += desiredVelocity;
            }
            if (allBots.Length <= 1)
            {
                return(Vector2.zero);
            }
            totalDesiredVelocity /= (allBots.Length - 1);   // do not count myself

            totalDesiredVelocity *= movement.maxSpeed;

            Vector2 desiredAcceleration = totalDesiredVelocity - movement.state.currentVelocity;

            desiredAcceleration = Vector2.ClampMagnitude(desiredAcceleration, movement.maxAcceleration);

            return(desiredAcceleration);
        }
Exemple #6
0
        public override Vector2 ComputeSteering(SteeringMovement movement)
        {
            var allBots = GameObject.FindObjectsOfType <AIBot_Steering>();

            Vector2 myPosition = movement.transform.position;

            // Compute an average position based on the positions of other bots
            Vector2 averagePosition   = Vector2.zero;
            int     insideCircleCount = 0;

            foreach (var bot in allBots)
            {
                Vector2 targetPosition = bot.transform.position;


                Vector2 toTargetDir = (targetPosition - myPosition).normalized;
                Vector2 currentDir  = movement.state.currentVelocity.normalized;

                if (Mathf.Abs(Vector2.Angle(toTargetDir, currentDir)) > 30)
                {
                    continue;
                }

                float botDistance = (targetPosition - myPosition).magnitude;
                if (botDistance <= distanceThreshold && botDistance > 0)
                {
                    averagePosition   += targetPosition;
                    insideCircleCount += 1;
                }
            }
            if (insideCircleCount == 0)
            {
                return(Vector2.zero);
            }
            averagePosition /= insideCircleCount;

            Vector2 desiredDirection = (averagePosition - myPosition).normalized;
            //desiredDirection = -desiredDirection; // we want to get away
            float desiredSpeed = 0;
            float distance     = (averagePosition - myPosition).magnitude;

            if (distance >= distanceThreshold)
            {
                desiredSpeed = 0;
            }
            else
            {
                desiredSpeed = distance / distanceThreshold;
            }

            Vector2 desiredVelocity = desiredDirection * desiredSpeed;

            desiredVelocity *= movement.maxSpeed;

            Vector2 desiredAcceleration = desiredVelocity - movement.state.currentVelocity;

            desiredAcceleration = Vector2.ClampMagnitude(desiredAcceleration, movement.maxAcceleration);

            //Debug.DrawLine(movement.transform.position, averagePosition, Color.black);

            return(desiredAcceleration);
        }
Exemple #7
0
 public abstract Vector2 ComputeSteering(SteeringMovement movement);