micromissiles-unity/Assets/Scripts/Sensors/IdealSensor.cs

72 lines
2.7 KiB
C#
Raw Permalink Normal View History

2024-09-12 00:17:21 -07:00
using UnityEngine;
2024-09-13 22:45:25 -07:00
public class IdealSensor : Sensor {
protected override void Start() {
base.Start();
}
2024-09-12 00:17:21 -07:00
2024-09-13 22:45:25 -07:00
public override SensorOutput Sense(Agent target) {
SensorOutput targetSensorOutput = new SensorOutput();
2024-09-12 00:17:21 -07:00
2024-09-13 22:45:25 -07:00
// Sense the target's position
PositionOutput targetPositionSensorOutput = SensePosition(target);
targetSensorOutput.position = targetPositionSensorOutput;
2024-09-12 00:17:21 -07:00
2024-09-13 22:45:25 -07:00
// Sense the target's velocity
VelocityOutput targetVelocitySensorOutput = SenseVelocity(target);
targetSensorOutput.velocity = targetVelocitySensorOutput;
2024-09-12 00:17:21 -07:00
2024-09-13 22:45:25 -07:00
return targetSensorOutput;
}
2024-09-12 00:17:21 -07:00
2024-09-13 22:45:25 -07:00
protected override PositionOutput SensePosition(Agent target) {
PositionOutput positionSensorOutput = new PositionOutput();
2024-09-12 00:17:21 -07:00
2024-09-13 22:45:25 -07:00
// Calculate the relative position of the target
Vector3 relativePosition = target.transform.position - transform.position;
2024-09-12 00:17:21 -07:00
2024-09-13 22:45:25 -07:00
// Calculate the distance (range) to the target
positionSensorOutput.range = relativePosition.magnitude;
2024-09-12 00:17:21 -07:00
2024-09-13 22:45:25 -07:00
// Calculate azimuth (horizontal angle relative to forward)
positionSensorOutput.azimuth =
Vector3.SignedAngle(transform.forward, relativePosition, transform.up);
2024-09-12 00:17:21 -07:00
2024-09-13 22:45:25 -07:00
// Calculate elevation (vertical angle relative to forward)
Vector3 flatRelativePosition = Vector3.ProjectOnPlane(relativePosition, transform.up);
positionSensorOutput.elevation =
Vector3.SignedAngle(flatRelativePosition, relativePosition, transform.right);
2024-09-12 00:17:21 -07:00
2024-09-13 22:45:25 -07:00
return positionSensorOutput;
}
protected override VelocityOutput SenseVelocity(Agent target) {
VelocityOutput velocitySensorOutput = new VelocityOutput();
// Calculate relative position and velocity
Vector3 relativePosition = target.transform.position - transform.position;
2024-09-25 17:51:44 -07:00
Vector3 relativeVelocity = target.GetVelocity() - GetComponent<Rigidbody>().linearVelocity;
2024-09-13 22:45:25 -07:00
// 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;
2024-09-12 00:17:21 -07:00
}
2024-09-13 22:45:25 -07:00
return velocitySensorOutput;
}
}