Skip to content

Commit 1595cec

Browse files
committed
Updates to fix Debug traces
1 parent 1dc9f19 commit 1595cec

File tree

6 files changed

+48
-17
lines changed

6 files changed

+48
-17
lines changed
2.75 KB
Binary file not shown.
Binary file not shown.
-238 Bytes
Binary file not shown.

Plugins/LocalPhysics/Source/LocalPhysics/Private/LocalPhysicsSimulation.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ uint32 FLocalSimulation:: CreateActor(PxRigidActor* RigidActor, const FTransform
218218
NewRigidBodyData->maxContactImpulse = RigidBody ? RigidBody->getMaxContactImpulse() : PX_MAX_F32;
219219

220220
NewActorHandle->ActorToBody = RigidBody ? P2UTransform(RigidBody->getCMassLocalPose()) : FTransform::Identity; //TODO: Why is this an unreal transform?
221+
//UE_LOG(LogTemp, Warning, L"ActorToBody: %s", *NewActorHandle->ActorToBody.ToHumanReadableString())
222+
NewActorHandle->ActorScale3D = WorldTM.GetScale3D();
221223
NewRigidBodyData->body2World = U2PTransform(NewActorHandle->ActorToBody * WorldTM);
222224

223225
NewRigidBodyData->linearDamping = RigidDynamic ? RigidDynamic->getLinearDamping() : 0.f;

Plugins/LocalPhysics/Source/LocalPhysics/Private/LocalSimulationVolume.cpp

+30-15
Original file line numberDiff line numberDiff line change
@@ -224,17 +224,19 @@ void ALocalSimulationVolume::UpdateMeshVisuals()
224224
LocalPhysics::FActorHandle& Handle = *MeshData->InHandle;
225225
//FTransform HandleTransform = Handle.GetWorldTransform();
226226
//HandleTransform.SetScale3D(Mesh.GetComponentTransform().GetScale3D());
227+
//const FTransform& BodyTransform = LocalSpace->GetComponentTransform().GetRelativeTransformReverse(Handle.GetWorldTransform());;
228+
const FTransform& BodyTransform = Handle.GetWorldTransform() * LocalSpace->GetComponentTransform();
229+
//UE_LOG(LocalSimulationLog, Warning, L"New transform: %s is %s", *Mesh.GetStaticMesh()->GetName(), *BodyTransform.ToHumanReadableString());
227230

228-
const FTransform& BodyTransform = (Handle.GetWorldTransform() * LocalSpace->ComponentToWorld);
229-
230231
switch (MeshData->InBodyType)
231232
{
232233
case ELocalPhysicsBodyType::Static:
233234
case ELocalPhysicsBodyType::Dynamic:
234235
{
235236
// update meshes back in 'world' space
236-
Mesh.SetWorldLocation(BodyTransform.GetLocation(), false, nullptr, ETeleportType::TeleportPhysics);
237-
Mesh.SetWorldRotation(BodyTransform.GetRotation().Rotator(), false, nullptr, ETeleportType::TeleportPhysics);
237+
Mesh.SetWorldTransform(BodyTransform, false, nullptr, ETeleportType::TeleportPhysics);
238+
//Mesh.SetWorldLocation(BodyTransform.GetLocation(), false, nullptr, ETeleportType::TeleportPhysics);
239+
//Mesh.SetWorldRotation(BodyTransform.GetRotation().Rotator(), false, nullptr, ETeleportType::TeleportPhysics);
238240
}
239241
break;
240242
case ELocalPhysicsBodyType::Kinematic:
@@ -247,9 +249,9 @@ void ALocalSimulationVolume::UpdateMeshVisuals()
247249

248250
// let's show everything in simulation.
249251
if (bShowDebugPhyics)
250-
{
251-
const FTransform& DebugTransform = (bDebugInWorldSpace ? BodyTransform : Handle.GetWorldTransform());
252-
UKismetSystemLibrary::DrawDebugBox(GetWorld(), DebugTransform.GetLocation(), Mesh.GetStaticMesh()->GetBounds().BoxExtent * Mesh.GetComponentTransform().GetScale3D(), DebugSimulatedColor, DebugTransform.Rotator(), DebugTick, DebugThickness);
252+
{ // temporary fix to show local transforms correctly.
253+
const FTransform& DebugTransform = (bDebugInWorldSpace ? FTransform(BodyTransform.Rotator(), Mesh.Bounds.Origin, BodyTransform.GetScale3D()) : FTransform(Handle.GetWorldTransform().Rotator(), FTransform(BodyTransform.Rotator(), Mesh.Bounds.Origin, BodyTransform.GetScale3D()).GetRelativeTransform(LocalSpace->ComponentToWorld).GetLocation(), Handle.GetWorldTransform().GetScale3D()));
254+
UKismetSystemLibrary::DrawDebugBox(GetWorld(), DebugTransform.GetLocation(), Mesh.GetStaticMesh()->GetBounds().BoxExtent * Handle.ActorScale3D, DebugSimulatedColor, DebugTransform.Rotator(), DebugTick, DebugThickness);
253255
}
254256
}
255257
}
@@ -296,19 +298,19 @@ void ALocalSimulationVolume::TransformUpdated(USceneComponent* InRootComponent,
296298
if(LocalPhysicData* MeshData = GetDataForStaticMesh(&Mesh))
297299
{
298300
// create easy reference for later
299-
LocalPhysics::FActorHandle* Handle = MeshData->InHandle;
301+
LocalPhysics::FActorHandle& Handle = *MeshData->InHandle;
300302

301303
const FTransform& WorldBodyTransform = Mesh.GetComponentTransform();
302304

303305
// Kinematic update for our physics in 'local' space
304-
Handle->SetWorldTransform(WorldBodyTransform.GetRelativeTransform(LocalSpace->ComponentToWorld));
306+
Handle.SetWorldTransform(WorldBodyTransform.GetRelativeTransform(LocalSpace->ComponentToWorld));
305307

306308
// let's show everything in simulation.
307309
if (bShowDebugPhyics)
308310
{
309-
const FTransform& BodyTransform = bDebugInWorldSpace ? WorldBodyTransform : Handle->GetWorldTransform();
311+
const FTransform& BodyTransform = bDebugInWorldSpace ? WorldBodyTransform : Handle.GetWorldTransform();
310312

311-
UKismetSystemLibrary::DrawDebugBox(GetWorld(), BodyTransform.GetLocation(), Mesh.GetStaticMesh()->GetBounds().BoxExtent * WorldBodyTransform.GetScale3D(), DebugKinematicColor, BodyTransform.Rotator(), DebugTick, DebugKinematicThickness);
313+
UKismetSystemLibrary::DrawDebugBox(GetWorld(), BodyTransform.GetLocation(), Mesh.GetStaticMesh()->GetBounds().BoxExtent * Handle.ActorScale3D, DebugKinematicColor, BodyTransform.Rotator(), DebugTick, DebugKinematicThickness);
312314
}
313315
}
314316
}
@@ -379,9 +381,11 @@ bool ALocalSimulationVolume::AddStaticMeshToSimulation(UStaticMeshComponent* Mes
379381
// if we don't find this mesh in Simulated or Kinematic arrays
380382
if (IsInSimulation(Mesh) == false)
381383
{
384+
//UE_LOG(LocalSimulationLog, Warning, L"Current transform: %s is %s", *Mesh->GetStaticMesh()->GetName(), *Mesh->GetComponentTransform().ToHumanReadableString());
382385
/*
383386
* messy check for static, kinematic, dynamic
384387
*/
388+
385389
FPhysScene* PhysScene = GetWorld()->GetPhysicsScene();
386390
if (PhysScene == nullptr)
387391
{
@@ -392,10 +396,12 @@ bool ALocalSimulationVolume::AddStaticMeshToSimulation(UStaticMeshComponent* Mes
392396
PxScene* SyncScene = PhysScene->GetPhysXScene(PST_Sync);
393397
SCOPED_SCENE_WRITE_LOCK(SyncScene); //SCOPED_SCENE_WRITE_LOCK or SCOPED_SCENE_READ_LOCK if you only need to read
394398

395-
// default is Dynamic, other checks will override this default if they're true.
399+
// default is Dynamic, other checks will override this default if they're true.
396400
ELocalPhysicsBodyType typeOfAdd = ELocalPhysicsBodyType::Dynamic;
401+
397402
// check if Kinematic by Component Mobility == Movable && Physics active
398-
typeOfAdd = Mesh->Mobility.GetValue() == EComponentMobility::Movable && (Mesh->IsSimulatingPhysics() == false) ? ELocalPhysicsBodyType::Kinematic : typeOfAdd;
403+
typeOfAdd = (Mesh->Mobility.GetValue() == EComponentMobility::Movable) && (Mesh->IsSimulatingPhysics() == false) ? ELocalPhysicsBodyType::Kinematic : typeOfAdd;
404+
399405
// check if Static by Component Mobility == Static
400406
typeOfAdd = Mesh->Mobility.GetValue() == EComponentMobility::Static ? ELocalPhysicsBodyType::Static : typeOfAdd;
401407

@@ -406,7 +412,13 @@ bool ALocalSimulationVolume::AddStaticMeshToSimulation(UStaticMeshComponent* Mes
406412
FBodyInstance& BodyInstance = Mesh->BodyInstance;
407413

408414
// create copy of new relative transform
409-
FTransform BodyTransform = Mesh->GetComponentTransform().GetRelativeTransform(LocalSpace->GetComponentTransform());
415+
FTransform BodyTransform = BodyInstance.GetUnrealWorldTransform_AssumesLocked(false).GetRelativeTransform(LocalSpace->GetComponentTransform());
416+
//BodyTransform.SetScale3D(Mesh->GetComponentTransform().GetScale3D());
417+
//FTransform BodyTransform = FTransform(Mesh->GetComponentTransform().Rotator(), Mesh->Bounds.Origin, Mesh->GetComponentTransform().GetScale3D()).GetRelativeTransform(LocalSpace->GetComponentTransform());
418+
//FTransform BodyTransform = Mesh->GetComponentTransform();
419+
//BodyTransform.SetScale3D(FVector(1.f));
420+
//BodyTransform = BodyTransform.GetRelativeTransform(LocalSpace->GetComponentTransform());
421+
410422

411423
UStaticMeshComponent* DynamicMesh = NewMeshData->InPhysicsMesh;
412424
if(ShouldExistInBothScenes)
@@ -491,6 +503,9 @@ bool ALocalSimulationVolume::AddStaticMeshToSimulation(UStaticMeshComponent* Mes
491503
BodyInstance.TermBody();
492504
}
493505

506+
// store scale so that Unreal component gets returned the correct scale / debug looks percise.
507+
NewMeshData->InHandle->ActorScale3D = Mesh->GetComponentTransform().GetScale3D();
508+
494509
// create new pair in kinematic meshses array (we don't update on tick)
495510
haveWeAddedMesh = (SimulatedActors.Add(NewMeshData) > -1);
496511
}
@@ -518,7 +533,7 @@ bool ALocalSimulationVolume::AddConstraintToStaticMeshes(UStaticMeshComponent* M
518533

519534
LocalPhysics::LocalPhysicJointData* newData = new LocalPhysicJointData(*LocalSimulation, { MeshDataOne, MeshDataTwo }, nullptr, { MeshDataOne->InBodyType, MeshDataTwo->InBodyType });
520535

521-
PxD6Joint* PD6Joint = PxD6JointCreate(*GPhysXSDK, nullptr, PxTransform(PxIdentity), nullptr, U2PTransform(ActorTwo->GetWorldTransform().GetRelativeTransform(ActorOne->GetWorldTransform())));
536+
PxD6Joint* PD6Joint = PxD6JointCreate(*GPhysXSDK, nullptr, PxTransform(PxIdentity), nullptr, U2PTransform(ActorTwo->GetBodyTransform().GetRelativeTransform(ActorOne->GetBodyTransform())));
522537

523538
if(PD6Joint)
524539
{

Plugins/LocalPhysics/Source/LocalPhysics/Public/LocalPhysicsActorHandle.h

+16-2
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,22 @@ struct LOCALPHYSICS_API FActorHandle
1717

1818
private:
1919
/** Converts from actor space (i.e. the transform in world space as the client gives us) to body space (body with its origin at the COM and oriented to inertia tensor) */
20-
FVector ActorScale3D;
2120
FTransform ActorToBody;
2221
FLocalSimulation& OwningSimulation;
2322
int32 ActorDataIndex;
2423
int rigidBodyType = -1;
2524

2625

2726
public:
27+
FVector ActorScale3D;
28+
2829
/** Sets the world transform.*/
2930
void SetWorldTransform(const FTransform& WorldTM)
3031
{
3132
#if WITH_PHYSX
3233
OwningSimulation.GetLowLevelBody(ActorDataIndex).body2World = U2PTransform(ActorToBody * WorldTM);
3334
#endif
34-
ActorScale3D = WorldTM.GetScale3D();
35+
//ActorScale3D = WorldTM.GetScale3D();
3536
}
3637

3738
/** Whether the body is simulating */
@@ -62,6 +63,19 @@ struct LOCALPHYSICS_API FActorHandle
6263
#endif
6364
}
6465

66+
/** Get the world transform */
67+
FTransform GetProjectedTransform() const
68+
{
69+
#if WITH_PHYSX
70+
auto Transform = ActorToBody;
71+
Transform.SetLocation(FVector::ZeroVector);
72+
Transform = Transform.GetRelativeTransformReverse(P2UTransform(OwningSimulation.GetLowLevelBody(ActorDataIndex).body2World));
73+
return Transform;
74+
#else
75+
return FTransform::Identity;
76+
#endif
77+
}
78+
6579
/** Set the linear velocity */
6680
void SetLinearVelocity(const FVector& NewLinearVelocity)
6781
{

0 commit comments

Comments
 (0)