2020-09-01 14:07:48 -04:00
|
|
|
// Copyright Epic Games, Inc. All Rights Reserved.
|
|
|
|
|
|
|
|
|
|
#include "Physics/PhysicsDataCollection.h"
|
|
|
|
|
#include "Physics/CollisionGeometryConversion.h"
|
|
|
|
|
|
2021-02-25 05:39:27 -04:00
|
|
|
#include "Engine/StaticMesh.h"
|
|
|
|
|
#include "Components/StaticMeshComponent.h"
|
|
|
|
|
#include "PhysicsEngine/BodySetup.h"
|
2020-09-01 14:07:48 -04:00
|
|
|
|
2021-03-09 19:33:56 -04:00
|
|
|
using namespace UE::Geometry;
|
2020-09-01 14:07:48 -04:00
|
|
|
|
|
|
|
|
void FPhysicsDataCollection::InitializeFromComponent(const UActorComponent* Component, bool bInitializeAggGeom)
|
|
|
|
|
{
|
2021-06-02 15:58:00 -04:00
|
|
|
const UStaticMeshComponent* StaticMeshComponent = Cast<UStaticMeshComponent>(Component);
|
2021-02-25 05:39:27 -04:00
|
|
|
if (ensure(StaticMeshComponent))
|
2020-11-17 22:32:38 -04:00
|
|
|
{
|
2021-02-25 05:39:27 -04:00
|
|
|
const UStaticMesh* StaticMesh = StaticMeshComponent->GetStaticMesh();
|
|
|
|
|
if (ensure(StaticMesh))
|
|
|
|
|
{
|
|
|
|
|
SourceComponent = StaticMeshComponent;
|
|
|
|
|
SourceStaticMesh = StaticMesh;
|
|
|
|
|
BodySetup = StaticMesh->GetBodySetup();
|
|
|
|
|
|
|
|
|
|
ExternalScale3D = FVector(1.f, 1.f, 1.f);
|
|
|
|
|
|
|
|
|
|
if (bInitializeAggGeom)
|
|
|
|
|
{
|
|
|
|
|
AggGeom = BodySetup->AggGeom;
|
|
|
|
|
UE::Geometry::GetShapeSet(AggGeom, Geometry);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-17 22:32:38 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void FPhysicsDataCollection::InitializeFromStaticMesh(const UStaticMesh* StaticMesh, bool bInitializeAggGeom)
|
|
|
|
|
{
|
2021-02-25 05:39:27 -04:00
|
|
|
if (ensure(StaticMesh))
|
2020-09-01 14:07:48 -04:00
|
|
|
{
|
2021-02-25 05:39:27 -04:00
|
|
|
SourceStaticMesh = StaticMesh;
|
|
|
|
|
BodySetup = StaticMesh->GetBodySetup();
|
|
|
|
|
|
|
|
|
|
ExternalScale3D = FVector(1.f, 1.f, 1.f);
|
|
|
|
|
|
|
|
|
|
if (bInitializeAggGeom)
|
|
|
|
|
{
|
|
|
|
|
AggGeom = BodySetup->AggGeom;
|
|
|
|
|
UE::Geometry::GetShapeSet(AggGeom, Geometry);
|
|
|
|
|
}
|
2020-09-01 14:07:48 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void FPhysicsDataCollection::InitializeFromExisting(const FPhysicsDataCollection& Other)
|
|
|
|
|
{
|
|
|
|
|
SourceComponent = Other.SourceComponent;
|
2020-11-17 22:32:38 -04:00
|
|
|
SourceStaticMesh = Other.SourceStaticMesh;
|
2020-09-01 14:07:48 -04:00
|
|
|
BodySetup = Other.BodySetup;
|
|
|
|
|
|
|
|
|
|
ExternalScale3D = Other.ExternalScale3D;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void FPhysicsDataCollection::CopyGeometryFromExisting(const FPhysicsDataCollection& Other)
|
|
|
|
|
{
|
|
|
|
|
Geometry = Other.Geometry;
|
|
|
|
|
AggGeom = Other.AggGeom;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void FPhysicsDataCollection::ClearAggregate()
|
|
|
|
|
{
|
|
|
|
|
AggGeom = FKAggregateGeom();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FPhysicsDataCollection::CopyGeometryToAggregate()
|
|
|
|
|
{
|
|
|
|
|
for (FBoxShape3d& BoxGeom : Geometry.Boxes)
|
|
|
|
|
{
|
|
|
|
|
FKBoxElem Element;
|
|
|
|
|
UE::Geometry::GetFKElement(BoxGeom.Box, Element);
|
|
|
|
|
AggGeom.BoxElems.Add(Element);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (FSphereShape3d& SphereGeom : Geometry.Spheres)
|
|
|
|
|
{
|
|
|
|
|
FKSphereElem Element;
|
|
|
|
|
UE::Geometry::GetFKElement(SphereGeom.Sphere, Element);
|
|
|
|
|
AggGeom.SphereElems.Add(Element);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (FCapsuleShape3d& CapsuleGeom : Geometry.Capsules)
|
|
|
|
|
{
|
|
|
|
|
FKSphylElem Element;
|
|
|
|
|
UE::Geometry::GetFKElement(CapsuleGeom.Capsule, Element);
|
|
|
|
|
AggGeom.SphylElems.Add(Element);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (FConvexShape3d& ConvexGeom : Geometry.Convexes)
|
|
|
|
|
{
|
|
|
|
|
FKConvexElem Element;
|
|
|
|
|
UE::Geometry::GetFKElement(ConvexGeom.Mesh, Element);
|
2021-01-21 16:22:06 -04:00
|
|
|
|
2021-02-22 17:00:53 -04:00
|
|
|
#if !WITH_CHAOS
|
2021-01-21 16:22:06 -04:00
|
|
|
// Chaos will compute the IndexData itself on the call to FKConvexElem::UpdateElemBox() in ::GetFKElement() above.
|
|
|
|
|
// PhysX will not, so initialize that data with the mesh triangles.
|
|
|
|
|
// (This code should go into ::GetFKElement but cannot because it needs to be added in a hotfix)
|
|
|
|
|
for (FIndex3i Triangle : ConvexGeom.Mesh.TrianglesItr())
|
|
|
|
|
{
|
|
|
|
|
Element.IndexData.Add(Triangle.A);
|
|
|
|
|
Element.IndexData.Add(Triangle.B);
|
|
|
|
|
Element.IndexData.Add(Triangle.C);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-09-01 14:07:48 -04:00
|
|
|
AggGeom.ConvexElems.Add(Element);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|