示例#1
0
        //=========================================================//


        //calculate force and update position
        private void UpdatePosition(ref Vector2 targetPosition)
        {
            heading = Vector2.Normalize(velocity);
            switch (SB)
            {
            case SB.None:
                steerForce = Vector2.Zero;
                break;

            case SB.Seek:
                steerForce = SteeringBehaviours.Seek(ref targetPosition, ref currentPosition, ref velocity, max_speed);
                break;

            case SB.Flee:
                steerForce = SteeringBehaviours.Flee(ref targetPosition, ref currentPosition, ref velocity, max_speed, FOV, vehicleNo);
                break;

            case SB.Arrive:
                steerForce = SteeringBehaviours.Arrive(ref targetPosition, ref currentPosition, ref velocity, ArriveRadius, max_speed, vehicleNo);
                break;

            case SB.Pursuit:
                break;

            case SB.Evade:
                break;

            case SB.Wander:
                steerForce = SteeringBehaviours.Wander(ref wanderTarget, ref currentPosition, ref velocity, ref heading, WanderRadius, WanderDistance, WanderJitter);
                break;

            case SB.PathFollowing:
                if (!enableSpecialPath)
                {
                    steerForce = SteeringBehaviours.PathFollowing(ref targetPosition, ref currentPosition, ref velocity, ref pathPoints, ref currentPathPoint, maxPathPoints, max_speed);
                }
                else
                {
                    steerForce = SteeringBehaviours.PathFollowing(ref targetPosition, ref currentPosition, ref velocity, ref specialPathPoints, ref currentPathPoint, maxSpecialPathPoints, max_speed);
                }
                break;

            case SB.Cohesion:
                steerForce = SteeringBehaviours.Cohesion(ref allCars, this, currentPosition, velocity, max_speed, CohesionRadius);
                break;

            case SB.Alignment:
                steerForce = SteeringBehaviours.Alignment(ref allCars, this, ref currentPosition, ref velocity, max_speed);
                break;

            case SB.Separation:
                steerForce = SteeringBehaviours.Separation(ref allCars, this, ref currentPosition, ref velocity, max_speed);
                break;

            case SB.CF:
                steerForce = Vector2.Add(SteeringBehaviours.Cohesion(ref allCars, this, currentPosition, velocity, max_speed, CohesionRadius), SteeringBehaviours.Flee(ref targetPosition, ref currentPosition, ref velocity, max_speed, FOV, vehicleNo));
                break;

            case SB.CA:
                if (weightedSum)
                {
                    steerForce += Vector2.Multiply(SteeringBehaviours.Cohesion(ref allCars, this, currentPosition, velocity, max_speed, CohesionRadius), .2f);
                    steerForce += Vector2.Multiply(SteeringBehaviours.Alignment(ref allCars, this, ref currentPosition, ref velocity, max_speed), .1f);
                }
                else
                {
                    steerForce = Vector2.Add(SteeringBehaviours.Cohesion(ref allCars, this, currentPosition, velocity, max_speed, CohesionRadius), SteeringBehaviours.Alignment(ref allCars, this, ref currentPosition, ref velocity, max_speed));
                }
                break;

            case SB.CAS:
                if (weightedSum)
                {
                    steerForce = Vector2.Add(SteeringBehaviours.Separation(ref allCars, this, ref currentPosition, ref velocity, max_speed), Vector2.Add(SteeringBehaviours.Cohesion(ref allCars, this, currentPosition, velocity, max_speed, CohesionRadius), SteeringBehaviours.Alignment(ref allCars, this, ref currentPosition, ref velocity, max_speed)));
                }
                else
                {
                    steerForce  = Vector2.Multiply(SteeringBehaviours.Separation(ref allCars, this, ref currentPosition, ref velocity, max_speed), .3f);
                    steerForce += Vector2.Multiply(SteeringBehaviours.Cohesion(ref allCars, this, currentPosition, velocity, max_speed, CohesionRadius), .2f);
                    steerForce += Vector2.Multiply(SteeringBehaviours.Alignment(ref allCars, this, ref currentPosition, ref velocity, max_speed), .5f);
                }

                break;

            case SB.CS:
                if (weightedSum)
                {
                    steerForce = Vector2.Add(SteeringBehaviours.Cohesion(ref allCars, this, currentPosition, velocity, max_speed, CohesionRadius), SteeringBehaviours.Separation(ref allCars, this, ref currentPosition, ref velocity, max_speed));
                }
                else
                {
                    steerForce  = Vector2.Multiply(SteeringBehaviours.Separation(ref allCars, this, ref currentPosition, ref velocity, max_speed), .8f);
                    steerForce += Vector2.Multiply(SteeringBehaviours.Cohesion(ref allCars, this, currentPosition, velocity, max_speed, CohesionRadius), .2f);
                }
                break;

            case SB.FCAS:
                if (weightedSum)
                {
                    steerForce = Vector2.Add(SteeringBehaviours.Alignment(ref allCars, this, ref currentPosition, ref velocity, max_speed), Vector2.Add(SteeringBehaviours.Separation(ref allCars, this, ref currentPosition, ref velocity, max_speed), Vector2.Add(SteeringBehaviours.Cohesion(ref allCars, this, currentPosition, velocity, max_speed, CohesionRadius), SteeringBehaviours.Flee(ref targetPosition, ref currentPosition, ref velocity, max_speed, FOV, vehicleNo))));
                }
                else
                {
                    steerForce  = Vector2.Multiply(SteeringBehaviours.Flee(ref targetPosition, ref currentPosition, ref velocity, max_speed, FOV, vehicleNo), .4f);
                    steerForce += Vector2.Multiply(SteeringBehaviours.Separation(ref allCars, this, ref currentPosition, ref velocity, max_speed), .3f);
                    steerForce += Vector2.Multiply(SteeringBehaviours.Cohesion(ref allCars, this, currentPosition, velocity, max_speed, CohesionRadius), .2f);
                    steerForce += Vector2.Multiply(SteeringBehaviours.Alignment(ref allCars, this, ref currentPosition, ref velocity, max_speed), .5f);
                }
                break;

            case SB.FCS:
                steerForce = Vector2.Add(SteeringBehaviours.Separation(ref allCars, this, ref currentPosition, ref velocity, max_speed), Vector2.Add(SteeringBehaviours.Cohesion(ref allCars, this, currentPosition, velocity, max_speed, CohesionRadius), SteeringBehaviours.Flee(ref targetPosition, ref currentPosition, ref velocity, max_speed, FOV, vehicleNo)));
                break;

            default:
                break;
            }
            steerForce      = VectorHelpers.Truncate(steerForce, max_force);
            acceleration    = steerForce / mass;
            velocity        = VectorHelpers.Truncate(velocity + acceleration, max_speed);
            currentPosition = Vector2.Add(velocity, currentPosition);

            var mirrored = true;

            if (!mirrored)
            {
                if (currentPosition.X > clientSize.Width)
                {
                    currentPosition.X = 0;
                }
                if (currentPosition.Y > clientSize.Height)
                {
                    currentPosition.Y = 0;
                }
                if (currentPosition.X < 0)
                {
                    currentPosition.X = clientSize.Width;
                }
                if (currentPosition.Y < 0)
                {
                    currentPosition.Y = clientSize.Height;
                }
            }
            else
            {
                if (currentPosition.X > clientSize.Width)
                {
                    velocity.X *= -1;
                }
                if (currentPosition.Y > clientSize.Height)
                {
                    velocity.Y *= -1;
                }
                if (currentPosition.X < 0)
                {
                    velocity.X *= -1;
                }
                ;
                if (currentPosition.Y < 0)
                {
                    velocity.Y *= -1;
                }
                ;
            }
            currentPosition = new Vector2(MathHelper.Clamp(currentPosition.X, 0, clientSize.Width),
                                          MathHelper.Clamp(currentPosition.Y, 0, clientSize.Height));
            targetChanged = false;
        }