CARLA
Toggle main menu visibility
Loading...
Searching...
No Matches
Unreal
CarlaUnreal
Plugins
Carla
Source
Carla
Util
ActorAttacher.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/Util/ActorAttacher.h
"
8
#include "
Carla.h
"
9
#include "GameFramework/SpringArmComponent.h"
10
11
static
void
UActorAttacher_AttachActorsWithSpringArm
(
12
AActor
*Child,
13
AActor
*Parent)
14
{
15
auto
SpringArm
= NewObject<USpringArmComponent>(Parent);
16
17
// Child location negated to compensate for the spring arm rotation (rotated
18
// from the "other end" of the arm).
19
const
auto
ChildLocation = -Child->GetActorLocation();
20
Child->SetActorLocation(FVector::ZeroVector);
21
22
// Adding Z-offset to avoid colliding against the ground on bumpy terrain.
23
SpringArm
->TargetOffset = FVector(0.0f, 0.0f, 30.0f);
24
SpringArm
->bDoCollisionTest =
true
;
25
26
FRotator LookAt = FRotationMatrix::MakeFromX(ChildLocation).Rotator();
27
SpringArm
->SetRelativeRotation(LookAt);
28
SpringArm
->SetupAttachment(Parent->GetRootComponent());
29
30
SpringArm
->TargetArmLength = ChildLocation.Size();
31
SpringArm
->bEnableCameraRotationLag =
true
;
32
SpringArm
->CameraRotationLagSpeed = 8.0f;
33
34
SpringArm
->bInheritPitch =
false
;
35
SpringArm
->bInheritRoll =
false
;
36
SpringArm
->bInheritYaw =
true
;
37
38
SpringArm
->AttachToComponent(
39
Parent->GetRootComponent(),
40
FAttachmentTransformRules::KeepRelativeTransform);
41
SpringArm
->RegisterComponent();
42
43
auto
ChildComp = NewObject<UChildActorComponent>(Parent);
44
ChildComp->SetupAttachment(
45
SpringArm
,
46
USpringArmComponent::SocketName);
47
Child->AttachToComponent(
48
ChildComp,
49
FAttachmentTransformRules::KeepRelativeTransform);
50
ChildComp->RegisterComponent();
51
}
52
53
static
void
UActorAttacher_AttachActorsWithSpringArmGhost
(
54
AActor
*Child,
55
AActor
*Parent)
56
{
57
auto
SpringArm
= NewObject<USpringArmComponent>(Parent);
58
59
// Child location negated to compensate for the spring arm rotation (rotated
60
// from the "other end" of the arm).
61
const
auto
ChildLocation = -Child->GetActorLocation();
62
Child->SetActorLocation(FVector::ZeroVector);
63
64
// Adding Z-offset to avoid colliding against the ground on bumpy terrain.
65
SpringArm
->TargetOffset = FVector(0.0f, 0.0f, 0.0f);
66
SpringArm
->bDoCollisionTest =
false
;
67
68
FRotator LookAt = FRotationMatrix::MakeFromX(ChildLocation).Rotator();
69
SpringArm
->SetRelativeRotation(LookAt);
70
SpringArm
->SetupAttachment(Parent->GetRootComponent());
71
72
SpringArm
->TargetArmLength = ChildLocation.Size();
73
SpringArm
->bEnableCameraRotationLag =
true
;
74
SpringArm
->CameraRotationLagSpeed = 8.0f;
75
76
SpringArm
->bInheritPitch =
false
;
77
SpringArm
->bInheritRoll =
false
;
78
SpringArm
->bInheritYaw =
true
;
79
80
SpringArm
->AttachToComponent(
81
Parent->GetRootComponent(),
82
FAttachmentTransformRules::KeepRelativeTransform);
83
SpringArm
->RegisterComponent();
84
85
auto
ChildComp = NewObject<UChildActorComponent>(Parent);
86
ChildComp->SetupAttachment(
87
SpringArm
,
88
USpringArmComponent::SocketName);
89
Child->AttachToComponent(
90
ChildComp,
91
FAttachmentTransformRules::KeepRelativeTransform);
92
ChildComp->RegisterComponent();
93
}
94
95
void
UActorAttacher::AttachActors
(
96
AActor
*Child,
97
AActor
*Parent,
98
const
EAttachmentType
AttachmentType)
99
{
100
check(Child !=
nullptr
);
101
check(Parent !=
nullptr
);
102
103
switch
(AttachmentType)
104
{
105
case
EAttachmentType::Rigid
:
106
Child->AttachToActor(Parent, FAttachmentTransformRules::KeepRelativeTransform);
107
break
;
108
case
EAttachmentType::SpringArm
:
109
UActorAttacher_AttachActorsWithSpringArm
(Child, Parent);
110
break
;
111
case
EAttachmentType::SpringArmGhost
:
112
UActorAttacher_AttachActorsWithSpringArmGhost
(Child, Parent);
113
break
;
114
default
:
115
UE_LOG(LogCarla, Fatal, TEXT(
"Invalid attachment type"
));
116
}
117
118
Child->SetOwner(Parent);
119
}
UActorAttacher_AttachActorsWithSpringArmGhost
static void UActorAttacher_AttachActorsWithSpringArmGhost(AActor *Child, AActor *Parent)
Definition
ActorAttacher.cpp:53
UActorAttacher_AttachActorsWithSpringArm
static void UActorAttacher_AttachActorsWithSpringArm(AActor *Child, AActor *Parent)
Definition
ActorAttacher.cpp:11
ActorAttacher.h
EAttachmentType
EAttachmentType
Definition
ActorAttacher.h:23
EAttachmentType::SpringArm
@ SpringArm
Definition
ActorAttacher.h:26
EAttachmentType::SpringArmGhost
@ SpringArmGhost
Definition
ActorAttacher.h:27
EAttachmentType::Rigid
@ Rigid
Definition
ActorAttacher.h:25
Carla.h
AActor
UActorAttacher::AttachActors
static void AttachActors(AActor *Child, AActor *Parent, EAttachmentType AttachmentType)
Definition
ActorAttacher.cpp:95
Generated by
1.18.0