Add serialized fields for instantaneous velocity and acceleration

more-targets
Titan Yuan 2024-09-24 18:27:26 -07:00
parent 014dfa7714
commit 4293bfec94
4 changed files with 29 additions and 20 deletions

View File

@ -8,6 +8,15 @@ public abstract class Agent : MonoBehaviour {
[SerializeField] [SerializeField]
private FlightPhase _flightPhase = FlightPhase.INITIALIZED; private FlightPhase _flightPhase = FlightPhase.INITIALIZED;
[SerializeField]
protected Vector3 _velocity;
[SerializeField]
protected Vector3 _acceleration;
[SerializeField]
protected Vector3 _dragAcceleration;
[SerializeField] [SerializeField]
protected Agent _target; protected Agent _target;
protected bool _isHit = false; protected bool _isHit = false;
@ -131,6 +140,7 @@ public abstract class Agent : MonoBehaviour {
protected virtual void Awake() { protected virtual void Awake() {
_staticConfig = ConfigLoader.LoadStaticConfig(staticConfigFile); _staticConfig = ConfigLoader.LoadStaticConfig(staticConfigFile);
GetComponent<Rigidbody>().mass = _staticConfig.bodyConfig.mass;
} }
// Start is called before the first frame update // Start is called before the first frame update
@ -174,7 +184,12 @@ public abstract class Agent : MonoBehaviour {
case FlightPhase.TERMINATED: case FlightPhase.TERMINATED:
break; break;
} }
_velocity = GetComponent<Rigidbody>().velocity;
_acceleration =
GetComponent<Rigidbody>().GetAccumulatedForce() / GetComponent<Rigidbody>().mass;
} }
protected virtual void AlignWithVelocity() { protected virtual void AlignWithVelocity() {
Vector3 velocity = GetVelocity(); Vector3 velocity = GetVelocity();
if (velocity.magnitude > 0.1f) // Only align if we have significant velocity if (velocity.magnitude > 0.1f) // Only align if we have significant velocity

View File

@ -4,7 +4,6 @@ using JetBrains.Annotations;
using UnityEngine; using UnityEngine;
public class Hydra70 : Missile { public class Hydra70 : Missile {
private Vector3 _acceleration;
private bool _submunitionsLaunched = false; private bool _submunitionsLaunched = false;
protected override void FixedUpdate() { protected override void FixedUpdate() {
@ -25,7 +24,6 @@ public class Hydra70 : Missile {
// Calculate and set the total acceleration // Calculate and set the total acceleration
Vector3 acceleration = CalculateAcceleration(accelerationInput); Vector3 acceleration = CalculateAcceleration(accelerationInput);
GetComponent<Rigidbody>().AddForce(acceleration, ForceMode.Acceleration); GetComponent<Rigidbody>().AddForce(acceleration, ForceMode.Acceleration);
_acceleration = acceleration;
} }
protected override void DrawDebugVectors() { protected override void DrawDebugVectors() {

View File

@ -6,8 +6,6 @@ public class Missile : Agent {
[SerializeField] [SerializeField]
protected bool _showDebugVectors = true; protected bool _showDebugVectors = true;
[SerializeField]
private Vector3 _boostAcceleration;
// Return whether a target can be assigned to the missile. // Return whether a target can be assigned to the missile.
public override bool IsAssignable() { public override bool IsAssignable() {
bool assignable = !HasLaunched() && !HasAssignedTarget(); bool assignable = !HasLaunched() && !HasAssignedTarget();
@ -51,8 +49,8 @@ public class Missile : Agent {
// Apply the acceleration force // Apply the acceleration force
GetComponent<Rigidbody>().AddForce(acceleration, ForceMode.Acceleration); GetComponent<Rigidbody>().AddForce(acceleration, ForceMode.Acceleration);
_boostAcceleration = acceleration;
} }
protected override void UpdateMidCourse(double deltaTime) {} protected override void UpdateMidCourse(double deltaTime) {}
protected Vector3 CalculateAcceleration(Vector3 accelerationInput, protected Vector3 CalculateAcceleration(Vector3 accelerationInput,
@ -69,6 +67,7 @@ public class Missile : Agent {
// Project the drag acceleration onto the forward direction // Project the drag acceleration onto the forward direction
Vector3 dragAccelerationAlongRoll = dragAcceleration * transform.forward; Vector3 dragAccelerationAlongRoll = dragAcceleration * transform.forward;
_dragAcceleration = dragAccelerationAlongRoll;
return accelerationInput + gravity + dragAccelerationAlongRoll; return accelerationInput + gravity + dragAccelerationAlongRoll;
} }

View File

@ -51,7 +51,9 @@ public class SimManager : MonoBehaviour {
} }
public List<Agent> GetActiveAgents() { public List<Agent> GetActiveAgents() {
return _activeMissiles.ConvertAll(missile => missile as Agent).Concat(_activeTargets.ConvertAll(target => target as Agent)).ToList(); return _activeMissiles.ConvertAll(missile => missile as Agent)
.Concat(_activeTargets.ConvertAll(target => target as Agent))
.ToList();
} }
void Awake() { void Awake() {
@ -266,21 +268,16 @@ public class SimManager : MonoBehaviour {
return agentObject; return agentObject;
} }
public void LoadNewConfig(string configFileName) public void LoadNewConfig(string configFileName) {
{
simulationConfig = ConfigLoader.LoadSimulationConfig(configFileName); simulationConfig = ConfigLoader.LoadSimulationConfig(configFileName);
if (simulationConfig != null) if (simulationConfig != null) {
{
Debug.Log($"Loaded new configuration: {configFileName}"); Debug.Log($"Loaded new configuration: {configFileName}");
RestartSimulation(); RestartSimulation();
} } else {
else
{
Debug.LogError($"Failed to load configuration: {configFileName}"); Debug.LogError($"Failed to load configuration: {configFileName}");
} }
} }
public void RestartSimulation() { public void RestartSimulation() {
OnSimulationEnded?.Invoke(); OnSimulationEnded?.Invoke();
Debug.Log("Simulation ended"); Debug.Log("Simulation ended");