This commit is contained in:
turtlebasket
2024-10-04 02:51:19 +00:00
parent be34e325d6
commit 7805acc392
387 changed files with 95157 additions and 0 deletions

View File

@@ -0,0 +1,71 @@
using UnityEngine;
public class IdealSensor : Sensor {
protected override void Start() {
base.Start();
}
public override SensorOutput Sense(Agent target) {
SensorOutput targetSensorOutput = new SensorOutput();
// Sense the target's position
PositionOutput targetPositionSensorOutput = SensePosition(target);
targetSensorOutput.position = targetPositionSensorOutput;
// Sense the target's velocity
VelocityOutput targetVelocitySensorOutput = SenseVelocity(target);
targetSensorOutput.velocity = targetVelocitySensorOutput;
return targetSensorOutput;
}
protected override PositionOutput SensePosition(Agent target) {
PositionOutput positionSensorOutput = new PositionOutput();
// Calculate the relative position of the target
Vector3 relativePosition = target.transform.position - transform.position;
// Calculate the distance (range) to the target
positionSensorOutput.range = relativePosition.magnitude;
// Calculate azimuth (horizontal angle relative to forward)
positionSensorOutput.azimuth =
Vector3.SignedAngle(transform.forward, relativePosition, transform.up);
// Calculate elevation (vertical angle relative to forward)
Vector3 flatRelativePosition = Vector3.ProjectOnPlane(relativePosition, transform.up);
positionSensorOutput.elevation =
Vector3.SignedAngle(flatRelativePosition, relativePosition, transform.right);
return positionSensorOutput;
}
protected override VelocityOutput SenseVelocity(Agent target) {
VelocityOutput velocitySensorOutput = new VelocityOutput();
// Calculate relative position and velocity
Vector3 relativePosition = target.transform.position - transform.position;
Vector3 relativeVelocity = target.GetVelocity() - GetComponent<Rigidbody>().linearVelocity;
// Calculate range rate (radial velocity)
velocitySensorOutput.range = Vector3.Dot(relativeVelocity, relativePosition.normalized);
// Project relative velocity onto a plane perpendicular to relative position
Vector3 tangentialVelocity =
Vector3.ProjectOnPlane(relativeVelocity, relativePosition.normalized);
// Calculate azimuth rate
Vector3 horizontalVelocity = Vector3.ProjectOnPlane(tangentialVelocity, transform.up);
velocitySensorOutput.azimuth =
Vector3.Dot(horizontalVelocity, transform.right) / relativePosition.magnitude;
// Calculate elevation rate
Vector3 verticalVelocity = Vector3.Project(tangentialVelocity, transform.up);
velocitySensorOutput.elevation = verticalVelocity.magnitude / relativePosition.magnitude;
if (Vector3.Dot(verticalVelocity, transform.up) < 0) {
velocitySensorOutput.elevation *= -1;
}
return velocitySensorOutput;
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f9b285afc8c96ee4f9ca22836fd105d0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,70 @@
using UnityEngine;
public abstract class Sensor : MonoBehaviour {
protected Agent _agent;
protected virtual void Start() {
_agent = GetComponent<Agent>();
}
/// <summary>
/// Main sensing method to gather information about a target agent.
/// </summary>
/// <param name="target">The agent to sense.</param>
/// <returns>SensorOutput containing position and velocity data.</returns>
/// <remarks>
/// Implementers should:
/// 1. Call SensePosition to get position data.
/// 2. Call SenseVelocity to get velocity data.
/// 3. Combine results into a SensorOutput struct.
/// </remarks>
public abstract SensorOutput Sense(Agent target);
/// <summary>
/// Calculates the relative position of the target agent.
/// </summary>
/// <param name="target">The agent to sense.</param>
/// <returns>PositionOutput containing range, azimuth, and elevation.</returns>
/// <remarks>
/// Implementers should calculate:
/// - range: Distance to the target (in unity units).
/// - azimuth: Horizontal angle to the target (in degrees).
/// Positive is clockwise from the forward direction.
/// - elevation: Vertical angle to the target (in degrees).
/// Positive is above the horizontal plane.
/// </remarks>
protected abstract PositionOutput SensePosition(Agent target);
/// <summary>
/// Calculates the relative velocity of the target agent.
/// </summary>
/// <param name="target">The agent to sense.</param>
/// <returns>VelocityOutput containing range rate, azimuth rate, and elevation rate.</returns>
/// <remarks>
/// Implementers should calculate:
/// - range: Radial velocity (closing speed) in units/second.
/// Positive means the target is moving away.
/// - azimuth: Rate of change of azimuth in degrees/second.
/// Positive means the target is moving clockwise.
/// - elevation: Rate of change of elevation in degrees/second.
/// Positive means the target is moving upwards.
/// </remarks>
protected abstract VelocityOutput SenseVelocity(Agent target);
}
public struct SensorOutput {
public PositionOutput position;
public VelocityOutput velocity;
}
public struct PositionOutput {
public float range;
public float azimuth;
public float elevation;
}
public struct VelocityOutput {
public float range;
public float azimuth;
public float elevation;
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 285bc14e5585a804b9bc44b10b8c3cb1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: