// Update is called once per frame void Update() { MovementWithWeight mouseSeek = null; Debug.Log(this.blendedMovement.Movements.ToString()); if (Input.GetMouseButton(0)) { target.Position = new Vector3((((Input.mousePosition.x * X_WORLD_SIZE * 2) / Screen.width) - (X_WORLD_SIZE)), 0, (((Input.mousePosition.y * Z_WORLD_SIZE * 2) / Screen.height) - (Z_WORLD_SIZE))); var seekToPointMovement = new DynamicArrive() { Character = this.character.KinematicData, MaxAcceleration = MAX_ACCELERATION, Target = target }; mouseSeek = new MovementWithWeight(seekToPointMovement, 0.1f); this.blendedMovement.Movements.Add(mouseSeek); mouse = true; } UpdateMovingGameObject(); //the mouse variable can be for level 5 if (mouse) { this.blendedMovement.Movements.Remove(mouseSeek); mouse = false; } }
private int width = 1; // We know that width of cube is 1. // Use this for initialization void Start() { arrive = GetComponent <DynamicArrive>(); align = GetComponent <DynamicAlign>(); k = GameObject.CreatePrimitive(PrimitiveType.Cube); k.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f); }
public override MovementOutput GetMovement() { MovementOutput steering = new MovementOutput(); // Percorrer cada canal if (goal.hasOrientation) { // usar o DynamicAlign... DynamicVelocityMatch da = new DynamicVelocityMatch() { Character = this.Character, Target = new KinematicData(), MaxAcceleration = this.MaxAcceleration }; da.Target.orientation = goal.orientation; steering.angular = da.GetMovement().angular; } if (goal.hasPosition) { // usar o DynamicSeek DynamicArrive ds = new DynamicArrive() { Character = this.Character, Target = new KinematicData(), MaxAcceleration = this.MaxAcceleration, SlowRadius = 8.0f, StopRadius = 4.0f }; /* if (ds.Target.orientation > goal.orientation) * { * ds.Target.position += 0.1f; * } * else * { * ds.Target.position -= 0.1f; * }*/ ds.Target.position = goal.position; steering.linear = ds.GetMovement().linear; } // velocidades e possivelmente erros steering.linear.Normalize(); steering.linear *= this.MaxAcceleration; steering.angular *= this.MaxAcceleration; steering.linear.y = 0.0f; // Failsafe // Debug.Log("X->"+ Character.position.x + " Y->" + Character.position.y + " Z->" + Character.position.z); // Debug.Log("Orientation->" + Character.orientation.ToString()); return(steering); }
public override MovementOutput GetMovement() { MovementOutput steering = new MovementOutput(); // Percorrer cada canal if (goal.hasOrientation) { // usar o DynamicAlign... DynamicVelocityMatch da = new DynamicVelocityMatch() { Character = this.Character, Target = new KinematicData(), MaxAcceleration = this.MaxAcceleration }; da.Target.orientation = goal.orientation; steering.angular = da.GetMovement().angular; } if (goal.hasPosition) { // usar o DynamicSeek DynamicArrive ds = new DynamicArrive() { Character = this.Character, Target = new KinematicData(), MaxAcceleration = this.MaxAcceleration, SlowRadius = 8.0f, StopRadius = 4.0f }; /* if (ds.Target.orientation > goal.orientation) { ds.Target.position += 0.1f; } else { ds.Target.position -= 0.1f; }*/ ds.Target.position = goal.position; steering.linear = ds.GetMovement().linear; } // velocidades e possivelmente erros steering.linear.Normalize(); steering.linear *= this.MaxAcceleration; steering.angular *= this.MaxAcceleration; steering.linear.y = 0.0f; // Failsafe // Debug.Log("X->"+ Character.position.x + " Y->" + Character.position.y + " Z->" + Character.position.z); // Debug.Log("Orientation->" + Character.orientation.ToString()); return steering; }
/* * public SteeringOutput Seek() * { * return new DynamicSeek(agent.k, target.k, maxAcceleration).getSteering(); * } * public SteeringOutput Flee() * { * return new DynamicFlee(agent.k, target.k, maxAcceleration).getSteering(); * } * * public SteeringOutput Pursue() * { * DynamicPursue dp = new DynamicPursue(agent.k, target.k, maxAcceleration, maxPrediction); * SteeringOutput so = dp.getSteering(); * agent.DrawCircle(dp.predictPos, targetRadiusL); * return so; * } */ public SteeringOutput Arrive() { DynamicArrive da = new DynamicArrive(agent.k, target.k, maxAcceleration, maxSpeed, targetRadiusL, slowRadiusL); agent.DrawCircle(target.k.position, slowRadiusL); SteeringOutput so = da.getSteering(); if (pathFollow && !change && current < 5) { current++; change = true; } else if (!agent.hit) { agent.CaughtTarget(); } return(so); }
public void ManageAI(MovementAI ai, Rigidbody2D character) { Kinematic characterKinematic = KinematicAdapter.FromRigidbody2DToKinematic(character); Kinematic targetKinematic = new Kinematic(); Algorithm algorithm = new DynamicNone(); switch (ai.aiAlgorithm) { case AIAlgorithm.DynamicSeek: targetKinematic = KinematicAdapter.FromRigidbody2DToKinematic(ai.target); algorithm = new DynamicSeek(characterKinematic, targetKinematic, ai.maxSpeed); break; case AIAlgorithm.DynamicFlee: targetKinematic = KinematicAdapter.FromRigidbody2DToKinematic(ai.target); algorithm = new DynamicFlee(characterKinematic, targetKinematic, ai.maxSpeed); break; case AIAlgorithm.DynamicArrive: targetKinematic = KinematicAdapter.FromRigidbody2DToKinematic(ai.target); algorithm = new DynamicArrive(characterKinematic, targetKinematic, ai.maxAcceleration, ai.maxSpeed, ai.targetRadius, ai.slowRadius, ai.timeToTarget); break; case AIAlgorithm.DynamicAlign: targetKinematic = KinematicAdapter.FromRigidbody2DToKinematic(ai.target); algorithm = new DynamicAlign(characterKinematic, targetKinematic, ai.maxRotation, ai.maxAngularAcceleration, ai.targetRadius, ai.slowRadius, ai.timeToTarget); break; case AIAlgorithm.DynamicVelocityMatch: targetKinematic = KinematicAdapter.FromRigidbody2DToKinematic(ai.target); algorithm = new DynamicVelocityMatch(characterKinematic, targetKinematic, ai.maxAcceleration, ai.timeToTarget); break; } SteeringOutput steering = algorithm.getSteering(); steering.Apply(characterKinematic, ai.lookWhereYoureGoing, ai.maxSpeed, Time.deltaTime); KinematicAdapter.UpdateRigidbody2DWithKinematic(character, characterKinematic); }
// Use this for initialization void Start() { var textObj = GameObject.Find("InstructionsText"); if (textObj != null) { textObj.GetComponent <Text>().text = "Instructions\n\n" + "Blue Character\n" + "Q - Stationary\n" + "W - Seek\n" + "E - Flee\n" + "R - Arrive\n" + "T - Wander\n\n" + "Green Character\n" + "A - Stationary\n" + "S - Seek\n" + "D - Flee\n" + "F - Arrive\n" + "G - Wander\n"; } // Associating and intializing the movement to the Game Objects in the scene var blueObj = GameObject.Find("Blue"); if (blueObj != null) { this.BlueCharacter = new DynamicCharacter(blueObj) { Drag = DRAG, MaxSpeed = MAX_SPEED } } ; var greenObj = GameObject.Find("Green"); if (greenObj != null) { this.GreenCharacter = new DynamicCharacter(greenObj) { Drag = DRAG, MaxSpeed = MAX_SPEED } } ; this.BlueMovementText = GameObject.Find("BlueMovement").GetComponent <Text>(); this.GreenMovementText = GameObject.Find("GreenMovement").GetComponent <Text>(); #region movement initialization var blueKinematicData = new KinematicData(new StaticData(this.BlueCharacter.GameObject.transform.position)); var greenKinematicData = new KinematicData(new StaticData(this.GreenCharacter.GameObject.transform.position)); this.BlueDynamicSeek = new DynamicSeek { Character = blueKinematicData, Target = this.GreenCharacter.KinematicData, MaxAcceleration = MAX_ACCELERATION }; this.BlueDynamicFlee = new DynamicFlee { Character = blueKinematicData, Target = this.GreenCharacter.KinematicData, MaxAcceleration = MAX_ACCELERATION }; this.BlueDynamicWander = new DynamicWander { Character = blueKinematicData, DebugTarget = this.blueDebugTarget, WanderRate = MathConstants.MATH_PI_2, WanderOffset = 4, WanderRadius = 5, MaxAcceleration = MAX_ACCELERATION }; this.BlueDynamicArrive = new DynamicArrive { Character = blueKinematicData, Target = this.GreenCharacter.KinematicData, MaxAcceleration = MAX_ACCELERATION, stopRadius = 5, slowRadius = 15 }; this.GreenDynamicSeek = new DynamicSeek { Character = greenKinematicData, Target = this.BlueCharacter.KinematicData, MaxAcceleration = MAX_ACCELERATION }; this.GreenDynamicFlee = new DynamicFlee { Character = greenKinematicData, Target = this.BlueCharacter.KinematicData, MaxAcceleration = MAX_ACCELERATION }; this.GreenDynamicWander = new DynamicWander { Character = greenKinematicData, DebugTarget = greenDebugTarget, WanderRate = MathConstants.MATH_2_PI, WanderOffset = 10, WanderRadius = 5, MaxAcceleration = MAX_ACCELERATION }; this.GreenDynamicArrive = new DynamicArrive { Character = greenKinematicData, Target = this.BlueCharacter.KinematicData, MaxAcceleration = MAX_ACCELERATION, stopRadius = 5, slowRadius = 15 }; #endregion } void Update() { if (Input.GetKeyDown(KeyCode.Q)) { this.BlueCharacter.Movement = null; } else if (Input.GetKeyDown(KeyCode.W)) { this.BlueCharacter.Movement = this.BlueDynamicSeek; } else if (Input.GetKeyDown(KeyCode.E)) { this.BlueCharacter.Movement = this.BlueDynamicFlee; } else if (Input.GetKeyDown(KeyCode.T)) { this.BlueCharacter.Movement = this.BlueDynamicWander; } else if (Input.GetKeyDown(KeyCode.R)) { this.BlueCharacter.Movement = this.BlueDynamicArrive; } if (Input.GetKeyDown(KeyCode.A)) { this.GreenCharacter.Movement = null; } else if (Input.GetKeyDown(KeyCode.S)) { this.GreenCharacter.Movement = this.GreenDynamicSeek; } else if (Input.GetKeyDown(KeyCode.D)) { this.GreenCharacter.Movement = this.GreenDynamicFlee; } else if (Input.GetKeyDown(KeyCode.G)) { this.GreenCharacter.Movement = this.GreenDynamicWander; } else if (Input.GetKeyDown(KeyCode.F)) { this.GreenCharacter.Movement = this.GreenDynamicArrive; } this.UpdateMovingGameObject(this.BlueCharacter); this.UpdateMovingGameObject(this.GreenCharacter); // Debugging objects if (this.blueDebugTarget != null && this.BlueCharacter.Movement != null) { this.blueDebugTarget.transform.position = this.BlueCharacter.Movement.Target.position; } if (this.greenDebugTarget != null && this.GreenCharacter.Movement != null) { this.greenDebugTarget.transform.position = this.GreenCharacter.Movement.Target.position; } this.UpdateMovementText(); }
void Update() { if (Input.GetKeyDown(KeyCode.Q)) { this.RedCharacter.Movement = null; } Camera camera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>(); Vector3 PointInWorld = new Vector3(); bool buttonPressed = false; if (Input.GetMouseButton(0)) { var mousePos = Input.mousePosition; mousePos.z = CAMERA_Y; PointInWorld = camera.ScreenToWorldPoint(mousePos); buttonPressed = true; } int FlockArriveCount = 0; foreach (var character in this.Flock) { BlendedMovement movement = (BlendedMovement)character.Movement; MovementWithWeight arriveSearch = movement.Movements.Find(x => x.Movement.Name == "Arrive"); if (arriveSearch != null) { DynamicArrive DynamicArriveMovement = (DynamicArrive)arriveSearch.Movement; if (DynamicArriveMovement.Arrived) { FlockArriveCount++; } } } foreach (var character in this.Flock) { BlendedMovement movement = (BlendedMovement)character.Movement; MovementWithWeight arriveSearch = movement.Movements.Find(x => x.Movement.Name == "Arrive"); if (buttonPressed) { MovementWithWeight wanderSearch = movement.Movements.Find(x => x.Movement.Name == "Wander"); if (wanderSearch != null) { movement.Movements.Remove(wanderSearch); } var DynamicArriveMovement = new DynamicArrive() { Character = character.KinematicData, MaxAcceleration = MAX_ACCELERATION, MovementDebugColor = Color.blue, Flock = this.Flock, Target = new KinematicData(), MaxSpeed = MAXSPEED, SlowRadius = 0, StopRadius = (STOPRADIUS*Flock.Count)/20 }; if (arriveSearch != null) { movement.Movements.Remove(arriveSearch); } DynamicArriveMovement.Target.position = PointInWorld; DynamicArriveMovement.Target.position.y = character.KinematicData.position.y; movement.Movements.Add(new MovementWithWeight(DynamicArriveMovement, (obstacles.Length + this.Flock.Count) * ARRIVEB)); character.Movement = movement; } if (arriveSearch != null) { DynamicArrive DynamicArriveMovement = (DynamicArrive)arriveSearch.Movement; if (FlockArriveCount >= this.Flock.Count) { movement.Movements.Remove(arriveSearch); /* var DynamicWanderMovement = new DynamicWander { Character = character.KinematicData, Target = character.KinematicData, MaxAcceleration = MAX_ACCELERATION, MovementDebugColor = Color.yellow }; movement.Movements.Add(new MovementWithWeight(DynamicWanderMovement, obstacles.Length + this.Flock.Count)); */ character.Movement = movement; } } this.UpdateMovingGameObject(character); } this.UpdateMovementText(); }