Skip to content

Commit

Permalink
Multiple rotations to GetShortestPath
Browse files Browse the repository at this point in the history
  • Loading branch information
AlvaroHG committed Aug 10, 2024
1 parent 02eb012 commit 18783a3
Showing 1 changed file with 92 additions and 5 deletions.
97 changes: 92 additions & 5 deletions unity/Assets/Scripts/BaseFPSAgentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ public Vector3[] getReachablePositions(
);
}
#if UNITY_EDITOR
Debug.DrawLine(p, newPosition, Color.cyan, 100000f);
// Debug.DrawLine(p, newPosition, Color.cyan, 100000f);
#endif
}
}
Expand Down Expand Up @@ -6329,6 +6329,28 @@ public UnityEngine.AI.NavMeshPath getShortestPath(
);
}

private void getShortestPath(
string objectType,
string objectId,
Vector3 startPosition,
IEnumerable<Quaternion> startRotations,
float allowedError,
IEnumerable<int> navMeshIds = null,
bool sampleFromNavmesh = true
) {
SimObjPhysics sop = getSimObjectFromTypeOrId(objectType, objectId);
var path = GetSimObjectNavMeshTargets(
sop,
startPosition,
startRotations,
allowedError,
navMeshIds: navMeshIds,
sampleFromNavmesh: sampleFromNavmesh
);
// VisualizePath(startPosition, path);
actionFinishedEmit(success: true, actionReturn: path);
}

private void getShortestPath(
string objectType,
string objectId,
Expand Down Expand Up @@ -6371,6 +6393,26 @@ public void GetShortestPath(
);
}

public void GetShortestPath(
Vector3 position,
List<Vector3> rotations,
string objectType = null,
string objectId = null,
float allowedError = DefaultAllowedErrorInShortestPath,
IEnumerable<int> navMeshIds = null,
bool sampleFromNavmesh = true
) {
getShortestPath(
objectType: objectType,
objectId: objectId,
startPosition: position,
startRotations: rotations.Select(r => Quaternion.Euler(r)),
allowedError: allowedError,
navMeshIds: navMeshIds,
sampleFromNavmesh: sampleFromNavmesh
);
}

// public void GetShortestPathNew(
// Vector3 position,
// Vector3 rotation,
Expand Down Expand Up @@ -6405,7 +6447,7 @@ public void GetShortestPath(
objectType,
objectId,
position,
Quaternion.Euler(Vector3.zero),
Quaternion.identity,
allowedError,
navMeshIds,
sampleFromNavmesh: sampleFromNavmesh
Expand Down Expand Up @@ -6754,21 +6796,27 @@ public bool getReachablePositionToObjectVisible(
"Procedural3",
"Procedural0"
);

var rotations = new List<float> { 0.0f, 90.0f, 180.0f, 270f }; //, 180.0f, 360f};
int stepsTaken = 0;
pos = Vector3.negativeInfinity;
while (pointsQueue.Count != 0) {
stepsTaken += 1;
Vector3 p = pointsQueue.Dequeue();

if (!goodPoints.Contains(p)) {
var preRotation = transform.rotation;
// foreach (var rotation in rotations) {
goodPoints.Add(p);
transform.position = p;
var rot = transform.rotation;
// transform.rotation = Quaternion.Euler(new Vector3(0.0f, rotation, 0.0f));
// make sure to rotate just the Camera, not the whole agent
m_Camera.transform.LookAt(targetSOP.transform, transform.up);

bool isVisible = IsInteractable(targetSOP);

transform.rotation = rot;
// transform.rotation = rot;

if (isVisible) {
pos = p;
Expand Down Expand Up @@ -6834,13 +6882,15 @@ public bool getReachablePositionToObjectVisible(
if (shouldEnqueue) {
pointsQueue.Enqueue(newPosition);
#if UNITY_EDITOR
Debug.DrawLine(p, newPosition, Color.cyan, 100000f);
Debug.DrawLine(p, newPosition, Color.green, 100000f);
#endif
#if UNITY_EDITOR
Debug.DrawLine(p, newPosition, Color.cyan, 10f);
// Debug.DrawLine(p, newPosition, Color.cyan, 10f);
#endif
}
}
// }
// transform.rotation = preRotation;
}
if (stepsTaken > Math.Floor(maxStepCount / (gridSize * gridSize))) {
throw new InvalidOperationException(
Expand All @@ -6857,6 +6907,43 @@ public bool getReachablePositionToObjectVisible(
return false;
}

private UnityEngine.AI.NavMeshPath GetSimObjectNavMeshTargets(
SimObjPhysics targetSOP,
Vector3 initialPosition,
IEnumerable<Quaternion> initialRotations,
float allowedError,
bool visualize = false,
IEnumerable<int> navMeshIds = null,
bool sampleFromNavmesh = true
) {
var path = new UnityEngine.AI.NavMeshPath();
InvalidOperationException lastException = null;
foreach (var rotation in initialRotations) {
lastException = null;
try {
path = GetSimObjectNavMeshTarget(
targetSOP: targetSOP,
initialPosition: initialPosition,
initialRotation: rotation,
allowedError: allowedError,
visualize: visualize,
navMeshIds: navMeshIds,
sampleFromNavmesh: sampleFromNavmesh
);
} catch (InvalidOperationException e) {
lastException = e;
}

if (path.status == NavMeshPathStatus.PathComplete) {
return path;
}
}
if (lastException != null) {
throw lastException;
}
return path;
}

private UnityEngine.AI.NavMeshPath GetSimObjectNavMeshTarget(
SimObjPhysics targetSOP,
Vector3 initialPosition,
Expand Down

0 comments on commit 18783a3

Please sign in to comment.