Added constraint handling capabilities for door animation

This commit is contained in:
Axel 2021-09-29 14:15:06 +02:00 committed by bernat
parent 9346d24b21
commit 62e9e8dc6a
2 changed files with 92 additions and 27 deletions

View File

@ -83,6 +83,25 @@ void ACarlaWheeledVehicle::BeginPlay()
UDefaultMovementComponent::CreateDefaultMovementComponent(this); UDefaultMovementComponent::CreateDefaultMovementComponent(this);
// Get constraint components and their initial transforms
DoorComponentsTransform.Empty();
FTransform ActorInverseTransform = GetActorTransform().Inverse();
for (UPhysicsConstraintComponent * Constraint: ConstraintsComponents)
{
UPrimitiveComponent* DoorComponent = Cast<UPrimitiveComponent>(GetDefaultSubobjectByName(Constraint->ComponentName1.ComponentName));
if(DoorComponent)
{
UE_LOG(LogCarla, Warning, TEXT("Door name: %s"), *(DoorComponent->GetName()));
FTransform ComponentWorldTransform = DoorComponent->GetComponentTransform();
FTransform RelativeTransform = ComponentWorldTransform * ActorInverseTransform;
DoorComponentsTransform.Add(DoorComponent, RelativeTransform);
}
else
{
UE_LOG(LogCarla, Error, TEXT("Missing component for constraint: %s"), *(Constraint->GetName()));
}
}
float FrictionScale = 3.5f; float FrictionScale = 3.5f;
UWheeledVehicleMovementComponent4W *Vehicle4W = Cast<UWheeledVehicleMovementComponent4W>( UWheeledVehicleMovementComponent4W *Vehicle4W = Cast<UWheeledVehicleMovementComponent4W>(
@ -579,6 +598,29 @@ void ACarlaWheeledVehicle::SetSimulatePhysics(bool enabled) {
GetWorld()->GetPhysicsScene()->GetPxScene()->unlockWrite(); GetWorld()->GetPhysicsScene()->GetPxScene()->unlockWrite();
bPhysicsEnabled = enabled; bPhysicsEnabled = enabled;
// Set simulate physics for doors
for (auto& ComponentTransform : DoorComponentsTransform)
{
UPrimitiveComponent* Component = ComponentTransform.Key;
Component->SetSimulatePhysics(bPhysicsEnabled);
}
if (bPhysicsEnabled)
{
FTransform ActorTransform = GetActorTransform();
// recreate physics constraints
for (auto& ComponentTransform : DoorComponentsTransform)
{
UPrimitiveComponent* Component = ComponentTransform.Key;
FTransform ComponentWorldTransform = ComponentTransform.Value * ActorTransform;
Component->SetWorldTransform(ComponentWorldTransform);
}
for (UPhysicsConstraintComponent* Constraint : ConstraintsComponents)
{
Constraint->InitComponentConstraint();
}
}
} }
FVector ACarlaWheeledVehicle::GetVelocity() const FVector ACarlaWheeledVehicle::GetVelocity() const
@ -592,49 +634,52 @@ void ACarlaWheeledVehicle::EndPlay(const EEndPlayReason::Type EndPlayReason)
} }
void ACarlaWheeledVehicle::OpenDoor(const EVehicleDoor DoorIdx) { void ACarlaWheeledVehicle::OpenDoor(const EVehicleDoor DoorIdx) {
// We check if the car has any door configured if (int(DoorIdx) >= ConstraintsComponents.Num() && DoorIdx != EVehicleDoor::All) {
if (DoorAnimMaxAngle.Num() == 0) {
UE_LOG(LogTemp, Warning, TEXT("The car has no doors configured."));
return;
}
// door exist
if (int(DoorIdx) > DoorAnimMaxAngle.Num() && DoorIdx != EVehicleDoor::All) {
UE_LOG(LogTemp, Warning, TEXT("This door is not configured for this car.")); UE_LOG(LogTemp, Warning, TEXT("This door is not configured for this car."));
return; return;
} }
if (DoorIdx == EVehicleDoor::All) { if (DoorIdx == EVehicleDoor::All) {
for (int i = 0; i < DoorAnimMaxAngle.Num(); i++) for (int i = 0; i < ConstraintsComponents.Num(); i++)
OpenDoorAnim(EVehicleDoor(i)); {
OpenDoorPhys(EVehicleDoor(i));
}
return; return;
} }
OpenDoorAnim(DoorIdx); OpenDoorPhys(DoorIdx);
} }
void ACarlaWheeledVehicle::CloseDoor(const EVehicleDoor DoorIdx) { void ACarlaWheeledVehicle::CloseDoor(const EVehicleDoor DoorIdx) {
// We check if the car has any door configured if (int(DoorIdx) >= ConstraintsComponents.Num() && DoorIdx != EVehicleDoor::All) {
if (DoorAnimMaxAngle.Num() == 0) {
UE_LOG(LogTemp, Warning, TEXT("The car has no doors configured."));
return;
}
// door exist
if (int(DoorIdx) > DoorAnimMaxAngle.Num() && DoorIdx != EVehicleDoor::All) {
UE_LOG(LogTemp, Warning, TEXT("This door is not configured for this car.")); UE_LOG(LogTemp, Warning, TEXT("This door is not configured for this car."));
return; return;
} }
if (DoorIdx == EVehicleDoor::All) { if (DoorIdx == EVehicleDoor::All) {
for (int i = 0; i < DoorAnimMaxAngle.Num(); i++) for (int i = 0; i < ConstraintsComponents.Num(); i++)
CloseDoorAnim(EVehicleDoor(i)); {
CloseDoorPhys(EVehicleDoor(i));
}
return; return;
} }
CloseDoorAnim(DoorIdx); CloseDoorPhys(DoorIdx);
}
void ACarlaWheeledVehicle::OpenDoorPhys(const EVehicleDoor DoorIdx)
{
UPhysicsConstraintComponent* Constraint = ConstraintsComponents[static_cast<int>(DoorIdx)];
float AngleLimit = Constraint->ConstraintInstance.GetAngularSwing1Limit();
Constraint->SetAngularOrientationTarget(FRotator(0, 0, AngleLimit));
Constraint->SetAngularDriveParams(DoorOpenStrength, 1.0, 0.0);
}
void ACarlaWheeledVehicle::CloseDoorPhys(const EVehicleDoor DoorIdx)
{
UPhysicsConstraintComponent* Constraint = ConstraintsComponents[static_cast<int>(DoorIdx)];
Constraint->SetAngularOrientationTarget(FRotator(0, 0, 0));
Constraint->SetAngularDriveParams(DoorCloseStrength, 1.0, 0.0);
} }
void ACarlaWheeledVehicle::OpenDoorAnim_Implementation(const EVehicleDoor DoorIdx) void ACarlaWheeledVehicle::OpenDoorAnim_Implementation(const EVehicleDoor DoorIdx)

View File

@ -16,6 +16,7 @@
#include "VehicleVelocityControl.h" #include "VehicleVelocityControl.h"
#include "WheeledVehicleMovementComponent4W.h" #include "WheeledVehicleMovementComponent4W.h"
#include "VehicleAnimInstance.h" #include "VehicleAnimInstance.h"
#include "PhysicsEngine/PhysicsConstraintComponent.h"
#include "MovementComponents/BaseCarlaMovementComponent.h" #include "MovementComponents/BaseCarlaMovementComponent.h"
#include "CoreMinimal.h" #include "CoreMinimal.h"
@ -154,6 +155,7 @@ public:
void ApplyVehiclePhysicsControl(const FVehiclePhysicsControl &PhysicsControl); void ApplyVehiclePhysicsControl(const FVehiclePhysicsControl &PhysicsControl);
UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
void SetSimulatePhysics(bool enabled); void SetSimulatePhysics(bool enabled);
void SetWheelCollision(UWheeledVehicleMovementComponent4W *Vehicle4W, const FVehiclePhysicsControl &PhysicsControl); void SetWheelCollision(UWheeledVehicleMovementComponent4W *Vehicle4W, const FVehiclePhysicsControl &PhysicsControl);
@ -270,6 +272,15 @@ protected:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Door Animation") UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Door Animation")
TArray<float> DoorAnimAlpha; TArray<float> DoorAnimAlpha;
UPROPERTY(Category="Door Animation", EditAnywhere, BlueprintReadWrite)
TArray<UPhysicsConstraintComponent*> ConstraintsComponents;
UPROPERTY(Category="Door Animation", EditAnywhere, BlueprintReadWrite)
float DoorOpenStrength = 100.0f;
UPROPERTY(Category="Door Animation", EditAnywhere, BlueprintReadWrite)
float DoorCloseStrength = 10000.0f;
private: private:
/// Current state of the vehicle controller (for debugging purposes). /// Current state of the vehicle controller (for debugging purposes).
@ -310,6 +321,12 @@ public:
UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable) UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
void CloseDoor(const EVehicleDoor DoorIdx); void CloseDoor(const EVehicleDoor DoorIdx);
UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
void OpenDoorPhys(const EVehicleDoor DoorIdx);
UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
void CloseDoorPhys(const EVehicleDoor DoorIdx);
UFUNCTION(BlueprintNativeEvent, Category = "CARLA Wheeled Vehicle") UFUNCTION(BlueprintNativeEvent, Category = "CARLA Wheeled Vehicle")
void OpenDoorAnim(const EVehicleDoor DoorIdx); void OpenDoorAnim(const EVehicleDoor DoorIdx);
@ -332,4 +349,7 @@ private:
UPROPERTY(Category="CARLA Wheeled Vehicle", VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) UPROPERTY(Category="CARLA Wheeled Vehicle", VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
UBaseCarlaMovementComponent * BaseMovementComponent = nullptr; UBaseCarlaMovementComponent * BaseMovementComponent = nullptr;
UPROPERTY(Category="CARLA Wheeled Vehicle", VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
TMap<UPrimitiveComponent*, FTransform> DoorComponentsTransform;
}; };