CARLA
 
载入中...
搜索中...
未找到
FrictionTrigger.cpp
浏览该文件的文档.
1// Copyright (c) 2019 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB). This work is licensed under the terms of the MIT license.
3// For a copy, see <https://opensource.org/licenses/MIT>.
4
5#include "Carla.h"
6#include "FrictionTrigger.h"
8
9AFrictionTrigger::AFrictionTrigger(const FObjectInitializer &ObjectInitializer)
10 : Super(ObjectInitializer)
11{
12 RootComponent = ObjectInitializer.CreateDefaultSubobject<USceneComponent>(this, TEXT("SceneRootComponent"));
13 RootComponent->SetMobility(EComponentMobility::Static);
14
15 TriggerVolume = CreateDefaultSubobject<UBoxComponent>(TEXT("TriggerVolume"));
16 TriggerVolume->SetupAttachment(RootComponent);
17 TriggerVolume->SetHiddenInGame(true);
18 TriggerVolume->SetMobility(EComponentMobility::Static);
19 TriggerVolume->SetCollisionProfileName(FName("OverlapAll"));
20 TriggerVolume->SetGenerateOverlapEvents(true);
21}
22
24{
25 // Register delegate on begin overlap.
26 if (!TriggerVolume->OnComponentBeginOverlap.IsAlreadyBound(this, &AFrictionTrigger::OnTriggerBeginOverlap))
27 {
28 TriggerVolume->OnComponentBeginOverlap.AddDynamic(this, &AFrictionTrigger::OnTriggerBeginOverlap);
29 }
30
31 // Register delegate on end overlap.
32 if (!TriggerVolume->OnComponentEndOverlap.IsAlreadyBound(this, &AFrictionTrigger::OnTriggerEndOverlap))
33 {
34 TriggerVolume->OnComponentEndOverlap.AddDynamic(this, &AFrictionTrigger::OnTriggerEndOverlap);
35 }
36}
37
38void AFrictionTrigger::UpdateWheelsFriction(AActor *OtherActor, TArray<float>& NewFriction)
39{
40 ACarlaWheeledVehicle *Vehicle = Cast<ACarlaWheeledVehicle>(OtherActor);
41 if (Vehicle != nullptr)
42 Vehicle->SetWheelsFrictionScale(NewFriction);
43}
44
46 UPrimitiveComponent * /*OverlappedComp*/,
47 AActor *OtherActor,
48 UPrimitiveComponent * /*OtherComp*/,
49 int32 /*OtherBodyIndex*/,
50 bool /*bFromSweep*/,
51 const FHitResult & /*SweepResult*/)
52{
53
54 // Save original friction of the wheels
55 ACarlaWheeledVehicle *Vehicle = Cast<ACarlaWheeledVehicle>(OtherActor);
56 if(Vehicle == nullptr)
57 return;
58
59 OldFrictionValues = Vehicle->GetWheelsFrictionScale();
60 TArray<float> TriggerFriction = {Friction, Friction, Friction, Friction};
61 UpdateWheelsFriction(OtherActor, TriggerFriction);
62}
63
65 UPrimitiveComponent * /*OverlappedComp*/,
66 AActor *OtherActor,
67 UPrimitiveComponent * /*OtherComp*/,
68 int32 /*OtherBodyIndex*/)
69{
70 // Set Back Default Friction Value
72
73 ACarlaWheeledVehicle *Vehicle = Cast<ACarlaWheeledVehicle>(OtherActor);
74 if(Vehicle == nullptr)
75 return;
76
77 TArray<float> CurrFriction = Vehicle->GetWheelsFrictionScale();
78}
79
80// Called when the game starts or when spawned
82{
83 Super::BeginPlay();
84 Init();
85}
86
87void AFrictionTrigger::EndPlay(const EEndPlayReason::Type EndPlayReason)
88{
89 // Deregister delegates
90 if (TriggerVolume->OnComponentBeginOverlap.IsAlreadyBound(this, &AFrictionTrigger::OnTriggerBeginOverlap))
91 {
92 TriggerVolume->OnComponentBeginOverlap.RemoveDynamic(this, &AFrictionTrigger::OnTriggerBeginOverlap);
93 }
94
95 if (TriggerVolume->OnComponentEndOverlap.IsAlreadyBound(this, &AFrictionTrigger::OnTriggerEndOverlap))
96 {
97 TriggerVolume->OnComponentEndOverlap.RemoveDynamic(this, &AFrictionTrigger::OnTriggerEndOverlap);
98 }
99
100 Super::EndPlay(EndPlayReason);
101}
102
103// Called every frame
104void AFrictionTrigger::Tick(float DeltaTime)
105{
106 Super::Tick(DeltaTime);
107}
Base class for CARLA wheeled vehicles.
virtual void Tick(float DeltaTime) override
virtual void EndPlay(EEndPlayReason::Type EndPlayReason) override
void OnTriggerBeginOverlap(UPrimitiveComponent *OverlappedComp, AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult)
AFrictionTrigger(const FObjectInitializer &ObjectInitializer)
UBoxComponent * TriggerVolume
virtual void BeginPlay() override
void UpdateWheelsFriction(AActor *OtherActor, TArray< float > &NewFriction)
void OnTriggerEndOverlap(UPrimitiveComponent *OverlappedComp, AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex)
TArray< float > OldFrictionValues