From 78249fdb0096085e480b20883605538ed3586480 Mon Sep 17 00:00:00 2001 From: Selinux24 Date: Thu, 8 Aug 2024 14:07:38 +0200 Subject: [PATCH] Little tweaks in the car follower component --- .../Common/Agents/AgentFollower.cs | 18 +++++++++++++- Samples/05 AISamples/Common/Agents/Car.cs | 16 ++++++++++++- Samples/05 AISamples/Common/World.cs | 14 +++++++---- .../SceneCWRVirtualWorld/VirtualWorldScene.cs | 24 ++++++++++++++++--- 4 files changed, 62 insertions(+), 10 deletions(-) diff --git a/Samples/05 AISamples/Common/Agents/AgentFollower.cs b/Samples/05 AISamples/Common/Agents/AgentFollower.cs index 84f43dbd..de7cbf33 100644 --- a/Samples/05 AISamples/Common/Agents/AgentFollower.cs +++ b/Samples/05 AISamples/Common/Agents/AgentFollower.cs @@ -8,6 +8,7 @@ class AgentFollower(float distance, float height) : IFollower { private readonly float distance = distance; private readonly float height = height; + private Car lastCar = null; public Func Car { get; set; } public Vector3 Position { get; private set; } @@ -19,6 +20,9 @@ public void Update(IGameTime gameTime) var car = Car?.Invoke(); if (car == null) { + //Clears the last car + lastCar = car; + return; } @@ -29,7 +33,19 @@ public void Update(IGameTime gameTime) d3.Y = height; Interest = new Vector3(p.X, 0f, p.Y); - Position = Vector3.Lerp(Position, Interest + d3, 0.1f); + + if (lastCar != car) + { + lastCar = car; + + //Move camera to the new car + Position = Interest + d3; + } + else + { + //Interpolates the camera position + Position = Vector3.Lerp(Position, Interest + d3, 0.1f); + } } } } diff --git a/Samples/05 AISamples/Common/Agents/Car.cs b/Samples/05 AISamples/Common/Agents/Car.cs index f2ef628f..b4173789 100644 --- a/Samples/05 AISamples/Common/Agents/Car.cs +++ b/Samples/05 AISamples/Common/Agents/Car.cs @@ -27,6 +27,9 @@ class Car private float scale = 1f; + private int stuckedFrames = 0; + private const int maxStuckedFrames = 600; + public AgentControls Controls { get; } public bool Forward => !Damaged && Controls.Forward; public bool Reverse => !Damaged && Controls.Reverse; @@ -37,6 +40,7 @@ class Car public Sensor Sensor { get; } public NeuralNetwork Brain { get; } public bool Damaged { get; private set; } = false; + public bool Stucked { get; private set; } = false; public float FittnessValue { get; set; } = 0; public Car(float width, float height, float depth, AgentControlTypes controlType, float maxSpeed = 3f, float maxReverseSpeed = 1.5f) @@ -86,7 +90,6 @@ public void Update(IGameTime gameTime, Segment2[] roadBorders, Car[] traffic, bo Move(time); CreatePoints(); - if (!Damaged) { var damageBorders = Array.FindAll(roadBorders, b => b.DistanceToPoint(new(x, y)) <= radius); @@ -154,6 +157,17 @@ private void Move(float time) x += direction.X; y += direction.Y; FittnessValue += direction.LengthSquared(); + + if (Forward == Reverse) + { + stuckedFrames++; + } + else + { + stuckedFrames = 0; + } + + Stucked = stuckedFrames >= maxStuckedFrames; } private bool AssessDamage(Segment2[] roadBorders, Car[] traffic, bool damageTraffic) { diff --git a/Samples/05 AISamples/Common/World.cs b/Samples/05 AISamples/Common/World.cs index 1a629942..950162a4 100644 --- a/Samples/05 AISamples/Common/World.cs +++ b/Samples/05 AISamples/Common/World.cs @@ -63,6 +63,7 @@ class World(Graph graph, float height) private const float carLength = 15; private const float carMaxSpeed = 1f; private const float carMaxReverseSpeed = 0.2f; + private float carScale = 1f; private Car bestCar = null; private readonly Color4 bestCarColor = new Color(252, 212, 32, 255); private readonly Color4 carColor = new Color(252, 222, 200, 255); @@ -458,6 +459,9 @@ private async Task InitializeCars(Scene scene) nameof(carDrawer), cDesc); + var bbox = carDrawer[0].GetBoundingBox(); + carScale = MathF.Max(MathF.Max(carWidth / bbox.Width, carHeight / bbox.Height), carLength / bbox.Depth); + var sDesc = new GeometryColorDrawerDescription() { Count = 1000, @@ -529,7 +533,9 @@ private void UpdateCars(IGameTime gameTime) carDrawer[i].TintColor = car == bestCar ? bestCarColor : carColor; } - bestCar = cars.Where(c => !c.Damaged).MaxBy(c => c.FittnessValue); + cars.RemoveAll(c => c == null || c.Damaged || c.Stucked); + + bestCar = cars.MaxBy(c => c.FittnessValue); carDrawer.Visible = cars.Count > 0; @@ -750,11 +756,8 @@ public void RemoveMarking(Marking marking) public Car CreateCar(AgentControlTypes controlType, string brainFile, bool mutate = false, float mutationDelta = 0.1f) { - Car car = new(carWidth, carHeight, carLength, AgentControlTypes.AI, carMaxSpeed, carMaxReverseSpeed); + Car car = new(carWidth, carHeight, carLength, controlType, carMaxSpeed, carMaxReverseSpeed); - var bbox = carDrawer[0].GetBoundingBox(); - float scale = MathF.Max(MathF.Max(carWidth / bbox.Width, carHeight / bbox.Height), carLength / bbox.Depth); - car.SetScale(scale); car.Brain.Load(brainFile); if (mutate) @@ -765,6 +768,7 @@ public Car CreateCar(AgentControlTypes controlType, string brainFile, bool mutat (Vector2 start, Vector2 dir) = GetStart(); car.SetPosition(start); car.SetDirection(dir); + car.SetScale(carScale); AddCar(car); diff --git a/Samples/05 AISamples/SceneCWRVirtualWorld/VirtualWorldScene.cs b/Samples/05 AISamples/SceneCWRVirtualWorld/VirtualWorldScene.cs index e2182567..fb1572fb 100644 --- a/Samples/05 AISamples/SceneCWRVirtualWorld/VirtualWorldScene.cs +++ b/Samples/05 AISamples/SceneCWRVirtualWorld/VirtualWorldScene.cs @@ -53,11 +53,17 @@ class VirtualWorldScene : Scene private const string helpText = @"F1 - CLOSE THIS HELP F2 - TOGGLE TOOLS F5 - ADDS A CAR TO THE WORLD +F6 - ADDS A MUTATED CAR +F7 - ADDS N CARS PRESERVING FIRST BRAIN +F8 - SAVE BEST CAR BRAIN +F - TOGGLE BEST CAR FOLLOWING +R - REMOVE BEST CAR + WASD - MOVE CAMERA SPACE - MOVE CAMERA UP C - MOVE CAMERA DOWN MOUSE - ROTATE CAMERA -F - TOGGLE CAR FOLLOWING + ESC - EXIT"; private bool showHelp = false; @@ -93,7 +99,7 @@ public VirtualWorldScene(Game game) : base(game) tools.AddEditor(EditorModes.Lights, 0); tools.AddEditor(EditorModes.Crossings, 0); tools.AddEditor(EditorModes.Parkings, 0); - + carFollower.Car = world.GetBestCar; } @@ -311,7 +317,7 @@ private void ToggleHelp() } private void UpdateInputCamera(IGameTime gameTime) { - if (!followCar) + if (!followCar || world.GetBestCar() == null) { Camera.Following = null; @@ -433,6 +439,10 @@ private void UpdateInputCars() { ToggleFollow(); } + if (Game.Input.KeyJustReleased(Keys.R)) + { + RemoveBestCar(); + } } private void AddCars(int count) { @@ -466,6 +476,14 @@ private void SaveBestCar() bestCar.Brain.Save(bestCarFileName); } + private void RemoveBestCar() + { + var bestCar = world.GetBestCar(); + if (bestCar != null) + { + world.RemoveCar(bestCar); + } + } public override void GameGraphicsResized() {