2019-12-26 14:45:42 -05:00
// Copyright Epic Games, Inc. All Rights Reserved.
2018-12-12 11:25:29 -05:00
/*=============================================================================
GeometryCollectionActor . cpp : AGeometryCollectionActor methods .
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
# include "GeometryCollection/GeometryCollectionActor.h"
2022-11-09 20:53:21 -05:00
# include "Chaos/ChaosSolverActor.h"
2018-12-12 11:25:29 -05:00
# include "Chaos/Utilities.h"
# include "Chaos/Plane.h"
# include "Chaos/Box.h"
# include "Chaos/Sphere.h"
# include "Chaos/PerParticleGravity.h"
# include "Chaos/ImplicitObject.h"
# include "Engine/SkeletalMesh.h"
2022-10-26 12:57:32 -04:00
# include "Engine/World.h"
2018-12-12 11:25:29 -05:00
# include "GeometryCollection/GeometryCollectionAlgo.h"
# include "GeometryCollection/GeometryCollectionComponent.h"
2022-11-09 20:53:21 -05:00
# include "GeometryCollection/GeometryCollectionObject.h"
2018-12-12 11:25:29 -05:00
# include "GeometryCollection/GeometryCollectionUtility.h"
# include "Math/Box.h"
2022-11-09 20:53:21 -05:00
# include "Physics/Experimental/PhysScene_Chaos.h"
2018-12-12 11:25:29 -05:00
# include "Physics/PhysicsInterfaceCore.h"
2019-08-02 09:01:58 -04:00
# include "PhysicsSolver.h"
2020-04-08 10:59:25 -04:00
# include "GeometryCollection/GeometryCollectionDebugDrawComponent.h"
2018-12-12 11:25:29 -05:00
2022-09-24 13:57:58 -04:00
# include UE_INLINE_GENERATED_CPP_BY_NAME(GeometryCollectionActor)
2018-12-12 11:25:29 -05:00
DEFINE_LOG_CATEGORY_STATIC ( AGeometryCollectionActorLogging , Log , All ) ;
AGeometryCollectionActor : : AGeometryCollectionActor ( const FObjectInitializer & ObjectInitializer )
: Super ( ObjectInitializer )
{
UE_LOG ( AGeometryCollectionActorLogging , Verbose , TEXT ( " AGeometryCollectionActor::AGeometryCollectionActor() " ) ) ;
GeometryCollectionComponent = CreateDefaultSubobject < UGeometryCollectionComponent > ( TEXT ( " GeometryCollectionComponent0 " ) ) ;
RootComponent = GeometryCollectionComponent ;
2022-02-02 02:20:31 -05:00
GeometryCollectionDebugDrawComponent_DEPRECATED = nullptr ;
2018-12-12 11:25:29 -05:00
PrimaryActorTick . bCanEverTick = true ;
SetActorTickEnabled ( true ) ;
2020-09-01 14:07:48 -04:00
bReplicates = true ;
2022-05-04 12:47:38 -04:00
NetDormancy = DORM_Initial ;
2018-12-12 11:25:29 -05:00
}
void AGeometryCollectionActor : : Tick ( float DeltaTime )
{
UE_LOG ( AGeometryCollectionActorLogging , Verbose , TEXT ( " AGeometryCollectionActor::Tick() " ) ) ;
2021-08-10 20:17:46 -04:00
Super : : Tick ( DeltaTime ) ;
2018-12-12 11:25:29 -05:00
if ( GeometryCollectionComponent )
{
GeometryCollectionComponent - > SetRenderStateDirty ( ) ;
}
}
2019-08-02 09:01:58 -04:00
const Chaos : : FPhysicsSolver * GetSolver ( const AGeometryCollectionActor & GeomCollectionActor )
2018-12-12 11:25:29 -05:00
{
2019-06-08 17:15:34 -04:00
return GeomCollectionActor . GetGeometryCollectionComponent ( ) - > ChaosSolverActor ! = nullptr ? GeomCollectionActor . GetGeometryCollectionComponent ( ) - > ChaosSolverActor - > GetSolver ( ) : GeomCollectionActor . GetWorld ( ) - > PhysicsScene_Chaos - > GetSolver ( ) ;
2018-12-12 11:25:29 -05:00
}
2021-02-03 14:57:28 -04:00
bool LowLevelRaycastImp ( const Chaos : : FVec3 & Start , const Chaos : : FVec3 & Dir , float DeltaMag , const AGeometryCollectionActor & GeomCollectionActor , FHitResult & OutHit )
2018-12-12 11:25:29 -05:00
{
2021-04-13 10:51:11 -04:00
# if TODO_REIMPLEMENT_GET_RIGID_PARTICLES
2018-12-12 11:25:29 -05:00
using namespace Chaos ;
2020-03-09 13:22:54 -04:00
FPhysScene_Chaos * Scene = GeomCollectionActor . GetGeometryCollectionComponent ( ) - > GetInnerChaosScene ( ) ;
2018-12-12 11:25:29 -05:00
ensure ( Scene ) ;
2019-08-02 09:01:58 -04:00
const Chaos : : FPhysicsSolver * Solver = GetSolver ( GeomCollectionActor ) ;
2019-06-08 17:15:34 -04:00
if ( ensure ( Solver ) )
2018-12-12 11:25:29 -05:00
{
2019-06-08 17:15:34 -04:00
const TPBDRigidParticles < float , 3 > & Particles = Solver - > GetRigidParticles ( ) ; //todo(ocohen): should these just get passed in instead of hopping through scene?
2021-02-18 18:13:28 -04:00
FAABB3 RayBox ( Start , Start ) ;
2019-06-08 17:15:34 -04:00
RayBox . Thicken ( Dir * DeltaMag ) ;
const auto & PotentialIntersections = Solver - > GetSpatialAcceleration ( ) - > FindAllIntersections ( RayBox ) ;
Solver - > ReleaseSpatialAcceleration ( ) ;
2018-12-12 11:25:29 -05:00
2019-06-08 17:15:34 -04:00
for ( const auto RigidBodyIdx : PotentialIntersections )
2018-12-12 11:25:29 -05:00
{
2019-06-08 17:15:34 -04:00
const TRigidTransform < float , 3 > TM ( Particles . X ( RigidBodyIdx ) , Particles . R ( RigidBodyIdx ) ) ;
2021-02-03 14:57:28 -04:00
const FVec3 StartLocal = TM . InverseTransformPositionNoScale ( Start ) ;
const FVec3 DirLocal = TM . InverseTransformVectorNoScale ( Dir ) ;
const FVec3 EndLocal = StartLocal + DirLocal * DeltaMag ; //todo(ocohen): apeiron just undoes this later, we should fix the API
2018-12-12 11:25:29 -05:00
2019-10-31 17:28:08 -04:00
const FImplicitObject * Object = Particles . Geometry ( RigidBodyIdx ) . Get ( ) ; //todo(ocohen): can this ever be null?
2021-02-03 14:57:28 -04:00
Pair < FVec3 , bool > Result = Object - > FindClosestIntersection ( StartLocal , EndLocal , /*Thickness=*/ 0.f ) ;
2019-06-08 17:15:34 -04:00
if ( Result . Second ) //todo(ocohen): once we do more than just a bool we need to get the closest point
{
const float Distance = ( Result . First - StartLocal ) . Size ( ) ;
OutHit . Actor = const_cast < AGeometryCollectionActor * > ( & GeomCollectionActor ) ;
OutHit . Component = GeomCollectionActor . GetGeometryCollectionComponent ( ) ;
OutHit . bBlockingHit = true ;
OutHit . Distance = Distance ;
OutHit . Time = Distance / ( EndLocal - StartLocal ) . Size ( ) ;
OutHit . Location = TM . TransformPositionNoScale ( Result . First ) ;
OutHit . ImpactPoint = OutHit . Location ;
2021-02-03 14:57:28 -04:00
const FVec3 LocalNormal = Object - > Normal ( Result . First ) ;
2019-06-08 17:15:34 -04:00
OutHit . ImpactNormal = TM . TransformVectorNoScale ( LocalNormal ) ;
OutHit . Normal = OutHit . ImpactNormal ;
OutHit . Item = RigidBodyIdx ;
return true ;
}
2018-12-12 11:25:29 -05:00
}
}
2021-04-13 10:51:11 -04:00
# endif
2018-12-12 11:25:29 -05:00
return false ;
}
bool AGeometryCollectionActor : : RaycastSingle ( FVector Start , FVector End , FHitResult & OutHit ) const
{
if ( GeometryCollectionComponent )
{
OutHit = FHitResult ( ) ;
OutHit . TraceStart = Start ;
OutHit . TraceEnd = End ;
const FVector Delta = ( End - Start ) ;
const float DeltaMag = Delta . Size ( ) ;
if ( DeltaMag > KINDA_SMALL_NUMBER )
{
const FVector Dir = Delta / DeltaMag ;
return LowLevelRaycastImp ( Start , Dir , DeltaMag , * this , OutHit ) ;
}
}
return false ;
}
2019-06-08 17:15:34 -04:00
# if WITH_EDITOR
bool AGeometryCollectionActor : : GetReferencedContentObjects ( TArray < UObject * > & Objects ) const
{
Super : : GetReferencedContentObjects ( Objects ) ;
if ( GeometryCollectionComponent )
{
2019-06-20 12:19:58 -04:00
FGeometryCollectionEdit GeometryCollectionEdit = GeometryCollectionComponent - > EditRestCollection ( GeometryCollection : : EEditUpdate : : None ) ;
2019-06-08 17:15:34 -04:00
if ( UGeometryCollection * GeometryCollection = GeometryCollectionEdit . GetRestCollection ( ) )
{
Objects . Add ( GeometryCollection ) ;
}
}
return true ;
}
# endif
2022-09-24 13:57:58 -04:00