CARLA
Toggle main menu visibility
Loading...
Searching...
No Matches
Unreal
CarlaUnreal
Plugins
Carla
Source
Carla
Navigation
CarlaNavigationSubsystem.cpp
Go to the documentation of this file.
1
// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma
2
// de Barcelona (UAB).
3
//
4
// This work is licensed under the terms of the MIT license.
5
// For a copy, see <https://opensource.org/licenses/MIT>.
6
7
#include "
Carla/Navigation/CarlaNavigationSubsystem.h
"
8
#include "
Carla.h
"
9
#include "
Carla/Navigation/CarlaWalkerNavFilters.h
"
10
11
#include <util/ue-header-guard-begin.h>
12
#include "CollisionQueryParams.h"
13
#include "Engine/World.h"
14
#include "NavigationSystem.h"
15
#include "NavMesh/RecastNavMesh.h"
16
#include "WorldPartition/WorldPartition.h"
17
#include "WorldPartition/WorldPartitionRuntimeCell.h"
18
#include "WorldPartition/WorldPartitionStreamingSource.h"
19
#include "WorldPartition/WorldPartitionSubsystem.h"
20
#include <util/ue-header-guard-end.h>
21
22
namespace
23
{
24
/// Navmesh samples tried per GetRandomNavLocation call before giving up.
25
constexpr
int32 MaxSampleAttempts = 32;
26
27
/// Radius around a sampled point whose World Partition cells must be
28
/// activated before the point is accepted (cm).
29
constexpr
float
StreamingQueryRadius = 500.0f;
30
31
/// Clearance kept between the capsule's bottom and the sampled point, on
32
/// top of the agent step height, so the overlap test does not touch the
33
/// ground the walker stands on: covers the navmesh's height error and the
34
/// capsule's rim on slopes up to the agent's max slope (cm).
35
constexpr
float
GroundClearance = 10.0f;
36
37
/// Fallbacks when the nav data config has no agent dimensions; they match
38
/// the CarlaWalker agent in DefaultEngine.ini (cm).
39
constexpr
float
FallbackWalkerRadius = 35.0f;
40
constexpr
float
FallbackWalkerHeight = 180.0f;
41
constexpr
float
FallbackWalkerStepHeight = 25.0f;
42
}
43
44
bool
UCarlaNavigationSubsystem::HasServerSideNavigation
()
const
45
{
46
const
UNavigationSystemV1 *NavSys =
47
FNavigationSystem::GetCurrent<UNavigationSystemV1>(GetWorld());
48
if
(NavSys ==
nullptr
)
49
{
50
return
false
;
51
}
52
const
ARecastNavMesh *NavMesh =
53
Cast<ARecastNavMesh>(NavSys->GetDefaultNavDataInstance());
54
return
(NavMesh !=
nullptr
) && NavMesh->HasValidNavmesh();
55
}
56
57
bool
UCarlaNavigationSubsystem::GetRandomNavLocation
(FVector &OutLocation)
const
58
{
59
UNavigationSystemV1 *NavSys =
60
FNavigationSystem::GetCurrent<UNavigationSystemV1>(GetWorld());
61
if
(NavSys ==
nullptr
)
62
{
63
return
false
;
64
}
65
ANavigationData *NavData = NavSys->GetDefaultNavDataInstance();
66
if
(NavData ==
nullptr
)
67
{
68
return
false
;
69
}
70
// Sample with the non-crossing walker filter so random locations land on
71
// sidewalk/terrain polys, never on the roadway -- matching the legacy
72
// get_random_location_from_navigation semantics. On navmeshes built before
73
// the road/crosswalk areas existed the filter simply has no effect.
74
FSharedConstNavQueryFilter QueryFilter = UNavigationQueryFilter::GetQueryFilter(
75
*NavData, TSubclassOf<UNavigationQueryFilter>(UCarlaWalkerNavFilter::StaticClass()));
76
77
// The streamed navmesh also covers the terrain under some building
78
// footprints, and on World Partition maps the building's cell can stream in
79
// later than the nav chunk. A walker spawned there ends up inside the
80
// building's collision and is pushed down through the terrain (#10212), so
81
// every sample is checked and resampled until a walker fits.
82
int32 NoPoly = 0;
83
int32 NotStreamed = 0;
84
int32 Overlapping = 0;
85
for
(int32 Attempt = 0; Attempt < MaxSampleAttempts; ++Attempt)
86
{
87
FNavLocation Result;
88
if
(!NavSys->GetRandomPoint(Result, NavData, QueryFilter))
89
{
90
return
false
;
91
}
92
// UNavigationSystemV1::GetRandomPoint reports success even when Detour
93
// found no poly (e.g. it drew a tile whose polys the walker filter all
94
// excludes) and leaves the location at the origin; a walker spawned there
95
// falls out of the map.
96
if
(!Result.HasNodeRef())
97
{
98
++NoPoly;
99
continue
;
100
}
101
switch
(
CheckWalkerLocation
(Result.Location, *NavData))
102
{
103
case
ERejectReason::None
:
104
if
(NoPoly + NotStreamed + Overlapping > 0)
105
{
106
UE_LOG(LogCarla, Verbose,
107
TEXT(
"Random nav location: accepted after rejecting %d without poly, %d not streamed, %d overlapping"
),
108
NoPoly, NotStreamed, Overlapping);
109
}
110
OutLocation = Result.Location;
111
return
true
;
112
case
ERejectReason::NotStreamed
:
113
++NotStreamed;
114
break
;
115
case
ERejectReason::Overlap
:
116
++Overlapping;
117
UE_LOG(LogCarla, Verbose,
118
TEXT(
"Random nav location: rejected %s, a walker would overlap static geometry"
),
119
*Result.Location.ToString());
120
break
;
121
}
122
}
123
UE_LOG(LogCarla, Log,
124
TEXT(
"Random nav location: no acceptable point in %d samples (%d without poly, %d not streamed, %d overlapping)"
),
125
MaxSampleAttempts, NoPoly, NotStreamed, Overlapping);
126
return
false
;
127
}
128
129
UCarlaNavigationSubsystem::ERejectReason
UCarlaNavigationSubsystem::CheckWalkerLocation
(
130
const
FVector &
Location
,
131
const
ANavigationData &NavData)
const
132
{
133
UWorld *World = GetWorld();
134
check(World !=
nullptr
);
135
136
if
(World->GetWorldPartition() !=
nullptr
)
137
{
138
const
UWorldPartitionSubsystem *WPSubsystem =
139
World->GetSubsystem<UWorldPartitionSubsystem>();
140
if
(WPSubsystem !=
nullptr
)
141
{
142
FWorldPartitionStreamingQuerySource Query(
Location
);
143
Query.Radius = StreamingQueryRadius;
144
Query.bUseGridLoadingRange =
false
;
145
if
(!WPSubsystem->IsStreamingCompleted(
146
EWorldPartitionRuntimeCellState::Activated, {Query},
147
/*bExactState=*/
false
))
148
{
149
return
ERejectReason::NotStreamed
;
150
}
151
}
152
}
153
154
const
FNavDataConfig &Config = NavData.GetConfig();
155
const
float
Radius =
156
Config.AgentRadius > 0.0f ? Config.AgentRadius : FallbackWalkerRadius;
157
const
float
Height =
158
Config.AgentHeight > 0.0f ? Config.AgentHeight : FallbackWalkerHeight;
159
const
float
StepHeight = Config.HasStepHeightOverride()
160
? Config.AgentStepHeight : FallbackWalkerStepHeight;
161
const
float
HalfHeight = FMath::Max(Height * 0.5f, Radius);
162
163
// Static geometry only: vehicles and other walkers move away, a building
164
// does not.
165
FCollisionQueryParams Params(SCENE_QUERY_STAT(CarlaWalkerNavLocation),
false
);
166
const
FVector Center =
167
Location
+ FVector(0.0f, 0.0f, StepHeight + GroundClearance + HalfHeight);
168
const
bool
bOverlaps = World->OverlapAnyTestByObjectType(
169
Center, FQuat::Identity,
170
FCollisionObjectQueryParams(ECC_WorldStatic),
171
FCollisionShape::MakeCapsule(Radius, HalfHeight),
172
Params);
173
return
bOverlaps ?
ERejectReason::Overlap
:
ERejectReason::None
;
174
}
CarlaNavigationSubsystem.h
CarlaWalkerNavFilters.h
Carla.h
UCarlaNavigationSubsystem::CheckWalkerLocation
ERejectReason CheckWalkerLocation(const FVector &Location, const class ANavigationData &NavData) const
Checks a sampled nav point: on World Partition maps the cells around it must be activated (so a build...
Definition
CarlaNavigationSubsystem.cpp:129
UCarlaNavigationSubsystem::GetRandomNavLocation
bool GetRandomNavLocation(FVector &OutLocation) const
Random point on the loaded navmesh where a walker can stand, in world-local centimeters.
Definition
CarlaNavigationSubsystem.cpp:57
UCarlaNavigationSubsystem::ERejectReason
ERejectReason
Why a sampled nav point was rejected, for the Verbose log.
Definition
CarlaNavigationSubsystem.h:49
UCarlaNavigationSubsystem::ERejectReason::Overlap
@ Overlap
Definition
CarlaNavigationSubsystem.h:52
UCarlaNavigationSubsystem::ERejectReason::None
@ None
Definition
CarlaNavigationSubsystem.h:50
UCarlaNavigationSubsystem::ERejectReason::NotStreamed
@ NotStreamed
Definition
CarlaNavigationSubsystem.h:51
UCarlaNavigationSubsystem::HasServerSideNavigation
bool HasServerSideNavigation() const
True iff the current world has a RecastNavMesh with valid nav data: the signal for clients that walke...
Definition
CarlaNavigationSubsystem.cpp:44
Location
Definition
geom/Location.h:23
Generated by
1.18.0