Fix NPC static avoidance (#17039)

This commit is contained in:
metalgearsloth
2023-06-01 23:11:39 +10:00
committed by GitHub
parent 4783a655a4
commit a9e723a93f

View File

@@ -385,7 +385,8 @@ public sealed partial class NPCSteeringSystem
EntityQuery<PhysicsComponent> bodyQuery, EntityQuery<PhysicsComponent> bodyQuery,
EntityQuery<TransformComponent> xformQuery) EntityQuery<TransformComponent> xformQuery)
{ {
var detectionRadius = MathF.Max(1f, agentRadius); var objectRadius = 0.15f;
var detectionRadius = MathF.Max(0.35f, agentRadius + objectRadius);
foreach (var ent in _lookup.GetEntitiesInRange(uid, detectionRadius, LookupFlags.Static)) foreach (var ent in _lookup.GetEntitiesInRange(uid, detectionRadius, LookupFlags.Static))
{ {
@@ -400,31 +401,20 @@ public sealed partial class NPCSteeringSystem
continue; continue;
} }
if (!_physics.TryGetNearestPoints(uid, ent, out var pointA, out var pointB, xform, xformQuery.GetComponent(ent))) if (!_physics.TryGetNearestPoints(uid, ent, out _, out var pointB, xform, xformQuery.GetComponent(ent)))
continue; continue;
var obstacleDirection = pointB - pointA; var obstacleDirection = pointB - worldPos;
var obstableDistance = obstacleDirection.Length; var obstableDistance = obstacleDirection.Length;
if (obstableDistance > detectionRadius) if (obstableDistance > detectionRadius || obstableDistance == 0f)
continue; continue;
// Fallback to worldpos if we're colliding.
if (obstableDistance == 0f)
{
obstacleDirection = pointB - worldPos;
obstableDistance = obstacleDirection.Length;
if (obstableDistance == 0f)
continue;
obstableDistance = agentRadius;
}
dangerPoints.Add(pointB); dangerPoints.Add(pointB);
obstacleDirection = offsetRot.RotateVec(obstacleDirection); obstacleDirection = offsetRot.RotateVec(obstacleDirection);
var norm = obstacleDirection.Normalized; var norm = obstacleDirection.Normalized;
var weight = obstableDistance <= agentRadius ? 1f : (detectionRadius - obstableDistance) / detectionRadius; // Weight it to 1 if we used the fallback, otherwise relative distance.
var weight = obstableDistance <= agentRadius ? 1f : (obstableDistance - agentRadius) / objectRadius;
for (var i = 0; i < InterestDirections; i++) for (var i = 0; i < InterestDirections; i++)
{ {
@@ -455,7 +445,8 @@ public sealed partial class NPCSteeringSystem
EntityQuery<PhysicsComponent> bodyQuery, EntityQuery<PhysicsComponent> bodyQuery,
EntityQuery<TransformComponent> xformQuery) EntityQuery<TransformComponent> xformQuery)
{ {
var detectionRadius = MathF.Max(0.35f, agentRadius + 0.1f); var objectRadius = 0.1f;
var detectionRadius = MathF.Max(0.35f, agentRadius + objectRadius);
var ourVelocity = body.LinearVelocity; var ourVelocity = body.LinearVelocity;
var factionQuery = GetEntityQuery<FactionComponent>(); var factionQuery = GetEntityQuery<FactionComponent>();
factionQuery.TryGetComponent(uid, out var ourFaction); factionQuery.TryGetComponent(uid, out var ourFaction);
@@ -492,7 +483,7 @@ public sealed partial class NPCSteeringSystem
obstacleDirection = offsetRot.RotateVec(obstacleDirection); obstacleDirection = offsetRot.RotateVec(obstacleDirection);
var norm = obstacleDirection.Normalized; var norm = obstacleDirection.Normalized;
var weight = obstableDistance <= agentRadius ? 1f : (detectionRadius - obstableDistance) / detectionRadius; var weight = obstableDistance <= agentRadius ? 1f : (obstableDistance - agentRadius) / objectRadius;
weight *= 1f; weight *= 1f;
for (var i = 0; i < InterestDirections; i++) for (var i = 0; i < InterestDirections; i++)