-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncmode_demo.cpp
73 lines (63 loc) · 2.6 KB
/
syncmode_demo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "MessageManager.hpp"
#include <random>
static const string HOST = "127.0.0.1"; // sercer host.
static const uint16_t PORT = 2000; // server post.
static const size_t WORKER_THREADS = 0ULL;
static const string TOWN_NAME = "Town06";
static std::mt19937_64 rng((std::random_device())());
static volatile bool keyboardIrruption = false;
// to raise a runtime error.
#define EXPECT_TRUE(pred) \
if (!(pred)) \
{ \
throw std::runtime_error(#pred); \
}
// Pick a random element from @a range.
template <typename RangeT, typename RNG>
static auto &RandomChoice(const RangeT &range, RNG &&generator)
{
EXPECT_TRUE(range.size() > 0u);
std::uniform_int_distribution<size_t> dist{0u, range.size() - 1u};
return range[dist(std::forward<RNG>(generator))];
}
int main()
{
// connect to server
auto client = cc::Client(HOST, PORT, WORKER_THREADS);
client.SetTimeout(3s);
std::cout << "[INFO] Client API version : " << client.GetClientVersion() << '\n';
std::cout << "[INFO] Server API version : " << client.GetServerVersion() << '\n';
auto world = client.GetWorld();
auto settings = world.GetSettings();
settings.fixed_delta_seconds = 0.02;
settings.synchronous_mode = true;
world.ApplySettings(settings);
auto map = world.GetMap();
auto bplib = world.GetBlueprintLibrary();
auto transform = RandomChoice(map->GetRecommendedSpawnPoints(), rng);
auto bpVeh = *bplib->Find("vehicle.tesla.model3");
auto actor = world.TrySpawnActor(bpVeh, transform);
auto pActor = static_pointer_cast<cc::Vehicle>(actor);
pActor->SetAutopilot();
auto spectator = world.GetSpectator();
double lastframetime = 0;
while (true)
{
auto time_point = std::chrono::steady_clock::now() + std::chrono::milliseconds(1000 / 50);
world.Tick(1s);
auto snapshot = world.GetSnapshot();
auto delta_seconds = snapshot.GetTimestamp().delta_seconds;
auto now = snapshot.GetTimestamp().platform_timestamp;
auto plattimespend = now - lastframetime;
std::cout << "simulation time step: " << delta_seconds << "s" << std::endl;
std::cout << "real time step: " << plattimespend << " s" << std::endl;
lastframetime = now;
auto trans = pActor->GetTransform();
trans.location -= 16.0f * trans.GetForwardVector();
trans.location.z += 10.0f;
trans.rotation.yaw += 0.0f;
trans.rotation.pitch = -25.0f;
spectator->SetTransform(trans);
std::this_thread::sleep_until(time_point);
}
}