ThreatSourceLibaray/docs/articles/intro.md

359 lines
8.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 入门指南
## 安装和使用
### C#/.NET 版本
1. 下载 ThreatSourceLibrary 包并解压
2. 将以下文件复制到你的项目目录(建议放在 lib 文件夹下):
- ThreatSource.dll - 主要的库文件
- ThreatSource.deps.json - 依赖配置文件
- ThreatSource.xml - API 文档文件
3. 在 Visual Studio 中添加引用:
- 右键项目 -> 添加 -> 引用
- 浏览 -> 选择 ThreatSource.dll
4. 在代码中使用:
```csharp
using ThreatSource.Simulation;
class Program
{
static void Main()
{
try
{
// 创建仿真管理器
var simulationManager = new SimulationManager();
// 创建导弹配置
var missileProperties = new MissileProperties
{
Id = "missile1",
InitialPosition = new Vector3D(0, 0, 0),
InitialVelocity = new Vector3D(0, 0, 0),
MaxSpeed = 1000,
MaxAcceleration = 10,
MaxFlightTime = 30,
MaxFlightDistance = 5000,
Mass = 50
};
// 创建导弹实例
var missile = new TerminalSensitiveMissile(
"target1",
missileProperties,
simulationManager
);
// 注册实体
simulationManager.RegisterEntity(missile.Id, missile);
// 激活并发射导弹
missile.Activate();
missile.Fire();
// 仿真主循环
double deltaTime = 0.01;
while (missile.IsActive)
{
missile.Update(deltaTime);
}
}
catch (Exception ex)
{
Trace.TraceError($"Error: {ex.Message}");
}
}
}
```
### C++版本
C++项目有两种使用方式:
#### 方式一:动态加载(推荐)
这种方式适合纯C++项目不需要配置CLR支持。
1. 下载 ThreatSourceNative 包并解压
2. 将 bin 目录下的所有 DLL 文件复制到程序目录
3. 将 include/threat_source.h 添加到项目中
4. 使用 C 风格接口调用库功能
示例代码:
```cpp
#include "threat_source.h"
#include <stdio.h>
int main() {
// 初始化仿真
if (TS_CreateSimulation() != THREATSOURCE_SUCCESS) {
printf("Failed to create simulation\n");
return 1;
}
// 创建导弹
const char* missile_id = "missile1";
int result = TS_CreateMissile(
missile_id,
0, 0, 0, // 初始位置
0, 0, 0, // 初始速度
1000, // 最大速度
10, // 最大加速度
30, // 最大飞行时间
5000, // 最大飞行距离
50 // 质量
);
if (result != THREATSOURCE_SUCCESS) {
char error[256];
TS_GetLastError(error, sizeof(error));
printf("Failed to create missile: %s\n", error);
return 1;
}
// 激活并发射导弹
TS_ActivateMissile(missile_id);
TS_FireMissile(missile_id);
// 仿真主循环
double deltaTime = 0.01;
int is_active = 1;
while (is_active) {
TS_UpdateSimulation(deltaTime);
TS_IsMissileActive(missile_id, &is_active);
}
// 清理
TS_DestroySimulation();
return 0;
}
```
#### 方式二CLR 集成
如果需要使用 .NET 的完整功能,可以选择 CLR 集成方式:
1. 配置项目属性:
- C/C++ -> 常规 -> 公共语言运行时支持:/clr
- 常规 -> 平台工具集Visual Studio 2022 (v143)
- 常规 -> .NET目标框架net8.0
2. 引用 ThreatSource.dll
示例代码:
```cpp
using namespace System;
using namespace ThreatSource::Simulation;
int main() {
try {
// 创建仿真管理器
auto manager = gcnew SimulationManager();
// 创建导弹配置
auto properties = gcnew MissileProperties();
properties->Id = "missile1";
properties->InitialPosition = Vector3D(0, 0, 0);
properties->InitialVelocity = Vector3D(0, 0, 0);
properties->MaxSpeed = 1000;
properties->MaxAcceleration = 10;
properties->MaxFlightTime = 30;
properties->MaxFlightDistance = 5000;
properties->Mass = 50;
// 创建导弹
auto missile = gcnew TerminalSensitiveMissile("target1", properties, manager);
manager->RegisterEntity(missile->Id, missile);
// 激活并发射导弹
missile->Activate();
missile->Fire();
// 仿真主循环
double deltaTime = 0.01;
while (missile->IsActive) {
missile->Update(deltaTime);
}
return 0;
}
catch (Exception^ e) {
Trace::TraceError("Error: {0}", e->Message);
return 1;
}
}
```
## 基本用法
### 初始化仿真管理器
```csharp
var simulationManager = new SimulationManager();
```
### 创建导弹
```csharp
// 创建导弹配置
var missileProperties = new MissileProperties
{
Id = "missile1",
InitialPosition = new Vector3D(0, 0, 0),
InitialOrientation = new Orientation(0, 0, 0),
InitialSpeed = 800,
MaxSpeed = 1000,
MaxFlightTime = 30,
MaxFlightDistance = 5000,
MaxAcceleration = 10,
Mass = 50
};
// 创建导弹实例
var missile = new TerminalSensitiveMissile("target1", missileProperties, simulationManager);
```
### 创建目标
```csharp
// 创建目标
var tank = new Tank(
"target1",
new Vector3D(1000, 0, 0),
Math.PI/4,
simulationManager
);
```
### 注册实体
```csharp
simulationManager.RegisterEntity(missile.Id, missile);
simulationManager.RegisterEntity(tank.Id, tank);
```
### 运行仿真
```csharp
// 激活实体
missile.Activate();
tank.Activate();
// 发射导弹
missile.Fire();
// 仿真主循环
double deltaTime = 0.01;
while (missile.IsActive)
{
missile.Update(deltaTime);
tank.Update(deltaTime);
}
```
## C++使用指南
### C++/CLI基本用法
#### 1. 创建包装类
```cpp
using namespace System;
using namespace ThreatSource::Simulation;
public ref class SimulationWrapper
{
private:
SimulationManager^ manager;
public:
SimulationWrapper()
{
manager = gcnew SimulationManager();
}
void CreateAndRunSimulation()
{
try
{
// 创建导弹配置
auto properties = gcnew MissileProperties();
properties->Id = "missile1";
properties->InitialPosition = Vector3D(0, 0, 0);
properties->InitialSpeed = 800;
properties->MaxSpeed = 1000;
properties->MaxFlightTime = 30;
properties->MaxFlightDistance = 5000;
properties->MaxAcceleration = 10;
properties->Mass = 50;
// 创建导弹
auto missile = gcnew TerminalSensitiveMissile("target1", properties, manager);
// 注册和激活
manager->RegisterEntity(missile->Id, missile);
missile->Activate();
missile->Fire();
// 仿真主循环
double deltaTime = 0.01;
while (missile->IsActive)
{
missile->Update(deltaTime);
}
}
catch (Exception^ e)
{
Trace::TraceError("错误: {0}", e->Message);
}
}
};
```
#### 2. 在原生C++代码中使用
```cpp
int main()
{
try
{
auto simulation = gcnew SimulationWrapper();
simulation->CreateAndRunSimulation();
return 0;
}
catch (Exception^ e)
{
Trace::TraceError("错误: {0}", e->Message);
return 1;
}
}
```
### 注意事项
1. 内存管理
- C++/CLI使用垃圾回收
- 使用gcnew创建托管对象
- 注意托管和非托管资源的混合使用
2. 错误处理
- 使用托管异常处理try/catch
- 异常信息更详细,更容易调试
- 可以直接使用.NET的日志机制
3. 类型系统
- 使用托管类型(^
- 注意值类型和引用类型的区别
- 使用安全的类型转换
## 更多示例
更多示例请参考[示例代码](../examples/Simulation/README.md)。