Added collision detection for chrono vehicles.

This commit is contained in:
Axel 2021-03-04 12:58:49 +01:00 committed by bernat
parent 3a5b8c0620
commit 5da83fbfb6
4 changed files with 151 additions and 4 deletions

View File

@ -62,3 +62,29 @@ void UBaseCarlaMovementComponent::DisableUE4VehiclePhysics()
Bone->SetInstanceSimulatePhysics(false);
}
}
void UBaseCarlaMovementComponent::EnableUE4VehiclePhysics(bool bResetVelocity)
{
FVector CurrentVelocity(0, 0, 0);
if (!bResetVelocity)
{
CurrentVelocity = GetVelocity();
}
CarlaVehicle->GetMesh()->SetPhysicsLinearVelocity(CurrentVelocity, false, "Vehicle_Base");
CarlaVehicle->GetVehicleMovementComponent()->SetComponentTickEnabled(true);
CarlaVehicle->GetVehicleMovementComponent()->Activate();
CarlaVehicle->GetMesh()->PhysicsTransformUpdateMode = EPhysicsTransformUpdateMode::SimulationUpatesComponentTransform;
auto * Bone = CarlaVehicle->GetMesh()->GetBodyInstance(NAME_None);
if (Bone)
{
Bone->SetInstanceSimulatePhysics(true);
}
else
{
carla::log_warning("No bone with name");
}
CarlaVehicle->GetMesh()->SetCollisionResponseToChannel(ECollisionChannel::ECC_WorldStatic, ECollisionResponse::ECR_Block);
CarlaVehicle->GetMesh()->SetCollisionProfileName("Vehicle");
CarlaVehicle->RestoreVehiclePhysicsControl();
}

View File

@ -35,5 +35,9 @@ public:
virtual float GetVehicleForwardSpeed() const;
protected:
void DisableUE4VehiclePhysics();
void EnableUE4VehiclePhysics(bool bResetVelocity = true);
};

View File

@ -148,6 +148,21 @@ void UChronoMovementComponent::BeginPlay()
Sys.SetSolverMaxIterations(150);
Sys.SetMaxPenetrationRecoverySpeed(4.0);
InitializeChronoVehicle();
// Create the terrain
Terrain = chrono_types::make_shared<UERayCastTerrain>(CarlaVehicle, Vehicle.get());
CarlaVehicle->OnActorHit.AddDynamic(
this, &UChronoMovementComponent::OnVehicleHit);
CarlaVehicle->GetMesh()->OnComponentBeginOverlap.AddDynamic(
this, &UChronoMovementComponent::OnVehicleOverlap);
CarlaVehicle->GetMesh()->SetCollisionResponseToChannel(
ECollisionChannel::ECC_WorldStatic, ECollisionResponse::ECR_Overlap);
}
void UChronoMovementComponent::InitializeChronoVehicle()
{
// Initial location with small offset to prevent falling through the ground
FVector VehicleLocation = CarlaVehicle->GetActorLocation() + FVector(0,0,25);
FQuat VehicleRotation = CarlaVehicle->GetActorRotation().Quaternion();
@ -195,9 +210,6 @@ void UChronoMovementComponent::BeginPlay()
Vehicle->InitializeTire(tire, wheel, VisualizationType::MESH);
}
}
// Create the terrain
Terrain = chrono_types::make_shared<UERayCastTerrain>(CarlaVehicle, Vehicle.get());
}
void UChronoMovementComponent::ProcessControl(FVehicleControl &Control)
@ -311,4 +323,80 @@ float UChronoMovementComponent::GetVehicleForwardSpeed() const
}
return 0.f;
}
void UChronoMovementComponent::SynchronizeActorChronoTransform()
{
}
void UChronoMovementComponent::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
if(!CarlaVehicle)
{
return;
}
// reset callbacks to react to collisions
CarlaVehicle->OnActorHit.RemoveDynamic(
this, &UChronoMovementComponent::OnVehicleHit);
CarlaVehicle->GetMesh()->OnComponentBeginOverlap.RemoveDynamic(
this, &UChronoMovementComponent::OnVehicleOverlap);
CarlaVehicle->GetMesh()->SetCollisionResponseToChannel(
ECollisionChannel::ECC_WorldStatic, ECollisionResponse::ECR_Block);
}
#endif
void UChronoMovementComponent::DisableChronoPhysics(float Duration)
{
this->SetComponentTickEnabled(false);
EnableUE4VehiclePhysics(false);
CarlaVehicle->OnActorHit.RemoveDynamic(this, &UChronoMovementComponent::OnVehicleHit);
CarlaVehicle->GetMesh()->OnComponentBeginOverlap.RemoveDynamic(
this, &UChronoMovementComponent::OnVehicleOverlap);
CarlaVehicle->GetMesh()->SetCollisionResponseToChannel(
ECollisionChannel::ECC_WorldStatic, ECollisionResponse::ECR_Block);
// FTimerHandle TimerHandler;
// GetWorld()->GetTimerManager().
// SetTimer(TimerHandler, this, &UChronoMovementComponent::ResetChronoPhysics, Duration);
UDefaultMovementComponent::CreateDefaultMovementComponent(CarlaVehicle);
carla::log_warning("DisableChronoPhysics");
}
void UChronoMovementComponent::OnVehicleHit(AActor *Actor,
AActor *OtherActor,
FVector NormalImpulse,
const FHitResult &Hit)
{
DisableChronoPhysics(CollisionBehaviorTime);
}
// On car mesh overlap, only works when carsim is enabled
// (this event triggers when overlapping with static environment)
void UChronoMovementComponent::OnVehicleOverlap(
UPrimitiveComponent* OverlappedComponent,
AActor* OtherActor,
UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex,
bool bFromSweep,
const FHitResult & SweepResult)
{
if (OtherComp->GetCollisionResponseToChannel(
ECollisionChannel::ECC_WorldDynamic) ==
ECollisionResponse::ECR_Block)
{
DisableChronoPhysics(CollisionBehaviorTime);
}
}
void UChronoMovementComponent::ResetChronoPhysics()
{
DisableUE4VehiclePhysics();
InitializeChronoVehicle();
this->SetComponentTickEnabled(true);
carla::log_warning("ResetChronoPhysics");
CarlaVehicle->OnActorHit.AddDynamic(
this, &UChronoMovementComponent::OnVehicleHit);
CarlaVehicle->GetMesh()->OnComponentBeginOverlap.AddDynamic(
this, &UChronoMovementComponent::OnVehicleOverlap);
CarlaVehicle->GetMesh()->SetCollisionResponseToChannel(
ECollisionChannel::ECC_WorldStatic, ECollisionResponse::ECR_Overlap);
}

View File

@ -46,7 +46,6 @@ class CARLA_API UChronoMovementComponent : public UBaseCarlaMovementComponent
#ifdef WITH_CHRONO
chrono::ChSystemNSC Sys;
// chrono::vehicle::hmmwv::HMMWV_Full my_hmmwv;
std::shared_ptr<chrono::vehicle::WheeledVehicle> Vehicle;
std::shared_ptr<UERayCastTerrain> Terrain;
#endif
@ -74,6 +73,8 @@ public:
#ifdef WITH_CHRONO
virtual void BeginPlay() override;
void InitializeChronoVehicle();
void ProcessControl(FVehicleControl &Control) override;
void TickComponent(float DeltaTime,
@ -87,5 +88,33 @@ public:
virtual int32 GetVehicleCurrentGear() const override;
virtual float GetVehicleForwardSpeed() const override;
void SynchronizeActorChronoTransform();
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
#endif
private:
void DisableChronoPhysics(float Duration);
UFUNCTION()
void OnVehicleHit(AActor *Actor,
AActor *OtherActor,
FVector NormalImpulse,
const FHitResult &Hit);
// On car mesh overlap, only works when carsim is enabled
// (this event triggers when overlapping with static environment)
UFUNCTION()
void OnVehicleOverlap(UPrimitiveComponent* OverlappedComponent,
AActor* OtherActor,
UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex,
bool bFromSweep,
const FHitResult & SweepResult);
UFUNCTION()
void ResetChronoPhysics();
const float CollisionBehaviorTime = 0.1;
};