9 #include "Components/ActorComponent.h" 10 #include "Components/SkeletalMeshComponent.h" 14 #if defined(__clang__) 15 # pragma clang diagnostic push 16 # pragma clang diagnostic ignored "-Wdeprecated-register" 17 # pragma clang diagnostic ignored "-Wmisleading-indentation" 18 # pragma clang diagnostic ignored "-Wint-in-bool-context" 19 # pragma clang diagnostic ignored "-Wdeprecated-declarations" 20 # pragma clang diagnostic ignored "-Wshadow" 22 #include <Eigen/Dense> 23 #include <Eigen/Cholesky> 24 #include <Eigen/Eigenvalues> 25 #if defined(__clang__) 26 # pragma clang diagnostic pop 29 #include "SpringBasedVegetationComponent.generated.h" 31 USTRUCT(BlueprintType)
36 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"Skeleton Bone")
38 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
40 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
41 FVector CenterOfMass = FVector(0,0,0);
44 USTRUCT(BlueprintType)
49 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category =
"Skeleton Bone")
51 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
53 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
55 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
56 bool bIsStatic = false;
57 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
58 TArray<
int> ChildrenIds;
59 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
61 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
62 FRotator RestingAngles;
63 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
65 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
66 float SpringStrength = 1000.f;
67 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
68 FRotator AngularVelocity = FRotator(0,0,0);
69 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
70 FRotator AngularAcceleration = FRotator(0,0,0);
71 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
72 FTransform GlobalTransform;
73 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
74 FTransform GolbalInverseTransform;
75 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
76 TArray<FSkeletonBone> Bones;
77 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
78 FVector ExternalForces = FVector(0,0,0);
79 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
80 float CollisionForceProportionalFactor = 1.0f;
93 Eigen::Vector3d
Force = Eigen::Vector3d::Zero();
94 Eigen::Vector3d
Torque = Eigen::Vector3d::Zero();
105 USTRUCT(BlueprintType)
110 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"Skeleton Bone")
112 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
113 TArray<
int> EndJoints;
114 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
115 TArray<
int> EndToRootOrder;
116 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
117 TArray<
int> RootToEndOrder;
120 void ComputeChildrenJointsAndBones();
121 void ComputeEndJoints();
122 void AddForce(const FString& BoneName, const FVector&
Force);
123 void ClearExternalForces();
127 UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
128 class CARLA_API USpringBasedVegetationComponent :
public UActorComponent
134 USpringBasedVegetationComponent(
const FObjectInitializer& ObjectInitializer);
136 void BeginPlay()
override;
138 void EndPlay(
const EEndPlayReason::Type EndPlayReason)
override;
140 void TickComponent(
float DeltaTime,
141 enum ELevelTick TickType,
142 FActorComponentTickFunction * ThisTickFunction)
override;
144 void UpdateSkeletalMesh();
146 void UpdateGlobalTransform();
148 void GenerateCollisionCapsules();
150 void ResetComponent();
152 UFUNCTION(CallInEditor, Category =
"Spring Based Vegetation Component")
153 void ComputeSpringStrengthForBranches();
156 void OnCollisionEvent(
160 FVector NormalImpulse,
161 const FHitResult& Hit);
164 void OnBeginOverlapEvent(
168 int32 OtherBodyIndex,
170 const FHitResult& SweepResult);
173 void OnEndOverlapEvent(
177 int32 OtherBodyIndex);
179 UFUNCTION(CallInEditor, Category =
"Spring Based Vegetation Component")
180 void GenerateSkeletonHierarchy();
184 TMap<AActor*, TArray<UPrimitiveComponent*>> OverlappingActors;
186 UFUNCTION(BlueprintCallable)
187 void AddForce(
const FString& BoneName,
const FVector&
Force)
189 Skeleton.AddForce(BoneName, Force);
191 UPROPERTY(EditAnywhere, Category =
"Spring Based Vegetation Component")
192 TArray<FString> FixedJointsList = {
"joint1"};
194 UPROPERTY(EditAnywhere, Category =
"Spring Based Vegetation Component")
195 TArray<FString> DebugJointsToSimulate = {};
199 void ComputePerJointProperties(
200 std::vector<FJointProperties>& JointLocalPropertiesList,
201 std::vector<FJointProperties>& JointPropertiesList);
202 void ComputeCompositeBodyContribution(
203 std::vector<FJointProperties>& JointLocalPropertiesList,
204 std::vector<FJointProperties>& JointPropertiesList);
205 void ComputeFictitiousForces(
206 std::vector<FJointProperties>& JointLocalPropertiesList,
207 std::vector<FJointProperties>& JointPropertiesList);
208 void ResolveContactsAndCollisions(
209 std::vector<FJointProperties>& JointLocalPropertiesList,
210 std::vector<FJointProperties>& JointPropertiesList);
211 void SolveEquationOfMotion(std::vector<FJointProperties>& JointPropertiesList,
float DeltaTime);
213 std::vector<FJointCollision> JointCollisionList;
216 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"Spring Based Vegetation Component")
217 USkeletalMeshComponent* SkeletalMesh;
219 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"Spring Based Vegetation Component")
220 TArray<UPrimitiveComponent*> BoneCapsules;
222 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"Spring Based Vegetation Component")
223 TMap<UPrimitiveComponent*, int> CapsuleToJointId;
225 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"Spring Based Vegetation Component")
228 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"Spring Based Vegetation Component")
231 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"Spring Based Vegetation Component")
232 FVector Gravity = FVector(0,0,-1);
234 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"Spring Based Vegetation Component")
235 float BaseSpringStrength = 10000.f;
237 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"Spring Based Vegetation Component")
238 float MinSpringStrength = 2000.f;
240 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"Spring Based Vegetation Component")
241 float HorizontalFallof = 0.1f;
243 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"Spring Based Vegetation Component")
244 float VerticalFallof = 0.1f;
246 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"Spring Based Vegetation Component")
247 float RestFactor = 0.5f;
249 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"Spring Based Vegetation Component")
250 float DeltaTimeOverride = -1.f;
252 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"Spring Based Vegetation Component")
253 float CollisionForceParameter = 10.f;
255 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"Spring Based Vegetation Component")
256 float CollisionForceMinVel = 1.f;
258 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"Spring Based Vegetation Component")
259 float ForceDistanceFalloffExponent = 1.f;
260 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"Spring Based Vegetation Component")
261 float ForceMaxDistance = 180.f;
262 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"Spring Based Vegetation Component")
263 float MinForceFactor = 0.01;
264 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"Spring Based Vegetation Component")
265 float LineTraceMaxDistance = 180.f;
266 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"Spring Based Vegetation Component")
267 float CapsuleRadius = 6.0f;
268 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"Spring Based Vegetation Component")
269 float MinBoneLength = 10.f;
270 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"Spring Based Vegetation Component")
271 FVector SpringStrengthMulFactor = FVector(1,1,1);
272 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"Spring Based Vegetation Component")
273 float VehicleCenterZOffset = 120.f;
278 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"Spring Based Vegetation Component")
281 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"Spring Based Vegetation Component")
282 bool DebugEnableVisualization {
false };
284 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"Spring Based Vegetation Component")
285 bool DebugEnableAllCollisions {
false };
287 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"Spring Based Vegetation Component")
288 bool bAutoComputeStrength =
true;
Eigen::Vector3d AngularAcceleration
Eigen::Vector3d LinearAcceleration
Eigen::Vector3d CenterOfMass
Eigen::Vector3d LocalAngularAcceleration
Eigen::Vector3d AngularVelocity
Eigen::Vector3d FictitiousTorque
Eigen::Matrix3d JointToGlobalMatrix
Eigen::Matrix3d InertiaTensor
Eigen::Vector3d LinearVelocity
geom::Transform Transform