diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vegetation/BaseVegetationActor.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vegetation/BaseVegetationActor.cpp index bc61a585c..6e4f0bcb3 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vegetation/BaseVegetationActor.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vegetation/BaseVegetationActor.cpp @@ -10,8 +10,8 @@ void ABaseVegetationActor::BeginPlay() { + TRACE_CPUPROFILER_EVENT_SCOPE(ABaseVegetationActor::BeginPlay); Super::BeginPlay(); - SetParametersToComponent(); } void ABaseVegetationActor::GetParametersFromComponent() @@ -89,3 +89,20 @@ void ABaseVegetationActor::SetParametersToComponent() SpringComponent->bAutoComputeStrength = SpringParameters.bAutoComputeStrength; } + +void ABaseVegetationActor::UpdateSkeletonAndParameters() +{ + UActorComponent* Component = + GetComponentByClass(USpringBasedVegetationComponent::StaticClass()); + USpringBasedVegetationComponent* SpringComponent = + Cast(Component); + if (!SpringComponent) + { + UE_LOG(LogCarla, Error, TEXT("ABaseVegetationActor::UpdateSkeletonAndParameters Component not found")); + return; + } + SetParametersToComponent(); + SpringComponent->GenerateSkeletonHierarchy(); + SpringComponent->ComputeSpringStrengthForBranches(); + GetParametersFromComponent(); +} diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vegetation/BaseVegetationActor.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vegetation/BaseVegetationActor.h index 82f421a55..a0f8c4660 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vegetation/BaseVegetationActor.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vegetation/BaseVegetationActor.h @@ -82,14 +82,16 @@ public: virtual void BeginPlay() override; - UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Skeleton Bone") + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation") FSpringBasedVegetationParameters SpringParameters; - UFUNCTION(BlueprintCallable, CallInEditor) + UFUNCTION(BlueprintCallable, CallInEditor, Category = "Spring Based Vegetation") void GetParametersFromComponent(); - UFUNCTION(BlueprintCallable) + UFUNCTION(BlueprintCallable, CallInEditor, Category = "Spring Based Vegetation") void SetParametersToComponent(); + UFUNCTION(BlueprintCallable, CallInEditor, Category = "Spring Based Vegetation") + void UpdateSkeletonAndParameters(); }; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vegetation/SpringBasedVegetationComponent.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vegetation/SpringBasedVegetationComponent.cpp index 0e86d11e9..f30f543bf 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vegetation/SpringBasedVegetationComponent.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vegetation/SpringBasedVegetationComponent.cpp @@ -10,6 +10,7 @@ #include "Components/CapsuleComponent.h" #include "DrawDebugHelpers.h" #include "Kismet/KismetMathLibrary.h" +#include "BaseVegetationActor.h" #include #include #include "carla/rpc/String.h" @@ -322,6 +323,12 @@ void USpringBasedVegetationComponent::BeginPlay() return; } + ABaseVegetationActor* BaseVegetation = Cast(GetOwner()); + if(BaseVegetation) + { + BaseVegetation->SetParametersToComponent(); + } + // set callbacks SkeletalMesh->OnComponentHit.AddDynamic(this, &USpringBasedVegetationComponent::OnCollisionEvent); @@ -678,7 +685,7 @@ void USpringBasedVegetationComponent::ResolveContactsAndCollisions( const Eigen::Vector3d RepulsionForce = SpringTorque.cross(JointCapsuleVector) * JointCapsuleVector.squaredNorm(); FVector RepulsionForceUE = -ToUnrealVector(RepulsionForce) * 100.f; - Primitive->AddForceAtLocation(RepulsionForceUE, CapsuleLocation); + Primitive->AddForceAtLocation(RepulsionForceUE, ClosestPointOnCollider); // force to repel geometry overlapping float ForceFactor = 1.f; @@ -693,9 +700,9 @@ void USpringBasedVegetationComponent::ResolveContactsAndCollisions( // const Eigen::Vector3d OverlappingForces = (ColliderPosition - CapsulePosition).normalized() * CollisionForceParameter * ForceFactor; // const Eigen::Vector3d OverlappingForces = (ColliderPosition - PointOnCapsulePosition).normalized() * CollisionForceParameter * ForceFactor; float Factor = 1.0f - ((JointCollision.Iteration / 100.0f) * RestFactor); - const Eigen::Vector3d OverlappingForces = (ColliderPosition - PointOnCapsulePosition).normalized() * CollisionForceParameter * ForceFactor * Factor; + const Eigen::Vector3d OverlappingForces = (ColliderPosition - PointOnCapsulePosition).normalized() * CollisionForceParameter * ForceFactor * Factor * Joint.CollisionForceProportionalFactor; Primitive->AddForceAtLocation(-ToUnrealVector(OverlappingForces) * 100.f, ClosestPointOnCollider); - CollisionTorque += (JointProperties.CenterOfMass - JointGlobalPosition).cross(CollisionImpulse + OverlappingForces); + CollisionTorque += (PointOnCapsulePosition - JointGlobalPosition).cross(CollisionImpulse + OverlappingForces); JointProperties.Torque += CollisionTorque; // COLLISION_LOG(Log, "Joint: %s \n ProjectedSpeed %f, ProportionalFactor %f \n RepulsionForce %s \n", *Joint.JointName,ProjectedSpeed,ProportionalFactor,*EigenToFString(RepulsionForce),*EigenToFString(CollisionTorque)); //UE_LOG(LogCarla, Display, TEXT("DistanceToCollider: %f, ForceFactor: %f"), DistanceToCollider, ForceFactor); @@ -714,12 +721,20 @@ void USpringBasedVegetationComponent::ResolveContactsAndCollisions( if (DebugEnableVisualization) { + static constexpr float DEBUG_SPHERE_SIZE = 5.0f; // drawing const FVector Start = Capsule->GetComponentLocation(); const FVector End = Primitive->GetComponentLocation(); const FColor LineColor(FColor::Green); DrawDebugLine(GetWorld(), Start, End, LineColor, false, 0.1f, 0.0f, 1.f); - DrawDebugLine(GetWorld(), CapsuleLocation, CapsuleLocation+RepulsionForceUE.GetSafeNormal()*5.f, FColor::Red, false, 0.1f, 0.0f, 1.f); + DrawDebugLine(GetWorld(), ClosestPointOnCollider, ClosestPointOnCollider+RepulsionForceUE.GetSafeNormal()*20.f, FColor::Red, false, 0.1f, 0.0f, 1.f); + FVector UEOverlapForces = ToUnrealVector(OverlappingForces)*100.f; + DrawDebugLine(GetWorld(), ClosestPointOnCapsule, ClosestPointOnCapsule+UEOverlapForces.GetSafeNormal()*20.f, FColor::Turquoise, false, 0.1f, 0.0f, 1.f); + FVector UECOM = ToUnrealVector(JointProperties.CenterOfMass )*100.f; + DrawDebugSphere(GetWorld(), UECOM, DEBUG_SPHERE_SIZE, 64, FColor::Emerald); + FVector UEJointPos = ToUnrealVector(JointGlobalPosition )*100.f; + DrawDebugSphere(GetWorld(), UEJointPos, DEBUG_SPHERE_SIZE, 64, FColor::Purple); + DrawDebugLine(GetWorld(), ClosestPointOnCapsule, UEJointPos, FColor::Cyan, false, 0.1f, 0.0f, 1.f); } } } diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vegetation/SpringBasedVegetationComponent.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vegetation/SpringBasedVegetationComponent.h index f8acd7c80..870d56762 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vegetation/SpringBasedVegetationComponent.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vegetation/SpringBasedVegetationComponent.h @@ -76,6 +76,8 @@ struct FSkeletonJoint TArray Bones; UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone") FVector ExternalForces = FVector(0,0,0); + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Skeleton Bone") + float CollisionForceProportionalFactor = 1.0f; }; struct FJointCollision diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vehicle/CustomTerrainPhysicsComponent.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vehicle/CustomTerrainPhysicsComponent.cpp index 89d1c245f..e2295b015 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vehicle/CustomTerrainPhysicsComponent.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vehicle/CustomTerrainPhysicsComponent.cpp @@ -1088,7 +1088,7 @@ void UCustomTerrainPhysicsComponent::BeginPlay() DrawDebugInfo = false; bUseDynamicModel = false; bDisableVehicleGravity = false; - NNVerbose = true; + NNVerbose = false; bUseImpulse = false; bUseMeanAcceleration = false; bShowForces = true; @@ -1098,7 +1098,7 @@ void UCustomTerrainPhysicsComponent::BeginPlay() ParticleForceMulFactor = 1.f; FloorHeight = 0.0; bDrawLoadedTiles = false; - bUseSoilType = false; + bUseSoilType = true; EffectMultiplayer = 200.0f; MinDisplacement = -10.0f; MaxDisplacement = 10.0f;