9#include <util/ue-header-guard-begin.h>
10#include "Components/ActorComponent.h"
11#include "Components/SkeletalMeshComponent.h"
12#include <util/ue-header-guard-end.h>
16#include "SpringBasedVegetationComponent.generated.h"
25 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"Skeleton Bone")
27 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
29 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
38 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category =
"Skeleton Bone")
40 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
42 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
44 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
45 bool bIsStatic = false;
46 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
47 TArray<
int> ChildrenIds;
48 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
49 FTransform Transform = FTransform::Identity;
50 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
51 FRotator RestingAngles = FRotator::ZeroRotator;
52 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
54 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
55 float SpringStrength = 1000.f;
56 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
58 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
60 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
61 FTransform GlobalTransform = FTransform::Identity;
62 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
63 FTransform GlobalInverseTransform = FTransform::Identity;
64 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
66 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
67 FVector ExternalForces = FVector::ZeroVector;
68 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
69 float CollisionForceProportionalFactor = 1.0f;
83 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"Skeleton Bone")
85 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
86 TArray<
int> EndJoints;
87 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
88 TArray<
int> EndToRootOrder;
89 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
90 TArray<
int> RootToEndOrder;
93 void ComputeChildrenJointsAndBones();
94 void ComputeEndJoints();
95 void AddForce(const FString& BoneName, const FVector&
Force);
96 void ClearExternalForces();
100UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
101class CARLA_API USpringBasedVegetationComponent :
public UActorComponent
107 USpringBasedVegetationComponent(const FObjectInitializer& ObjectInitializer);
109 void BeginPlay() override;
111 void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
113 void TickComponent(
float DeltaTime,
114 enum ELevelTick TickType,
115 FActorComponentTickFunction * ThisTickFunction) override;
117 void UpdateSkeletalMesh();
119 void UpdateGlobalTransform();
121 void GenerateCollisionCapsules();
123 void ResetComponent();
125 UFUNCTION(CallInEditor, Category = "Spring Based Vegetation Component")
126 void ComputeSpringStrengthForBranches();
129 void OnCollisionEvent(
130 UPrimitiveComponent* HitComponent,
132 UPrimitiveComponent* OtherComponent,
133 FVector NormalImpulse,
134 const FHitResult& Hit);
137 void OnBeginOverlapEvent(
138 UPrimitiveComponent* OverlapComponent,
140 UPrimitiveComponent* OtherComponent,
141 int32 OtherBodyIndex,
143 const FHitResult& SweepResult);
146 void OnEndOverlapEvent(
147 UPrimitiveComponent* OverlapComponent,
149 UPrimitiveComponent* OtherComponent,
150 int32 OtherBodyIndex);
152 UFUNCTION(CallInEditor, Category = "Spring Based Vegetation Component")
153 void GenerateSkeletonHierarchy();
158 TMap<
AActor*, TArray<UPrimitiveComponent*>> OverlappingActors;
160 UFUNCTION(BlueprintCallable)
161 void AddForce(const FString& BoneName, const FVector& Force)
163 Skeleton.AddForce(BoneName, Force);
165 UPROPERTY(EditAnywhere, Category =
"Spring Based Vegetation Component")
166 TArray<FString> FixedJointsList = {
"joint1"};
168 UPROPERTY(EditAnywhere, Category =
"Spring Based Vegetation Component")
169 TArray<FString> DebugJointsToSimulate = {};
173 void ComputePerJointProperties(
174 std::vector<FJointProperties>& JointLocalPropertiesList,
175 std::vector<FJointProperties>& JointPropertiesList);
176 void ComputeCompositeBodyContribution(
177 std::vector<FJointProperties>& JointLocalPropertiesList,
178 std::vector<FJointProperties>& JointPropertiesList);
179 void ComputeFictitiousForces(
180 std::vector<FJointProperties>& JointLocalPropertiesList,
181 std::vector<FJointProperties>& JointPropertiesList);
182 void ResolveContactsAndCollisions(
183 std::vector<FJointProperties>& JointLocalPropertiesList,
184 std::vector<FJointProperties>& JointPropertiesList);
185 void SolveEquationOfMotion(std::vector<FJointProperties>& JointPropertiesList,
float DeltaTime);
187 std::vector<FJointCollision> JointCollisionList;
190 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"Spring Based Vegetation Component")
191 TObjectPtr<USkeletalMeshComponent> SkeletalMesh;
193 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
194 TArray<TObjectPtr<UPrimitiveComponent>> BoneCapsules;
196 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
197 TMap<TObjectPtr<UPrimitiveComponent>,
int> CapsuleToJointId;
199 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
202 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
205 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
206 FVector Gravity = FVector(0,0,-1);
208 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
209 float BaseSpringStrength = 10000.f;
211 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
212 float MinSpringStrength = 2000.f;
214 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
215 float HorizontalFallof = 0.1f;
217 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
218 float VerticalFallof = 0.1f;
220 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
221 float RestFactor = 0.5f;
223 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
224 float DeltaTimeOverride = -1.f;
226 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
227 float CollisionForceParameter = 10.f;
229 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
230 float CollisionForceMinVel = 1.f;
232 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
233 float ForceDistanceFalloffExponent = 1.f;
234 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
235 float ForceMaxDistance = 180.f;
236 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
237 float MinForceFactor = 0.01;
238 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
239 float LineTraceMaxDistance = 180.f;
240 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
241 float CapsuleRadius = 6.0f;
242 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
243 float MinBoneLength = 10.f;
244 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
245 FVector SpringStrengthMulFactor = FVector(1,1,1);
246 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
247 float VehicleCenterZOffset = 120.f;
252 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
255 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
256 bool DebugEnableVisualization {
false };
258 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"Spring Based Vegetation Component")
259 bool DebugEnableAllCollisions {
false };
261 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"Spring Based Vegetation Component")
262 bool bAutoComputeStrength = true;
Eigen::Vector3d AngularVelocity
Eigen::Vector3d AngularAcceleration
Eigen::Vector3d CenterOfMass