begin populate WIP missile threat + minor logging convienences
This commit is contained in:
@@ -42,7 +42,7 @@ public abstract class Agent : MonoBehaviour {
|
||||
|
||||
public void SetFlightPhase(FlightPhase flightPhase) {
|
||||
Debug.Log(
|
||||
$"Setting flight phase to {flightPhase} at time {SimManager.Instance.GetElapsedSimulationTime()}");
|
||||
$"Setting {name} flight phase to {flightPhase} at time {SimManager.Instance.GetElapsedSimulationTime()}");
|
||||
_timeInPhase = 0;
|
||||
_flightPhase = flightPhase;
|
||||
}
|
||||
|
||||
@@ -112,7 +112,7 @@ public class TargetConfig {
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
public enum InterceptorType { HYDRA_70, MICROMISSILE }
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
public enum ThreatType { DRONE, ANTISHIP_MISSILE }
|
||||
public enum ThreatType { DRONE, MISSILE }
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
public enum ConfigColor { BLUE, GREEN, RED }
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -118,6 +119,10 @@ public class SimManager : MonoBehaviour {
|
||||
foreach (var swarmConfig in simulationConfig.threat_swarm_configs) {
|
||||
for (int i = 0; i < swarmConfig.num_agents; i++) {
|
||||
var threat = CreateThreat(swarmConfig.agent_config);
|
||||
if (threat == null) {
|
||||
Debug.LogError($"Failed to create threat: {swarmConfig.agent_config}");
|
||||
continue;
|
||||
}
|
||||
threat.OnAgentHit += RegisterThreatHit;
|
||||
threat.OnAgentMiss += RegisterThreatMiss;
|
||||
}
|
||||
@@ -226,12 +231,14 @@ 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.ANTISHIP_MISSILE => "AntishipMissile",
|
||||
ThreatType.DRONE => "Drone", ThreatType.MISSILE => "MissileThreat",
|
||||
_ => throw new System.ArgumentException($"Unsupported threat type: {config.threat_type}")
|
||||
};
|
||||
GameObject threatObject = CreateAgent(config, prefabName);
|
||||
if (threatObject == null)
|
||||
if (threatObject == null) {
|
||||
Debug.LogError($"Failed to create threat for {prefabName}.");
|
||||
return null;
|
||||
}
|
||||
|
||||
_threats.Add(threatObject.GetComponent<Threat>());
|
||||
_activeThreats.Add(threatObject.GetComponent<Threat>());
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class AntishipMissile : MonoBehaviour {
|
||||
// Start is called before the first frame update
|
||||
void Start() {}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update() {}
|
||||
}
|
||||
107
Assets/Scripts/Threats/MissileThreat.cs
Normal file
107
Assets/Scripts/Threats/MissileThreat.cs
Normal file
@@ -0,0 +1,107 @@
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user