Exemplo n.º 1
0
    //early initialization
    void Awake()
    {
        // Creating a new Dynamic Character
        this.character = new DynamicCharacter(this.gameObject);

        // Making sure we know all the possible destinations
        destinations = new List <Transform>();
        var goals = GameObject.Find("DestinationPoints").transform;

        for (int i = 0; i < goals.childCount; i++)
        {
            destinations.Add(goals.transform.GetChild(i));
        }

        // Initializing the Priority and Blended movements
        this.priorityMovement = new PriorityMovement
        {
            Character = this.character.KinematicData
        };

        this.blendedMovement = new BlendedMovement
        {
            Character = this.character.KinematicData
        };
    }
    //early initialization
    void Awake()
    {
        this.character = new DynamicCharacter(this.gameObject);

        this.blendedMovement = new BlendedMovement
        {
            Character = this.character.KinematicData
        };
    }
Exemplo n.º 3
0
    //early initialization
    void Awake()
    {
        this.character        = new DynamicCharacter(this.gameObject);
        this.movementTextText = this.movementText.GetComponent <Text>();

        this.priorityMovement = new PriorityMovement
        {
            Character = this.character.KinematicData
        };

        this.blendedMovement = new BlendedMovement
        {
            Character = this.character.KinematicData
        };
    }
    //early initialization
    void Awake()
    {
        this.character = new DynamicCharacter(this.gameObject)
        {
            MaxSpeed = MAX_SPEED,
            Drag     = DRAG
        };


        this.priorityMovement = new PriorityMovement
        {
            Character = this.character.KinematicData
        };

        this.blendedMovement = new BlendedMovement
        {
            Character = this.character.KinematicData
        };
    }
Exemplo n.º 5
0
    private void InitializeCharacter(DynamicCharacter character)
    {
        var blended = new BlendedMovement
        {
            Character          = character.KinematicData,
            MovementDebugColor = Color.black
        };


        var priority = new PriorityMovement
        {
            Character = character.KinematicData
        };

        // VELOCITY MATCHING
        var flockVelocityMatching = new FlockVelocityMatching()
        {
            Character          = character.KinematicData,
            Flock              = this.getCharactersKinematicData(this.Characters),
            MaxAcceleration    = MAX_ACCELERATION,
            MovementDebugColor = Color.green
        };

        // SEPARATION
        var flockSeparation = new FlockSeparation()
        {
            Character          = character.KinematicData,
            Flock              = this.getCharactersKinematicData(this.Characters),
            MaxAcceleration    = MAX_ACCELERATION,
            MovementDebugColor = Color.grey
        };

        // COHESION
        var flockCohesion = new FlockCohesion()
        {
            Character          = character.KinematicData,
            Flock              = this.getCharactersKinematicData(this.Characters),
            MaxAcceleration    = MAX_ACCELERATION,
            MovementDebugColor = Color.cyan
        };

        // OBSTACLE AVOIDANCE
        var obstacleAvoidMovement = new DynamicObstacleAvoid()
        {
            MaxAcceleration    = MAX_ACCELERATION / 2,
            Character          = character.KinematicData,
            MovementDebugColor = Color.magenta
        };

        // FLEE
        var flockFlee = new FlockFlee()
        {
            Character          = character.KinematicData,
            Flock              = this.getCharactersKinematicData(this.Characters),
            Target             = new KinematicData(),
            MaxAcceleration    = MAX_ACCELERATION * 3.0f,
            MovementDebugColor = Color.green
        };

        //TODO: ADD THE 3 MOVEMENTS
        blended.Movements.Add(new MovementWithWeight(flockSeparation, 1.5f));
        blended.Movements.Add(new MovementWithWeight(flockCohesion, 1.7f));
        blended.Movements.Add(new MovementWithWeight(flockVelocityMatching, 2.0f));
        blended.Movements.Add(new MovementWithWeight(flockFlee, 2.0f));

        priority.Movements.Add(obstacleAvoidMovement);
        priority.Movements.Add(blended);


        character.Movement = priority;
    }
    private List <DynamicMovement> InitBoidMovements(List <DynamicCharacter> characters, IEnumerable <GameObject> obstacles)
    {
        List <DynamicMovement> avoidObstacleMovement = new List <DynamicMovement>();

        foreach (var obstacle in obstacles)
        {
            avoidObstacleMovement.Add(new DynamicAvoidObstacle(obstacle)
            {
                MaxAcceleration    = MAX_ACCELERATION,
                AvoidMargin        = AVOID_MARGIN,
                MaxLookAhead       = MAX_LOOK_AHEAD,
                MovementDebugColor = Color.magenta
            });
        }

        var cohesion = new DynamicCohesion()
        {
            Flock              = this.Flock,
            MaxSpeed           = MAX_SPEED,
            MaxAcceleration    = MAX_ACCELERATION,
            StopRadius         = 0.0F,
            SlowRadius         = 6.0F,
            FlockRadius        = COHESION_RADIUS,
            FanAngle           = FAN_ANGLE,
            MovementDebugColor = Color.red
        };

        var separation = new DynamicSeparation()
        {
            Flock              = this.Flock,
            FlockRadius        = SEPARATION_RADIUS,
            MaxAcceleration    = MAX_ACCELERATION,
            SeparationFactor   = SEPARATION_FACTOR,
            MovementDebugColor = Color.blue
        };

        var matchVelocity = new DynamicFlockVelocityMatch()
        {
            Flock              = this.Flock,
            MaxAcceleration    = MAX_ACCELERATION,
            FlockRadius        = COHESION_RADIUS,
            FanAngle           = FAN_ANGLE,
            MovementDebugColor = Color.green
        };

        var followMouse = new DynamicFollowMouse()
        {
            MaxSpeed           = MAX_SPEED,
            MaxAcceleration    = MAX_ACCELERATION,
            StopRadius         = STOP_RADIUS,
            SlowRadius         = SLOW_RADIUS,
            Arrived            = new HashSet <KinematicData>(),
            MovementDebugColor = Color.gray
        };

        FollowMouseMovement = new MovementWithWeight(followMouse, 0);

        var blended = new BlendedMovement();

        blended.Movements.Add(new MovementWithWeight(cohesion, COHESION_WEIGHT));
        blended.Movements.Add(new MovementWithWeight(separation, SEPARATION_WEIGHT));
        blended.Movements.Add(new MovementWithWeight(matchVelocity, VELOCITY_MATCH_WEIGHT));
        blended.Movements.Add(FollowMouseMovement);

        var allMovements = new List <DynamicMovement>();

        allMovements.AddRange(avoidObstacleMovement);
        allMovements.Add(blended);

        return(allMovements);
    }
Exemplo n.º 7
0
    private void InitializeCharacters(DynamicCharacter character, GameObject[] obstacles)
    {
        var Blended = new BlendedMovement
        {
            Character = character.KinematicData
        };

        foreach (var obstacle in obstacles)
        {
            var avoidObstacleMovement = new DynamicAvoidObstacle(obstacle)
            {
                MaxAcceleration    = MAX_ACCELERATION,
                AvoidMargin        = AVOID_MARGIN,
                MaxLookAhead       = MAX_LOOK_AHEAD,
                Character          = character.KinematicData,
                WhiskersLength     = MAX_WHISKERS_LOOK_AHEAD,
                WhiskersSpan       = MAX_WHISKERS_SPAN,
                MovementDebugColor = Color.magenta
            };
            Blended.Movements.Add(new MovementWithWeight(avoidObstacleMovement, 7));
        }

        var separation = new DynamicSeparation()
        {
            Character        = character.KinematicData,
            flock            = this.flock,
            maxAcceleration  = MAX_ACCELERATION,
            radius           = RADIUS,
            separationFactor = SEPARATION_FACTOR
        };

        var flockVelocityMatching = new DynamicFlockVelocityMatching()
        {
            Character       = character.KinematicData,
            flock           = this.flock,
            radius          = RADIUS,
            fanAngle        = FAN_ANGLE,
            MaxAcceleration = MAX_ACCELERATION
        };

        var cohesion = new DynamicCohesion()
        {
            Character       = character.KinematicData,
            flock           = this.flock,
            MaxAcceleration = MAX_ACCELERATION,
            radius          = RADIUS,
            fanAngle        = FAN_ANGLE
        };

        var straightAhead = new DynamicStraightAhead
        {
            MaxAcceleration    = MAX_ACCELERATION,
            Character          = character.KinematicData,
            MovementDebugColor = Color.yellow
        };

        Blended.Movements.Add(new MovementWithWeight(straightAhead, 3));
        Blended.Movements.Add(new MovementWithWeight(separation, 4));
        Blended.Movements.Add(new MovementWithWeight(flockVelocityMatching, 3));
        Blended.Movements.Add(new MovementWithWeight(cohesion, 3));

        character.Movement = Blended;
    }
Exemplo n.º 8
0
    void Update()
    {
        Camera  camera        = GameObject.FindGameObjectWithTag("MainCamera").GetComponent <Camera>();
        Vector3 PointInWorld  = new Vector3();
        var     buttonClicked = false;

        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            var mousePos = Input.mousePosition;
            mousePos.z    = CAMERA_Y;
            PointInWorld  = camera.ScreenToWorldPoint(mousePos);
            buttonClicked = true;
            Debug.Log("world point: " + PointInWorld);
        }

        foreach (var character in this.Characters)
        {
            BlendedMovement    movement  = (BlendedMovement)character.Movement;
            MovementWithWeight fleeClick = movement.Movements.Find(x => x.Movement.Name == "Flee");

            if (buttonClicked)
            {
                var distanceToPoint = (character.KinematicData.position - PointInWorld).magnitude;

                if (distanceToPoint < FLEE_RADIUS)
                {
                    var dynamicFlee = new DynamicFlee()
                    {
                        Character       = character.KinematicData,
                        Target          = new KinematicData(),
                        MaxAcceleration = MAX_ACCELERATION
                    };

                    if (fleeClick != null)
                    {
                        movement.Movements.Remove(fleeClick);
                    }

                    dynamicFlee.Target.position   = PointInWorld;
                    dynamicFlee.Target.position.y = character.KinematicData.position.y;

                    movement.Movements.Add(new MovementWithWeight(dynamicFlee, 7));
                }
            }
            else
            {
                var distanceToPoint = (character.KinematicData.position - PointInWorld).magnitude;

                if (distanceToPoint > FLEE_RADIUS)
                {
                    if (fleeClick != null)
                    {
                        movement.Movements.Remove(fleeClick);
                    }
                }
            }


            this.UpdateMovingGameObject(character);
        }
    }
Exemplo n.º 9
0
        private BlendedMovement GenerateBlendingMovementFor(DynamicCharacter character)
        {
            var blending = new BlendedMovement
            {
                MaxAcceleration = this.MaximumAcceleration,
                Character       = character.KinematicData
            };

            var cohesion = new DynamicCohesion(this)
            {
                Character          = character.KinematicData,
                MaxAcceleration    = this.MaximumAcceleration,
                MaxSpeed           = this.MaximumSpeed,
                FlockRadius        = this.FlockRadius,
                MovementDebugColor = Color.yellow
            };

            var separation = new DynamicSeparation(this)
            {
                Character          = character.KinematicData,
                MaxAcceleration    = this.MaximumAcceleration,
                FlockRadius        = this.FlockRadius,
                SeparationFactor   = this.SeparationFactor,
                MovementDebugColor = Color.red
            };

            var velocityMatch = new DynamicFlockVelocityMatching(this)
            {
                Character          = character.KinematicData,
                MaxAcceleration    = this.MaximumAcceleration,
                FlockRadius        = this.FlockRadius,
                MovementDebugColor = Color.green
            };

            var collisionDetection = new PriorityMovement
            {
                Character          = character.KinematicData,
                MovementDebugColor = Color.magenta
            };

            foreach (var obstacle in this.Obstacles)
            {
                var avoidMovement = new DynamicAvoidObstacle(obstacle)
                {
                    MaxAcceleration = this.MaximumAcceleration,
                    AvoidMargin     = 4.0f,
                    MaxLookAhead    = 10.0f,
                };

                collisionDetection.Movements.Add(avoidMovement);
            }

            var flockSeek = new DynamicFlockTarget(this)
            {
                Character          = character.KinematicData,
                MaxAcceleration    = this.MaximumAcceleration,
                MovementDebugColor = Color.cyan
            };

            blending.Movements.Add(new MovementWithWeight(cohesion));
            blending.Movements.Add(new MovementWithWeight(separation, 2.0f));
            blending.Movements.Add(new MovementWithWeight(velocityMatch));
            blending.Movements.Add(new MovementWithWeight(collisionDetection, 1000.0f));
            blending.Movements.Add(new MovementWithWeight(flockSeek));

            return(blending);
        }