162 lines
6.1 KiB
C++
162 lines
6.1 KiB
C++
/**
|
||
* 注意:这是一个C++/CLI示例,展示如何在C++项目中使用ThreatSource库
|
||
* 实际项目中需要:
|
||
* 1. 创建C++/CLI项目
|
||
* 2. 引用ThreatSource.dll
|
||
* 3. 配置正确的目标框架(.NET 8.0)
|
||
*/
|
||
|
||
using namespace System;
|
||
using namespace ThreatSource::Simulation;
|
||
using namespace ThreatSource::Data;
|
||
using namespace ThreatSource::Missile;
|
||
|
||
namespace ThreatSourceWrapper {
|
||
|
||
/// <summary>
|
||
/// C++/CLI包装类,用于在C++项目中使用红外成像制导导弹仿真
|
||
/// </summary>
|
||
public ref class IRMissileSimulationWrapper
|
||
{
|
||
private:
|
||
ISimulationManager^ simulationManager;
|
||
ThreatSourceDataManager^ dataManager;
|
||
|
||
public:
|
||
IRMissileSimulationWrapper()
|
||
{
|
||
simulationManager = gcnew SimulationManager();
|
||
dataManager = gcnew ThreatSourceDataManager();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 运行红外成像制导导弹仿真示例
|
||
/// </summary>
|
||
void RunSimulationExample()
|
||
{
|
||
try {
|
||
// 步骤1:初始化仿真管理器
|
||
simulationManager->StartSimulation(0.02); // 20ms时间步长
|
||
|
||
// 步骤2:创建目标实体
|
||
auto target = gcnew BaseEquipment("target_001");
|
||
target->KState = gcnew KinematicState();
|
||
target->KState->Position = Vector3(5000, 0, 1000); // 5km距离,1km高度
|
||
target->KState->Velocity = Vector3(-50, 0, 0); // 50m/s向西移动
|
||
target->KState->Orientation = Orientation(0, 0, 0);
|
||
|
||
// 步骤3:从配置创建红外成像制导导弹
|
||
auto missileData = dataManager->GetMissile("IR_Missile_Example");
|
||
auto missile = gcnew InfraredImagingTerminalGuidanceMissile("missile_001", missileData);
|
||
missile->KState = gcnew KinematicState();
|
||
missile->KState->Position = Vector3(0, 0, 100); // 起始位置
|
||
missile->KState->Velocity = Vector3(200, 0, 0); // 初始速度200m/s
|
||
missile->KState->Orientation = Orientation(0, 0, 0); // 初始朝向
|
||
|
||
// 步骤4:注册实体到仿真管理器
|
||
simulationManager->RegisterEntity(target);
|
||
simulationManager->RegisterEntity(missile);
|
||
|
||
// 步骤5:订阅仿真事件
|
||
simulationManager->Subscribe<MissileExplodeEvent^>(
|
||
gcnew Action<MissileExplodeEvent^>(this, &IRMissileSimulationWrapper::OnMissileExplode)
|
||
);
|
||
simulationManager->Subscribe<FlightPhaseChangeEvent^>(
|
||
gcnew Action<FlightPhaseChangeEvent^>(this, &IRMissileSimulationWrapper::OnFlightPhaseChange)
|
||
);
|
||
|
||
// 步骤6:运行仿真循环
|
||
double simulationTime = 0;
|
||
double maxTime = 60.0; // 最大仿真时间60秒
|
||
|
||
Console::WriteLine("开始红外成像制导导弹仿真...");
|
||
Console::WriteLine("目标位置: ({0}, {1}, {2})",
|
||
target->KState->Position.X,
|
||
target->KState->Position.Y,
|
||
target->KState->Position.Z);
|
||
Console::WriteLine("导弹初始位置: ({0}, {1}, {2})",
|
||
missile->KState->Position.X,
|
||
missile->KState->Position.Y,
|
||
missile->KState->Position.Z);
|
||
|
||
while (simulationTime < maxTime && missile->IsActive)
|
||
{
|
||
// 更新仿真
|
||
simulationManager->UpdateSimulation();
|
||
simulationTime = simulationManager->CurrentTime;
|
||
|
||
// 每秒输出一次状态
|
||
if (Math::Abs(simulationTime - Math::Floor(simulationTime)) < 0.02)
|
||
{
|
||
auto distance = Vector3::Distance(missile->KState->Position, target->KState->Position);
|
||
Console::WriteLine("时间: {0:F1}s, 距离目标: {1:F1}m, 导弹阶段: {2}",
|
||
simulationTime, distance, missile->FlightPhase);
|
||
}
|
||
|
||
System::Threading::Thread::Sleep(1); // 避免阻塞
|
||
}
|
||
|
||
// 步骤7:输出仿真结果
|
||
auto finalDistance = Vector3::Distance(missile->KState->Position, target->KState->Position);
|
||
Console::WriteLine("仿真结束 - 最终距离: {0:F2}m", finalDistance);
|
||
|
||
if (finalDistance < 10.0)
|
||
{
|
||
Console::WriteLine("导弹成功命中目标!");
|
||
}
|
||
else
|
||
{
|
||
Console::WriteLine("导弹未能命中目标。");
|
||
}
|
||
|
||
}
|
||
catch (Exception^ e) {
|
||
Console::WriteLine("仿真过程出错: {0}", e->Message);
|
||
}
|
||
finally {
|
||
// 清理资源
|
||
simulationManager->StopSimulation();
|
||
}
|
||
}
|
||
|
||
private:
|
||
/// <summary>
|
||
/// 处理导弹爆炸事件
|
||
/// </summary>
|
||
void OnMissileExplode(MissileExplodeEvent^ evt)
|
||
{
|
||
Console::WriteLine("导弹 {0} 在位置 ({1}, {2}, {3}) 爆炸",
|
||
evt->SenderId,
|
||
evt->Position.X,
|
||
evt->Position.Y,
|
||
evt->Position.Z);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 处理飞行阶段变化事件
|
||
/// </summary>
|
||
void OnFlightPhaseChange(FlightPhaseChangeEvent^ evt)
|
||
{
|
||
Console::WriteLine("导弹 {0} 进入 {1} 阶段", evt->SenderId, evt->NewPhase);
|
||
}
|
||
};
|
||
}
|
||
|
||
/// <summary>
|
||
/// 示例:如何在原生C++代码中使用包装类
|
||
/// </summary>
|
||
int main()
|
||
{
|
||
try {
|
||
auto simulation = gcnew ThreatSourceWrapper::IRMissileSimulationWrapper();
|
||
simulation->RunSimulationExample();
|
||
|
||
Console::WriteLine("按任意键退出...");
|
||
Console::ReadKey();
|
||
return 0;
|
||
}
|
||
catch (Exception^ e) {
|
||
Console::WriteLine("错误: {0}", e->Message);
|
||
return 1;
|
||
}
|
||
}
|