345 lines
9.0 KiB
Markdown
345 lines
9.0 KiB
Markdown
# 导弹仿真示例
|
||
|
||
本目录包含了使用仿真系统进行导弹仿真的示例代码。这些示例展示了如何配置和运行不同类型的导弹仿真。
|
||
|
||
## 关于本库
|
||
|
||
ThreatSource 是一个基于 .NET 8.0 的类库,提供了完整的导弹仿真功能。
|
||
|
||
## 系统要求
|
||
|
||
### C#/.NET 用户
|
||
|
||
- .NET 8.0 或更高版本
|
||
- 通过 NuGet 包管理器安装或直接引用 ThreatSource.dll
|
||
|
||
### C++用户
|
||
|
||
本库是一个 .NET 类库,C++用户需要通过 C++/CLI 包装层来使用:
|
||
|
||
- Windows 操作系统
|
||
- Visual Studio 2019 或更高版本
|
||
- 创建 C++/CLI 项目并引用 ThreatSource.dll
|
||
|
||
## 示例文件
|
||
|
||
### C#示例 ([IRMissileSimulation.cs](IRMissileSimulation.cs))
|
||
|
||
红外成像制导导弹仿真示例,展示了:
|
||
|
||
- 如何创建和配置导弹实体
|
||
- 如何设置红外成像制导系统
|
||
- 如何配置仿真环境和参数
|
||
- 如何运行仿真并获取结果
|
||
|
||
#### 源代码
|
||
|
||
```csharp
|
||
using ThreatSource.Simulation;
|
||
using ThreatSource.Missile;
|
||
using ThreatSource.Sensor;
|
||
using ThreatSource.Target;
|
||
|
||
/// <summary>
|
||
/// 红外成像制导导弹仿真示例
|
||
/// </summary>
|
||
public class IRMissileSimulation
|
||
{
|
||
private readonly ISimulationManager _simulationManager;
|
||
private readonly IMissileFactory _missileFactory;
|
||
private readonly ITargetFactory _targetFactory;
|
||
private readonly ISensorFactory _sensorFactory;
|
||
|
||
public IRMissileSimulation()
|
||
{
|
||
_simulationManager = new SimulationManager();
|
||
_missileFactory = new MissileFactory();
|
||
_targetFactory = new TargetFactory();
|
||
_sensorFactory = new SensorFactory();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 运行仿真
|
||
/// </summary>
|
||
public async Task RunSimulation()
|
||
{
|
||
// 创建目标
|
||
var target = _targetFactory.CreateTarget(new TargetConfig
|
||
{
|
||
Id = "target_001",
|
||
Position = new Vector3(1000, 0, 100),
|
||
Velocity = new Vector3(-100, 0, 0),
|
||
Signature = new IRSignature
|
||
{
|
||
Temperature = 350, // 开尔文
|
||
EmissivityFactor = 0.8f
|
||
}
|
||
});
|
||
|
||
// 创建导弹
|
||
var missile = _missileFactory.CreateMissile(new MissileConfig
|
||
{
|
||
Id = "missile_001",
|
||
Position = new Vector3(0, 0, 0),
|
||
MaxSpeed = 800, // 米/秒
|
||
MaxAcceleration = 30, // G
|
||
MaxTurnRate = 20 // 度/秒
|
||
});
|
||
|
||
// 创建红外成像传感器
|
||
var sensor = _sensorFactory.CreateSensor(new IRSensorConfig
|
||
{
|
||
Id = "sensor_001",
|
||
Resolution = new Vector2(640, 480),
|
||
FieldOfView = 60, // 度
|
||
MinTemperature = 270, // 开尔文
|
||
MaxTemperature = 400 // 开尔文
|
||
});
|
||
|
||
// 配置仿真参数
|
||
var config = new SimulationConfig
|
||
{
|
||
TimeStep = 0.02f, // 仿真步长(秒)
|
||
MaxSimulationTime = 60.0f, // 最大仿真时间(秒)
|
||
EnvironmentConditions = new EnvironmentConfig
|
||
{
|
||
Temperature = 288, // 开尔文
|
||
Pressure = 101325, // 帕斯卡
|
||
Humidity = 0.5f // 相对湿度
|
||
}
|
||
};
|
||
|
||
// 初始化仿真
|
||
await _simulationManager.Initialize(config);
|
||
|
||
// 添加实体
|
||
_simulationManager.AddEntity(target);
|
||
_simulationManager.AddEntity(missile);
|
||
_simulationManager.AddEntity(sensor);
|
||
|
||
// 启动仿真
|
||
await _simulationManager.StartSimulation();
|
||
|
||
// 等待仿真完成
|
||
while (_simulationManager.IsRunning)
|
||
{
|
||
await Task.Delay(100);
|
||
}
|
||
|
||
// 获取仿真结果
|
||
var results = _simulationManager.GetSimulationResults();
|
||
ProcessResults(results);
|
||
}
|
||
|
||
private void ProcessResults(SimulationResults results)
|
||
{
|
||
// 处理仿真结果
|
||
Console.WriteLine($"仿真完成时间: {results.CompletionTime}秒");
|
||
Console.WriteLine($"命中精度: {results.HitAccuracy}米");
|
||
Console.WriteLine($"导引头跟踪时间: {results.TrackingTime}秒");
|
||
}
|
||
}
|
||
```
|
||
|
||
### C++示例 ([IRMissileSimulation.cpp](IRMissileSimulation.cpp))
|
||
|
||
这是一个使用C++/CLI的示例代码,展示了如何在C++项目中使用本库:
|
||
|
||
- 如何创建C++/CLI包装层
|
||
- 如何配置导弹实体
|
||
- 如何设置仿真参数
|
||
- 如何运行仿真并获取结果
|
||
|
||
#### 源代码
|
||
|
||
```cpp
|
||
#include "ThreatSource.h"
|
||
using namespace System;
|
||
using namespace ThreatSource::Simulation;
|
||
using namespace ThreatSource::Missile;
|
||
using namespace ThreatSource::Sensor;
|
||
using namespace ThreatSource::Target;
|
||
|
||
/// <summary>
|
||
/// C++/CLI包装类,用于在C++项目中使用仿真系统
|
||
/// </summary>
|
||
public ref class SimulationWrapper
|
||
{
|
||
private:
|
||
ISimulationManager^ _simulationManager;
|
||
IMissileFactory^ _missileFactory;
|
||
ITargetFactory^ _targetFactory;
|
||
ISensorFactory^ _sensorFactory;
|
||
|
||
public:
|
||
SimulationWrapper()
|
||
{
|
||
_simulationManager = gcnew SimulationManager();
|
||
_missileFactory = gcnew MissileFactory();
|
||
_targetFactory = gcnew TargetFactory();
|
||
_sensorFactory = gcnew SensorFactory();
|
||
}
|
||
|
||
void RunSimulation()
|
||
{
|
||
// 创建目标
|
||
auto target = _targetFactory->CreateTarget(gcnew TargetConfig {
|
||
Id = "target_001",
|
||
Position = Vector3(1000, 0, 100),
|
||
Velocity = Vector3(-100, 0, 0),
|
||
Signature = gcnew IRSignature {
|
||
Temperature = 350,
|
||
EmissivityFactor = 0.8f
|
||
}
|
||
});
|
||
|
||
// 创建导弹
|
||
auto missile = _missileFactory->CreateMissile(gcnew MissileConfig {
|
||
Id = "missile_001",
|
||
Position = Vector3(0, 0, 0),
|
||
MaxSpeed = 800,
|
||
MaxAcceleration = 30,
|
||
MaxTurnRate = 20
|
||
});
|
||
|
||
// 创建传感器
|
||
auto sensor = _sensorFactory->CreateSensor(gcnew IRSensorConfig {
|
||
Id = "sensor_001",
|
||
Resolution = Vector2(640, 480),
|
||
FieldOfView = 60,
|
||
MinTemperature = 270,
|
||
MaxTemperature = 400
|
||
});
|
||
|
||
// 配置仿真参数
|
||
auto config = gcnew SimulationConfig {
|
||
TimeStep = 0.02f,
|
||
MaxSimulationTime = 60.0f,
|
||
EnvironmentConditions = gcnew EnvironmentConfig {
|
||
Temperature = 288,
|
||
Pressure = 101325,
|
||
Humidity = 0.5f
|
||
}
|
||
};
|
||
|
||
// 初始化仿真
|
||
_simulationManager->Initialize(config)->Wait();
|
||
|
||
// 添加实体
|
||
_simulationManager->AddEntity(target);
|
||
_simulationManager->AddEntity(missile);
|
||
_simulationManager->AddEntity(sensor);
|
||
|
||
// 启动仿真
|
||
_simulationManager->StartSimulation()->Wait();
|
||
|
||
// 等待仿真完成
|
||
while (_simulationManager->IsRunning)
|
||
{
|
||
System::Threading::Thread::Sleep(100);
|
||
}
|
||
|
||
// 获取仿真结果
|
||
auto results = _simulationManager->GetSimulationResults();
|
||
ProcessResults(results);
|
||
}
|
||
|
||
private:
|
||
void ProcessResults(SimulationResults^ results)
|
||
{
|
||
Console::WriteLine("仿真完成时间: {0}秒", results->CompletionTime);
|
||
Console::WriteLine("命中精度: {0}米", results->HitAccuracy);
|
||
Console::WriteLine("导引头跟踪时间: {0}秒", results->TrackingTime);
|
||
}
|
||
};
|
||
|
||
// 在C++代码中使用
|
||
int main()
|
||
{
|
||
auto simulation = gcnew SimulationWrapper();
|
||
simulation->RunSimulation();
|
||
return 0;
|
||
}
|
||
```
|
||
|
||
## 使用说明
|
||
|
||
### C#/.NET使用方式
|
||
|
||
1. 创建仿真适配器
|
||
|
||
```csharp
|
||
var adapter = new TestSimulationAdapter();
|
||
```
|
||
|
||
2. 配置导弹和目标
|
||
|
||
```csharp
|
||
var missile = new SimulationEntity
|
||
{
|
||
Id = "missile_001",
|
||
Position = new Vector3(0, 0, 0),
|
||
// ... 其他配置
|
||
};
|
||
```
|
||
|
||
3. 设置仿真参数
|
||
|
||
```csharp
|
||
var simConfig = new SimulationConfig
|
||
{
|
||
TimeStep = 0.02f, // 仿真步长(秒)
|
||
MaxSimulationTime = 60.0f, // 最大仿真时间(秒)
|
||
// ... 其他配置
|
||
};
|
||
```
|
||
|
||
4. 运行仿真
|
||
|
||
```csharp
|
||
await adapter.Initialize(simConfig);
|
||
await adapter.StartSimulation();
|
||
```
|
||
|
||
### C++使用方式
|
||
|
||
1. 创建C++/CLI项目
|
||
- 在Visual Studio中创建新的C++/CLI项目
|
||
- 添加对 ThreatSource.dll 的引用
|
||
|
||
2. 创建包装类
|
||
|
||
```cpp
|
||
public ref class SimulationWrapper
|
||
{
|
||
private:
|
||
TestSimulationAdapter^ adapter;
|
||
|
||
public:
|
||
SimulationWrapper()
|
||
{
|
||
adapter = gcnew TestSimulationAdapter();
|
||
}
|
||
|
||
// ... 其他包装方法
|
||
};
|
||
```
|
||
|
||
3. 在C++代码中使用
|
||
|
||
```cpp
|
||
auto simulation = gcnew SimulationWrapper();
|
||
simulation->Initialize();
|
||
simulation->StartSimulation();
|
||
```
|
||
|
||
## 注意事项
|
||
|
||
1. 确保所有参数单位正确(米、秒、开尔文等)
|
||
2. 合理设置仿真时间步长,平衡精度和性能
|
||
3. 注意处理异步操作和事件回调
|
||
4. C++/CLI注意事项:
|
||
- 仅支持Windows平台
|
||
- 需要正确配置项目的目标框架
|
||
- 注意托管和非托管资源的正确释放
|