Skip to content

Commit

Permalink
Sponsor characters no longer potentially NaN out by reaching the targ…
Browse files Browse the repository at this point in the history
…et goal (or otherwise having an influence sum of zero).
  • Loading branch information
RossNordby committed Apr 6, 2023
1 parent 240455a commit 22dac46
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
8 changes: 5 additions & 3 deletions Demos/Demos/Sponsors/SponsorCharacterAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public SponsorCharacterAI(CharacterControllers characters, in CollidableDescript
this.targetLocation = targetLocation;
}

public void Update(CharacterControllers characters, Simulation simulation, ref QuickList<SponsorNewt> newts, in Vector2 arenaMin, in Vector2 arenaMax, Random random)
public void Update(CharacterControllers characters, Simulation simulation, ref QuickList<SponsorNewt> newts, Random random)
{
var body = simulation.Bodies[bodyHandle];
Vector2 influenceSum = default;
Expand All @@ -44,7 +44,7 @@ public void Update(CharacterControllers characters, Simulation simulation, ref Q
var influenceMagnitude = 1f / (distance * 0.1f + .1f);
influenceSum -= new Vector2(offset.X, offset.Z) * influenceMagnitude / distance;
}
if(distance < 20)
if (distance < 20)
{
spooked = true;
}
Expand All @@ -53,7 +53,9 @@ public void Update(CharacterControllers characters, Simulation simulation, ref Q
influenceSum /= newts.Count;
ref var character = ref characters.GetCharacterByBodyHandle(bodyHandle);
influenceSum -= (new Vector2(body.Pose.Position.X, body.Pose.Position.Z) - targetLocation) * 0.001f;
var targetWorldVelocity = 6f * Vector2.Normalize(influenceSum);
//Newts should do a good job at avoiding a division by zero here, but just in case, guard against it.
var influenceSumLength = influenceSum.Length();
var targetWorldVelocity = influenceSumLength > 1e-6f ? influenceSum * (6f / influenceSumLength) : new Vector2();
//Rephrase the target velocity in terms of the character's control basis.
character.TargetVelocity = new Vector2(targetWorldVelocity.X, -targetWorldVelocity.Y);
if (spooked && random.NextDouble() < 0.015f)
Expand Down
2 changes: 1 addition & 1 deletion Demos/Demos/Sponsors/SponsorDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public override void Update(Window window, Camera camera, Input input, float dt)
}
for (int i = 0; i < characterAIs.Count; ++i)
{
characterAIs[i].Update(characterControllers, Simulation, ref newts, newtArenaMin, newtArenaMax, random);
characterAIs[i].Update(characterControllers, Simulation, ref newts, random);
}
simulationTime += TimestepDuration;
realTime += dt;
Expand Down
2 changes: 1 addition & 1 deletion Demos/SpecializedTests/Media/2.4/NewtTyrannyDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public override void Update(Window window, Camera camera, Input input, float dt)
}
for (int i = 0; i < characterAIs.Count; ++i)
{
characterAIs[i].Update(characterControllers, Simulation, ref newts, newtArenaMin, newtArenaMax, random);
characterAIs[i].Update(characterControllers, Simulation, ref newts, random);
}
simulationTime += TimestepDuration;

Expand Down

0 comments on commit 22dac46

Please sign in to comment.