Skip to content

Commit

Permalink
Little tweaks in the car follower component
Browse files Browse the repository at this point in the history
  • Loading branch information
Selinux24 committed Aug 8, 2024
1 parent 9860566 commit 78249fd
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 10 deletions.
18 changes: 17 additions & 1 deletion Samples/05 AISamples/Common/Agents/AgentFollower.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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> Car { get; set; }
public Vector3 Position { get; private set; }
Expand All @@ -19,6 +20,9 @@ public void Update(IGameTime gameTime)
var car = Car?.Invoke();
if (car == null)
{
//Clears the last car
lastCar = car;

return;
}

Expand All @@ -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);
}
}
}
}
16 changes: 15 additions & 1 deletion Samples/05 AISamples/Common/Agents/Car.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
{
Expand Down
14 changes: 9 additions & 5 deletions Samples/05 AISamples/Common/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<Triangle>()
{
Count = 1000,
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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)
Expand All @@ -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);

Expand Down
24 changes: 21 additions & 3 deletions Samples/05 AISamples/SceneCWRVirtualWorld/VirtualWorldScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -93,7 +99,7 @@ public VirtualWorldScene(Game game) : base(game)
tools.AddEditor<LightsEditor>(EditorModes.Lights, 0);
tools.AddEditor<CrossingsEditor>(EditorModes.Crossings, 0);
tools.AddEditor<ParkingsEditor>(EditorModes.Parkings, 0);

carFollower.Car = world.GetBestCar;
}

Expand Down Expand Up @@ -311,7 +317,7 @@ private void ToggleHelp()
}
private void UpdateInputCamera(IGameTime gameTime)
{
if (!followCar)
if (!followCar || world.GetBestCar() == null)
{
Camera.Following = null;

Expand Down Expand Up @@ -433,6 +439,10 @@ private void UpdateInputCars()
{
ToggleFollow();
}
if (Game.Input.KeyJustReleased(Keys.R))
{
RemoveBestCar();
}
}
private void AddCars(int count)
{
Expand Down Expand Up @@ -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()
{
Expand Down

0 comments on commit 78249fd

Please sign in to comment.