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

View File

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

View File

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

View File

@ -51,7 +51,9 @@ public class SimManager : MonoBehaviour {
}
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() {
@ -266,21 +268,16 @@ public class SimManager : MonoBehaviour {
return agentObject;
}
public void LoadNewConfig(string configFileName)
{
public void LoadNewConfig(string configFileName) {
simulationConfig = ConfigLoader.LoadSimulationConfig(configFileName);
if (simulationConfig != null)
{
if (simulationConfig != null) {
Debug.Log($"Loaded new configuration: {configFileName}");
RestartSimulation();
}
else
{
} else {
Debug.LogError($"Failed to load configuration: {configFileName}");
}
}
public void RestartSimulation() {
OnSimulationEnded?.Invoke();
Debug.Log("Simulation ended");