Vegetation fixes

This commit is contained in:
Axel 2022-11-09 15:20:57 +01:00 committed by bernat
parent a152863b11
commit d3dc199085
5 changed files with 46 additions and 10 deletions

View File

@ -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<USpringBasedVegetationComponent>(Component);
if (!SpringComponent)
{
UE_LOG(LogCarla, Error, TEXT("ABaseVegetationActor::UpdateSkeletonAndParameters Component not found"));
return;
}
SetParametersToComponent();
SpringComponent->GenerateSkeletonHierarchy();
SpringComponent->ComputeSpringStrengthForBranches();
GetParametersFromComponent();
}

View File

@ -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();
};

View File

@ -10,6 +10,7 @@
#include "Components/CapsuleComponent.h"
#include "DrawDebugHelpers.h"
#include "Kismet/KismetMathLibrary.h"
#include "BaseVegetationActor.h"
#include <unordered_set>
#include <vector>
#include "carla/rpc/String.h"
@ -322,6 +323,12 @@ void USpringBasedVegetationComponent::BeginPlay()
return;
}
ABaseVegetationActor* BaseVegetation = Cast<ABaseVegetationActor>(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);
}
}
}

View File

@ -76,6 +76,8 @@ struct FSkeletonJoint
TArray<FSkeletonBone> 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

View File

@ -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;