start on new threat types & strategy abstraction
This commit is contained in:
@@ -128,6 +128,10 @@ public abstract class Agent : MonoBehaviour {
|
||||
return GetComponent<Rigidbody>().linearVelocity;
|
||||
}
|
||||
|
||||
public Vector3 GetAcceleration() {
|
||||
return _acceleration;
|
||||
}
|
||||
|
||||
public double GetDynamicPressure() {
|
||||
var airDensity = Constants.CalculateAirDensityAtAltitude(transform.position.y);
|
||||
var flowSpeed = GetSpeed();
|
||||
|
||||
@@ -4,115 +4,121 @@ using UnityEngine;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
|
||||
|
||||
[Serializable]
|
||||
public class SimulationConfig {
|
||||
[Header("Simulation Settings")]
|
||||
public float timeScale = 0.05f;
|
||||
[Header("Simulation Settings")]
|
||||
public float timeScale = 0.05f;
|
||||
|
||||
[Header("Interceptor Swarm Configurations")]
|
||||
public List<SwarmConfig> interceptor_swarm_configs = new List<SwarmConfig>();
|
||||
// [Header("Defense Points")]
|
||||
// public SwarmConfig defense_points_config;
|
||||
|
||||
[Header("Threat Swarm Configurations")]
|
||||
public List<SwarmConfig> threat_swarm_configs = new List<SwarmConfig>();
|
||||
[Header("Interceptor Swarm Configurations")]
|
||||
public List<SwarmConfig> interceptor_swarm_configs = new List<SwarmConfig>();
|
||||
|
||||
[Header("Threat Swarm Configurations")]
|
||||
public List<SwarmConfig> threat_swarm_configs = new List<SwarmConfig>();
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class DynamicConfig {
|
||||
public LaunchConfig launch_config;
|
||||
public SensorConfig sensor_config;
|
||||
public LaunchConfig launch_config;
|
||||
public SensorConfig sensor_config;
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
[Serializable]
|
||||
public class SwarmConfig {
|
||||
public int num_agents;
|
||||
public AgentConfig agent_config;
|
||||
public int num_agents;
|
||||
public AgentConfig agent_config;
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class AgentConfig {
|
||||
public InterceptorType interceptor_type;
|
||||
public ThreatType threat_type;
|
||||
public InitialState initial_state;
|
||||
public StandardDeviation standard_deviation;
|
||||
public DynamicConfig dynamic_config;
|
||||
public PlottingConfig plotting_config;
|
||||
public SubmunitionsConfig submunitions_config;
|
||||
public InterceptorType interceptor_type;
|
||||
public ThreatType threat_type;
|
||||
public InitialState initial_state;
|
||||
public StandardDeviation standard_deviation;
|
||||
public DynamicConfig dynamic_config;
|
||||
public PlottingConfig plotting_config;
|
||||
public SubmunitionsConfig submunitions_config;
|
||||
|
||||
public static AgentConfig FromSubmunitionAgentConfig(SubmunitionAgentConfig submunitionConfig) {
|
||||
return new AgentConfig {
|
||||
interceptor_type = submunitionConfig.interceptor_type,
|
||||
initial_state = submunitionConfig.initial_state,
|
||||
standard_deviation = submunitionConfig.standard_deviation,
|
||||
dynamic_config = submunitionConfig.dynamic_config,
|
||||
plotting_config = submunitionConfig.plotting_config,
|
||||
// Set other fields as needed, using default values if not present in SubmunitionAgentConfig
|
||||
threat_type = ThreatType.DRONE, // Or another default value
|
||||
submunitions_config = null // Or a default value if needed
|
||||
};
|
||||
}
|
||||
public static AgentConfig FromSubmunitionAgentConfig(SubmunitionAgentConfig submunitionConfig) {
|
||||
return new AgentConfig {
|
||||
interceptor_type = submunitionConfig.interceptor_type,
|
||||
initial_state = submunitionConfig.initial_state,
|
||||
standard_deviation = submunitionConfig.standard_deviation,
|
||||
dynamic_config = submunitionConfig.dynamic_config,
|
||||
plotting_config = submunitionConfig.plotting_config,
|
||||
// Set other fields as needed, using default values if not present in SubmunitionAgentConfig
|
||||
threat_type = ThreatType.DRONE, // Or another default value
|
||||
submunitions_config = null // Or a default value if needed
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class InitialState {
|
||||
public Vector3 position;
|
||||
public Vector3 rotation;
|
||||
public Vector3 velocity;
|
||||
public Vector3 position;
|
||||
public Vector3 rotation;
|
||||
public Vector3 velocity;
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class StandardDeviation {
|
||||
public Vector3 position;
|
||||
public Vector3 velocity;
|
||||
public Vector3 position;
|
||||
public Vector3 velocity;
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
[Serializable]
|
||||
public class LaunchConfig {
|
||||
public float launch_time;
|
||||
public float launch_time;
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class PlottingConfig {
|
||||
public ConfigColor color;
|
||||
public LineStyle linestyle;
|
||||
public Marker marker;
|
||||
public ConfigColor color;
|
||||
public LineStyle linestyle;
|
||||
public Marker marker;
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class SubmunitionsConfig {
|
||||
public int num_submunitions;
|
||||
public LaunchConfig launch_config;
|
||||
public SubmunitionAgentConfig agent_config;
|
||||
public int num_submunitions;
|
||||
public LaunchConfig launch_config;
|
||||
public SubmunitionAgentConfig agent_config;
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class SubmunitionAgentConfig {
|
||||
public InterceptorType interceptor_type;
|
||||
public InitialState initial_state;
|
||||
public StandardDeviation standard_deviation;
|
||||
public DynamicConfig dynamic_config;
|
||||
public PlottingConfig plotting_config;
|
||||
public InterceptorType interceptor_type;
|
||||
public InitialState initial_state;
|
||||
public StandardDeviation standard_deviation;
|
||||
public DynamicConfig dynamic_config;
|
||||
public PlottingConfig plotting_config;
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class SensorConfig {
|
||||
public SensorType type;
|
||||
public float frequency;
|
||||
public SensorType type;
|
||||
public float frequency;
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class TargetConfig {
|
||||
public ThreatType threat_type;
|
||||
public InitialState initial_state;
|
||||
public PlottingConfig plotting_config;
|
||||
public string prefabName;
|
||||
public ThreatType threat_type;
|
||||
public InitialState initial_state;
|
||||
public PlottingConfig plotting_config;
|
||||
public string prefabName;
|
||||
}
|
||||
|
||||
// Enums
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
public enum InterceptorType { HYDRA_70, MICROMISSILE }
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
public enum ThreatType { DRONE, MISSILE }
|
||||
public enum ThreatType {
|
||||
DRONE,
|
||||
FIXED_WING_MISSILE,
|
||||
ROLL_STABILIZED_MISSILE
|
||||
} // Add ballistic later -michael
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
public enum ConfigColor { BLUE, GREEN, RED }
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
|
||||
28
Assets/Scripts/DefendPoint.cs
Normal file
28
Assets/Scripts/DefendPoint.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// Static targetable point to defend
|
||||
/// </summary>
|
||||
public class DefendPoint : Agent {
|
||||
// Currently just initializes to the origin
|
||||
public DefendPoint() {
|
||||
// Set the initial state
|
||||
this.transform.position = Vector3.zero;
|
||||
}
|
||||
|
||||
protected override void FixedUpdate() {
|
||||
return;
|
||||
}
|
||||
|
||||
protected override void UpdateBoost(double deltaTime) {
|
||||
return;
|
||||
}
|
||||
|
||||
protected override void UpdateMidCourse(double deltaTime) {
|
||||
return;
|
||||
}
|
||||
|
||||
protected override void UpdateReady(double deltaTime) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/DefendPoint.cs.meta
Normal file
2
Assets/Scripts/DefendPoint.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7eed014a152a65043be19cd044faa5d2
|
||||
@@ -75,7 +75,6 @@ public class SimManager : MonoBehaviour {
|
||||
StartSimulation();
|
||||
ResumeSimulation();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void SetTimeScale(float timeScale) {
|
||||
@@ -197,9 +196,10 @@ public class SimManager : MonoBehaviour {
|
||||
/// <param name="config">Configuration settings for the interceptor.</param>
|
||||
/// <returns>The created Interceptor instance, or null if creation failed.</returns>
|
||||
public Interceptor CreateInterceptor(AgentConfig config) {
|
||||
string prefabName = config.interceptor_type switch { InterceptorType.HYDRA_70 => "Hydra70",
|
||||
InterceptorType.MICROMISSILE => "Micromissile",
|
||||
_ => "Hydra70" };
|
||||
string prefabName =
|
||||
config.interceptor_type switch { InterceptorType.HYDRA_70 => "Hydra70",
|
||||
InterceptorType.MICROMISSILE => "Micromissile",
|
||||
_ => "Hydra70" };
|
||||
|
||||
GameObject missileObject = CreateAgent(config, prefabName);
|
||||
if (missileObject == null)
|
||||
@@ -231,7 +231,8 @@ public class SimManager : MonoBehaviour {
|
||||
/// <returns>The created Threat instance, or null if creation failed.</returns>
|
||||
private Threat CreateThreat(AgentConfig config) {
|
||||
string prefabName = config.threat_type switch {
|
||||
ThreatType.DRONE => "Drone", ThreatType.MISSILE => "MissileThreat",
|
||||
ThreatType.DRONE => "Drone", ThreatType.FIXED_WING_MISSILE => "FixedWingMissileThreat",
|
||||
ThreatType.ROLL_STABILIZED_MISSILE => "RollStabilizedMissileThreat",
|
||||
_ => throw new System.ArgumentException($"Unsupported threat type: {config.threat_type}")
|
||||
};
|
||||
GameObject threatObject = CreateAgent(config, prefabName);
|
||||
|
||||
@@ -2,7 +2,7 @@ using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class DroneTarget : Threat {
|
||||
public class DroneThreat : Threat {
|
||||
// Start is called before the first frame update
|
||||
protected override void Start() {
|
||||
base.Start();
|
||||
41
Assets/Scripts/Threats/FixedWingMissileThreat.cs
Normal file
41
Assets/Scripts/Threats/FixedWingMissileThreat.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class FixedWingMissileThreat : Threat {
|
||||
protected override void UpdateBoost(double deltaTime) {
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
|
||||
protected override void UpdateMidCourse(double deltaTime) {
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Strategy for moving the Threat in a straight path towards its target.
|
||||
/// </summary>
|
||||
public class DirectPathStrategy : NavigationStrategy {
|
||||
public override void Execute(Threat threat, List<Threat> swarmMates, FlightPhase flightPhase,
|
||||
List<Interceptor> interceptors, double deltaTime) {
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Strategy for moving the Threat in an S-curve towards a predefined target.
|
||||
/// </summary>
|
||||
public class SlalomStrategy : NavigationStrategy {
|
||||
private float maxAmplitude;
|
||||
private float periodDistance;
|
||||
|
||||
public SlalomStrategy(float maxAmplitude, float periodDistance) {
|
||||
this.maxAmplitude = maxAmplitude;
|
||||
this.periodDistance = periodDistance;
|
||||
}
|
||||
|
||||
public override void Execute(Threat threat, List<Threat> swarmMates, FlightPhase flightPhase,
|
||||
List<Interceptor> interceptors, double deltaTime) {
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Threats/FixedWingMissileThreat.cs.meta
Normal file
2
Assets/Scripts/Threats/FixedWingMissileThreat.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4e6c2fbd1e492be448760f5045b13b2e
|
||||
@@ -1,107 +0,0 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// Base class for missile targets. Uses same set of flight phases as base Hydra-70.
|
||||
/// </summary>
|
||||
public class MissileThreat : Threat
|
||||
{
|
||||
protected float boostAcceleration = 20;
|
||||
protected float midcourseAcceleration = 5;
|
||||
|
||||
protected override void UpdateReady(double deltaTime)
|
||||
{
|
||||
// if in ready phase, just set to boost phase immediately
|
||||
SetFlightPhase(FlightPhase.BOOST);
|
||||
}
|
||||
|
||||
protected override void UpdateBoost(double deltaTime)
|
||||
{
|
||||
// The interceptor only accelerates along its roll axis (forward in Unity)
|
||||
Vector3 rollAxis = transform.forward;
|
||||
|
||||
// Calculate boost acceleration
|
||||
float boostAcceleration =
|
||||
(float)(_staticConfig.boostConfig.boostAcceleration * Constants.kGravity);
|
||||
Vector3 accelerationInput = boostAcceleration * rollAxis;
|
||||
|
||||
// Calculate the total acceleration
|
||||
Vector3 acceleration = CalculateAcceleration(accelerationInput);
|
||||
|
||||
// Apply the acceleration force
|
||||
GetComponent<Rigidbody>().AddForce(acceleration, ForceMode.Acceleration);
|
||||
}
|
||||
|
||||
protected override void UpdateMidCourse(double deltaTime)
|
||||
{
|
||||
Vector3 accelerationInput = Vector3.zero;
|
||||
// Calculate and set the total acceleration
|
||||
Vector3 acceleration = CalculateAcceleration(accelerationInput);
|
||||
GetComponent<Rigidbody>().AddForce(acceleration, ForceMode.Acceleration);
|
||||
}
|
||||
|
||||
protected Vector3 CalculateAcceleration(Vector3 accelerationInput,
|
||||
bool compensateForGravity = false)
|
||||
{
|
||||
Vector3 gravity = Physics.gravity;
|
||||
if (compensateForGravity)
|
||||
{
|
||||
Vector3 gravityProjection = CalculateGravityProjectionOnPitchAndYaw();
|
||||
accelerationInput -= gravityProjection;
|
||||
}
|
||||
|
||||
float airDrag = CalculateDrag();
|
||||
float liftInducedDrag = CalculateLiftInducedDrag(accelerationInput);
|
||||
float dragAcceleration = -(airDrag + liftInducedDrag);
|
||||
|
||||
// Project the drag acceleration onto the forward direction
|
||||
Vector3 dragAccelerationAlongRoll = dragAcceleration * transform.forward;
|
||||
_dragAcceleration = dragAccelerationAlongRoll;
|
||||
|
||||
return accelerationInput + gravity + dragAccelerationAlongRoll;
|
||||
}
|
||||
|
||||
protected float CalculateMaxAcceleration()
|
||||
{
|
||||
float maxReferenceAcceleration =
|
||||
(float)(_staticConfig.accelerationConfig.maxReferenceAcceleration * Constants.kGravity);
|
||||
float referenceSpeed = _staticConfig.accelerationConfig.referenceSpeed;
|
||||
return Mathf.Pow(GetComponent<Rigidbody>().linearVelocity.magnitude / referenceSpeed, 2) *
|
||||
maxReferenceAcceleration;
|
||||
}
|
||||
|
||||
protected Vector3 CalculateGravityProjectionOnPitchAndYaw()
|
||||
{
|
||||
Vector3 gravity = Physics.gravity;
|
||||
Vector3 pitchAxis = transform.right;
|
||||
Vector3 yawAxis = transform.up;
|
||||
|
||||
// Project the gravity onto the pitch and yaw axes
|
||||
float gravityProjectionPitchCoefficient = Vector3.Dot(gravity, pitchAxis);
|
||||
float gravityProjectionYawCoefficient = Vector3.Dot(gravity, yawAxis);
|
||||
|
||||
// Return the sum of the projections
|
||||
return gravityProjectionPitchCoefficient * pitchAxis +
|
||||
gravityProjectionYawCoefficient * yawAxis;
|
||||
}
|
||||
|
||||
private float CalculateDrag()
|
||||
{
|
||||
float dragCoefficient = _staticConfig.liftDragConfig.dragCoefficient;
|
||||
float crossSectionalArea = _staticConfig.bodyConfig.crossSectionalArea;
|
||||
float mass = _staticConfig.bodyConfig.mass;
|
||||
float dynamicPressure = (float)GetDynamicPressure();
|
||||
float dragForce = dragCoefficient * dynamicPressure * crossSectionalArea;
|
||||
return dragForce / mass;
|
||||
}
|
||||
|
||||
private float CalculateLiftInducedDrag(Vector3 accelerationInput)
|
||||
{
|
||||
float liftAcceleration =
|
||||
(accelerationInput - Vector3.Dot(accelerationInput, transform.up) * transform.up).magnitude;
|
||||
float liftDragRatio = _staticConfig.liftDragConfig.liftDragRatio;
|
||||
return Mathf.Abs(liftAcceleration / liftDragRatio);
|
||||
}
|
||||
|
||||
}
|
||||
204
Assets/Scripts/Threats/RollStabilizedMissileThreat.cs
Normal file
204
Assets/Scripts/Threats/RollStabilizedMissileThreat.cs
Normal file
@@ -0,0 +1,204 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// Base class for missile targets. Uses same set of flight phases as base Hydra-70.
|
||||
/// </summary>
|
||||
public class RollStabilizedMissileThreat : Threat {
|
||||
protected float boostAcceleration = 20;
|
||||
protected float midcourseAcceleration = 0;
|
||||
protected float terminalAcceleration = 22;
|
||||
protected float maxAmplitude = 22;
|
||||
|
||||
public RollStabilizedMissileThreat() {
|
||||
strategy = new DirectPathStrategy();
|
||||
}
|
||||
|
||||
protected override void UpdateBoost(double deltaTime) {
|
||||
// The interceptor only accelerates along its roll axis (forward in Unity)
|
||||
Vector3 rollAxis = transform.forward;
|
||||
|
||||
// Calculate boost acceleration
|
||||
float boostAcceleration =
|
||||
(float)(_staticConfig.boostConfig.boostAcceleration * Constants.kGravity);
|
||||
Vector3 accelerationInput = boostAcceleration * rollAxis;
|
||||
|
||||
// Calculate the total acceleration
|
||||
Vector3 acceleration = CalculateAcceleration(accelerationInput);
|
||||
|
||||
// Apply the acceleration force
|
||||
GetComponent<Rigidbody>().AddForce(acceleration, ForceMode.Acceleration);
|
||||
}
|
||||
|
||||
protected override void UpdateMidCourse(double deltaTime) {
|
||||
Vector3 accelerationInput = Vector3.zero;
|
||||
// Calculate and set the total acceleration
|
||||
Vector3 acceleration = CalculateAcceleration(accelerationInput);
|
||||
GetComponent<Rigidbody>().AddForce(acceleration, ForceMode.Acceleration);
|
||||
}
|
||||
|
||||
protected Vector3 CalculateAcceleration(Vector3 accelerationInput,
|
||||
bool compensateForGravity = false) {
|
||||
Vector3 gravity = Physics.gravity;
|
||||
if (compensateForGravity) {
|
||||
Vector3 gravityProjection = CalculateGravityProjectionOnPitchAndYaw();
|
||||
accelerationInput -= gravityProjection;
|
||||
}
|
||||
|
||||
float airDrag = CalculateDrag();
|
||||
float liftInducedDrag = CalculateLiftInducedDrag(accelerationInput);
|
||||
float dragAcceleration = -(airDrag + liftInducedDrag);
|
||||
|
||||
// Project the drag acceleration onto the forward direction
|
||||
Vector3 dragAccelerationAlongRoll = dragAcceleration * transform.forward;
|
||||
_dragAcceleration = dragAccelerationAlongRoll;
|
||||
|
||||
return accelerationInput + gravity + dragAccelerationAlongRoll;
|
||||
}
|
||||
|
||||
protected float CalculateMaxAcceleration() {
|
||||
float maxReferenceAcceleration =
|
||||
(float)(_staticConfig.accelerationConfig.maxReferenceAcceleration * Constants.kGravity);
|
||||
float referenceSpeed = _staticConfig.accelerationConfig.referenceSpeed;
|
||||
return Mathf.Pow(GetComponent<Rigidbody>().linearVelocity.magnitude / referenceSpeed, 2) *
|
||||
maxReferenceAcceleration;
|
||||
}
|
||||
|
||||
protected Vector3 CalculateGravityProjectionOnPitchAndYaw() {
|
||||
Vector3 gravity = Physics.gravity;
|
||||
Vector3 pitchAxis = transform.right;
|
||||
Vector3 yawAxis = transform.up;
|
||||
|
||||
// Project the gravity onto the pitch and yaw axes
|
||||
float gravityProjectionPitchCoefficient = Vector3.Dot(gravity, pitchAxis);
|
||||
float gravityProjectionYawCoefficient = Vector3.Dot(gravity, yawAxis);
|
||||
|
||||
// Return the sum of the projections
|
||||
return gravityProjectionPitchCoefficient * pitchAxis +
|
||||
gravityProjectionYawCoefficient * yawAxis;
|
||||
}
|
||||
|
||||
private float CalculateDrag() {
|
||||
float dragCoefficient = _staticConfig.liftDragConfig.dragCoefficient;
|
||||
float crossSectionalArea = _staticConfig.bodyConfig.crossSectionalArea;
|
||||
float mass = _staticConfig.bodyConfig.mass;
|
||||
float dynamicPressure = (float)GetDynamicPressure();
|
||||
float dragForce = dragCoefficient * dynamicPressure * crossSectionalArea;
|
||||
return dragForce / mass;
|
||||
}
|
||||
|
||||
private float CalculateLiftInducedDrag(Vector3 accelerationInput) {
|
||||
float liftAcceleration =
|
||||
(accelerationInput - Vector3.Dot(accelerationInput, transform.up) * transform.up).magnitude;
|
||||
float liftDragRatio = _staticConfig.liftDragConfig.liftDragRatio;
|
||||
return Mathf.Abs(liftAcceleration / liftDragRatio);
|
||||
}
|
||||
|
||||
// ===========================================================
|
||||
// STRATEGIES
|
||||
// ===========================================================
|
||||
|
||||
/// <summary>
|
||||
/// Strategy for moving the Threat in a straight path towards its target.
|
||||
/// </summary>
|
||||
public class DirectPathStrategy : NavigationStrategy {
|
||||
DefendPoint target = new DefendPoint();
|
||||
|
||||
private float _navigationGain = 3f; // Typically 3-5
|
||||
private SensorOutput _sensorOutput;
|
||||
private Vector3 _accelerationCommand;
|
||||
private double _elapsedTime = 0;
|
||||
|
||||
public override void Execute(Threat threat, List<Threat> swarmMates, FlightPhase flightPhase,
|
||||
List<Interceptor> interceptors, double deltaTime) {
|
||||
RollStabilizedMissileThreat missileThreat = threat as RollStabilizedMissileThreat;
|
||||
if (missileThreat == null) {
|
||||
Debug.LogError("DirectPathStrategy can only be used with RollStabilizedMissileThreat");
|
||||
return;
|
||||
}
|
||||
|
||||
_elapsedTime += deltaTime;
|
||||
Vector3 accelerationInput = Vector3.zero;
|
||||
|
||||
if (target != null) {
|
||||
// Correct the state of the threat model at the sensor frequency
|
||||
float sensorUpdatePeriod =
|
||||
1f / missileThreat._agentConfig.dynamic_config.sensor_config.frequency;
|
||||
if (_elapsedTime >= sensorUpdatePeriod) {
|
||||
_sensorOutput = new SensorOutput();
|
||||
missileThreat.GetComponent<Sensor>().Sense(target);
|
||||
Debug.Log(_sensorOutput.velocity.range);
|
||||
_elapsedTime = 0;
|
||||
}
|
||||
|
||||
// Check whether the threat should be considered a miss
|
||||
SensorOutput sensorOutput = missileThreat.GetComponent<Sensor>().Sense(target);
|
||||
if (sensorOutput.velocity.range > 1000f) {
|
||||
missileThreat.MarkAsMiss();
|
||||
}
|
||||
|
||||
// Calculate the acceleration input
|
||||
accelerationInput = CalculateAccelerationCommand(missileThreat, _sensorOutput);
|
||||
} else {
|
||||
Debug.LogError("DirectPathStrategy requires a target to be set");
|
||||
}
|
||||
|
||||
// Calculate and set the total acceleration
|
||||
Vector3 acceleration =
|
||||
missileThreat.CalculateAcceleration(accelerationInput, compensateForGravity: true);
|
||||
missileThreat.GetComponent<Rigidbody>().AddForce(acceleration, ForceMode.Acceleration);
|
||||
}
|
||||
|
||||
private Vector3 CalculateAccelerationCommand(Threat threat, SensorOutput sensorOutput) {
|
||||
RollStabilizedMissileThreat missileThreat = threat as RollStabilizedMissileThreat;
|
||||
|
||||
// Implement Proportional Navigation guidance law
|
||||
Vector3 accelerationCommand;
|
||||
|
||||
// Extract relevant information from sensor output
|
||||
float los_rate_az = sensorOutput.velocity.azimuth;
|
||||
float los_rate_el = sensorOutput.velocity.elevation;
|
||||
float closing_velocity =
|
||||
-sensorOutput.velocity
|
||||
.range; // Negative because closing velocity is opposite to range rate
|
||||
|
||||
// Navigation gain (adjust as needed)
|
||||
float N = _navigationGain;
|
||||
|
||||
// Calculate acceleration commands in azimuth and elevation planes
|
||||
float acc_az = N * closing_velocity * los_rate_az;
|
||||
float acc_el = N * closing_velocity * los_rate_el;
|
||||
|
||||
// Convert acceleration commands to craft body frame
|
||||
accelerationCommand =
|
||||
missileThreat.transform.right * acc_az + missileThreat.transform.up * acc_el;
|
||||
|
||||
// Clamp the acceleration command to the maximum acceleration
|
||||
float maxAcceleration = missileThreat.CalculateMaxAcceleration();
|
||||
accelerationCommand = Vector3.ClampMagnitude(accelerationCommand, maxAcceleration);
|
||||
|
||||
// Update the stored acceleration command for debugging
|
||||
_accelerationCommand = accelerationCommand;
|
||||
return accelerationCommand;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Strategy for moving the Threat in a spiral towards a predefined target.
|
||||
/// </summary>
|
||||
public class SpiralStrategy : NavigationStrategy {
|
||||
private float spiralRadius;
|
||||
private float periodDistance;
|
||||
|
||||
public SpiralStrategy(float spiralRadius, float periodDistance) {
|
||||
this.spiralRadius = spiralRadius;
|
||||
this.periodDistance = periodDistance;
|
||||
}
|
||||
|
||||
public override void Execute(Threat threat, List<Threat> swarmMates, FlightPhase flightPhase,
|
||||
List<Interceptor> interceptors, double deltaTime) {
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
8
Assets/Scripts/Threats/Strategies.meta
Normal file
8
Assets/Scripts/Threats/Strategies.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0d798867791c1444b985d8807b144572
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -3,6 +3,29 @@ using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public abstract class Threat : Agent {
|
||||
/// <summary>
|
||||
/// Strategy for moving the Threat towards a predefined target.
|
||||
/// </summary>
|
||||
public abstract class NavigationStrategy {
|
||||
/// <summary>
|
||||
/// Execute one timestep of the strategy for the given threat and flight phase. Should only
|
||||
/// apply normal forces to the threat. Can also change the threat's flight phase. Should NOT
|
||||
/// manage thrust.
|
||||
/// </summary>
|
||||
/// <param name="threat">Parent Threat object</param>
|
||||
/// <param name="swarmMates">List of other threats in the swarm</param>
|
||||
/// <param name="flightPhase">Current flight phase</param>
|
||||
/// <param name="interceptors">List of active interceptors</param>
|
||||
/// <param name="deltaTime">Timestep</param>
|
||||
public abstract void Execute(Threat threat, List<Threat> swarmMates, FlightPhase flightPhase,
|
||||
List<Interceptor> interceptors, double deltaTime);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Navigation strategy that this threat will use to move towards its target.
|
||||
/// </summary>
|
||||
public NavigationStrategy strategy;
|
||||
|
||||
public override bool IsAssignable() {
|
||||
return false;
|
||||
}
|
||||
@@ -13,5 +36,18 @@ public abstract class Threat : Agent {
|
||||
|
||||
protected override void FixedUpdate() {
|
||||
base.FixedUpdate();
|
||||
// NOTE: no swarm-mates for now
|
||||
strategy.Execute(this, new List<Threat>(), GetFlightPhase(),
|
||||
SimManager.Instance.GetActiveInterceptors(), Time.fixedDeltaTime);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// OVERRIDE: THIS SHOULD NEVER BE CALLED; threats always start in midcourse or (at the very
|
||||
/// earliest) boost phase.
|
||||
/// </summary>
|
||||
/// <param name="deltaTime"></param>
|
||||
/// <exception cref="System.NotImplementedException"></exception>
|
||||
protected override void UpdateReady(double deltaTime) {
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user