diff --git a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCollision.cpp b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCollision.cpp index be3189256..b025c86ba 100644 --- a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCollision.cpp +++ b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCollision.cpp @@ -121,9 +121,17 @@ void UNewtonCollision::OnUnregister() void UNewtonCollision::SetTransform(const USceneComponent* const meshComponent) { - const AActor* const owner = GetOwner(); - check(owner); - const FTransform bodyTransform(owner->GetRootComponent()->GetComponentToWorld()); + //const AActor* const owner = GetOwner(); + //check(owner); + FTransform bodyTransform(GetComponentTransform()); + for (USceneComponent* parent = GetAttachParent(); parent; parent = parent->GetAttachParent()) + { + bodyTransform = parent->GetComponentTransform(); + if (Cast(parent)) + { + break; + } + } const FTransform globalTransform(meshComponent->GetComponentToWorld()); const FTransform localTransform(globalTransform * bodyTransform.Inverse()); @@ -247,7 +255,7 @@ ndShapeInstance* UNewtonCollision::CreateBodyInstanceShape(const ndMatrix& bodyM const ndVector scale(ndFloat32(uScale.X), ndFloat32(uScale.Y), ndFloat32(uScale.Z), ndFloat32(1.0f)); const FTransform transform(GetComponentToWorld()); - const ndMatrix matrix(UNewtonRigidBody::ToNewtonMatrix(transform) * bodyMatrix.OrthoInverse()); + const ndMatrix matrix(ToNewtonMatrix(transform) * bodyMatrix.OrthoInverse()); instance->SetScale(scale); instance->SetLocalMatrix(matrix); @@ -269,25 +277,25 @@ ndVector UNewtonCollision::GetVolumePosition(const ndMatrix& bodyMatrix) const return posit; } +bool UNewtonCollision::GetDebugMode() const +{ + for (USceneComponent* component = GetAttachParent(); component; component = component->GetAttachParent()) + { + const UNewtonRigidBody* const rigidBody = Cast(component); + if (rigidBody) + { + return rigidBody->ShowDebug; + } + } + return false; +} + void UNewtonCollision::ApplyPropertyChanges() { m_propertyChanged = false; if (m_shape) { - auto GetDebugMode = [this]() - { - for (USceneComponent* component = GetAttachParent(); component; component = component->GetAttachParent()) - { - const UNewtonRigidBody* const rigidBody = Cast(component); - if (rigidBody) - { - return rigidBody->ShowDebug; - } - } - return false; - }; - bool showDebug = GetDebugMode(); if (showDebug) { @@ -300,12 +308,12 @@ void UNewtonCollision::ApplyPropertyChanges() { //UE_LOG(LogTemp, Warning, TEXT("Rebuild Mesh")); PolygonizeMesh wireMesh; - ndShapeInstance* const instanceShape = CreateInstanceShape(); - instanceShape->DebugShape(ndGetIdentityMatrix(), wireMesh); + ndShapeInstance* const instance = CreateInstanceShape(); + instance->DebugShape(ndGetIdentityMatrix(), wireMesh); visualMesh = wireMesh.CreateDynamicMesh3(); plugin->AddDynamicMesh(visualMesh, m_hash); - delete instanceShape; + delete instance; } m_visualMesh = visualMesh; diff --git a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCollision.h b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCollision.h index 561e8e918..385d795e5 100644 --- a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCollision.h +++ b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCollision.h @@ -45,6 +45,7 @@ class UNewtonCollision : public UDynamicMeshComponent virtual void BuildNewtonShape(); virtual void ApplyPropertyChanges(); + bool GetDebugMode() const; virtual ndShape* CreateShape() const; virtual long long CalculateHash() const; virtual ndShapeInstance* CreateInstanceShape() const; diff --git a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCollisionConvexApproximate.cpp b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCollisionConvexApproximate.cpp index feff1cb4a..f425c33f3 100644 --- a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCollisionConvexApproximate.cpp +++ b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCollisionConvexApproximate.cpp @@ -257,6 +257,7 @@ ndConvexHullSet* UNewtonCollisionConvexApproximate::CreateConvexApproximationSha hullSet->Push(pointsSet); } + UE_LOG(LogTemp, Display, TEXT("number of vhcd convex generated: %d"), hullArray.GetCount()); delete vhacdHullSet; return hullSet; } @@ -313,7 +314,7 @@ ndShapeInstance* UNewtonCollisionConvexApproximate::CreateBodyInstanceShape(cons const ndVector scale(ndFloat32(1.0f)); const FTransform transform(GetComponentToWorld()); - const ndMatrix matrix(UNewtonRigidBody::ToNewtonMatrix(transform) * bodyMatrix.OrthoInverse()); + const ndMatrix matrix(ToNewtonMatrix(transform) * bodyMatrix.OrthoInverse()); instance->SetScale(scale); instance->SetLocalMatrix(matrix); diff --git a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCollisionConvexHull.cpp b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCollisionConvexHull.cpp index 36fea063a..040db24ea 100644 --- a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCollisionConvexHull.cpp +++ b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCollisionConvexHull.cpp @@ -49,31 +49,43 @@ ndShape* UNewtonCollisionConvexHull::CreateShape() const return new ndShapeNull(); } -void UNewtonCollisionConvexHull::InitStaticMeshCompoment(const USceneComponent* const meshComponent) +void UNewtonCollisionConvexHull::GenerateMesh(const USceneComponent* const meshComponent) { - SetTransform(meshComponent); - const UStaticMeshComponent* const staticMeshComponent = Cast(meshComponent); check(staticMeshComponent); UStaticMesh* const staticMesh = staticMeshComponent->GetStaticMesh().Get(); const FStaticMeshRenderData* const renderData = staticMesh->GetRenderData(); const FStaticMeshLODResourcesArray& renderResource = renderData->LODResources; - + const FVector uScale(GetComponentTransform().GetScale3D()); const ndVector scale(ndFloat32(uScale.X), ndFloat32(uScale.Y), ndFloat32(uScale.Z), ndFloat32(0.0f)); const ndVector bakedScale(scale.Scale(UNREAL_INV_UNIT_SYSTEM)); - + const FStaticMeshLODResources& renderLOD = renderResource[0]; const FStaticMeshVertexBuffers& staticMeshVertexBuffer = renderLOD.VertexBuffers;; const FPositionVertexBuffer& positBuffer = staticMeshVertexBuffer.PositionVertexBuffer; - + + FTransform bodyTransform(meshComponent->GetComponentToWorld()); + for (USceneComponent* parent = GetAttachParent(); parent; parent = parent->GetAttachParent()) + { + bodyTransform = parent->GetComponentTransform(); + if (Cast(parent)) + { + break; + } + } + + const FTransform globalTransform(GetComponentToWorld()); + const FTransform localTransform(globalTransform * bodyTransform.Inverse()); + const ndMatrix localMatrix(ToNewtonMatrix(localTransform)); + ndArray points; for (ndInt32 i = positBuffer.GetNumVertices() - 1; i >= 0; --i) { const FVector3f p(positBuffer.VertexPosition(i)); - const ndVector q(ndFloat32(p.X), ndFloat32(p.Y), ndFloat32(p.Z), ndFloat32(0.0f)); - points.PushBack(q * bakedScale); + const ndVector q(ndFloat32(p.X), ndFloat32(p.Y), ndFloat32(p.Z), ndFloat32(1.0f)); + points.PushBack(localMatrix.TransformVector(q * bakedScale)); } ndConvexHull3d convexHull(&points[0].m_x, sizeof(ndBigVector), points.GetCount(), Tolerance, MaxVertexCount); const ndArray& convexVertex = convexHull.GetVertexPool(); @@ -85,25 +97,44 @@ void UNewtonCollisionConvexHull::InitStaticMeshCompoment(const USceneComponent* } } -ndShapeInstance* UNewtonCollisionConvexHull::CreateBodyInstanceShape(const ndMatrix& bodyMatrix) const +void UNewtonCollisionConvexHull::InitStaticMeshCompoment(const USceneComponent* const meshComponent) +{ + SetTransform(meshComponent); + GenerateMesh(meshComponent); +} + +ndShapeInstance* UNewtonCollisionConvexHull::CreateInstanceShape() const { - ndShapeInstance* const instance = CreateInstanceShape(); + ndShapeInstance* const instance = new ndShapeInstance(m_shape); + //ndShapeInstance* const instance = new ndShapeInstance(new ndShapeSphere(0.5f)); - const ndVector scale(ndFloat32(1.0f)); - const FTransform transform(GetComponentToWorld()); - const ndMatrix matrix(UNewtonRigidBody::ToNewtonMatrix(transform) * bodyMatrix.OrthoInverse()); + FTransform bodyTransform(GetComponentToWorld()); + for (USceneComponent* parent = GetAttachParent(); parent; parent = parent->GetAttachParent()) + { + bodyTransform = parent->GetComponentTransform(); + if (Cast(parent)) + { + break; + } + } + + const FTransform globalTransform(GetComponentToWorld()); + const FTransform localTransform(globalTransform * bodyTransform.Inverse()); + const ndMatrix localMatrix(ToNewtonMatrix(localTransform)); + + const FVector uScale(GetComponentTransform().GetScale3D()); + const ndVector invScale(ndFloat32(1.0f/uScale.X), ndFloat32(1.0f/uScale.Y), ndFloat32(1.0f / uScale.Z), ndFloat32(0.0f)); - instance->SetScale(scale); - instance->SetLocalMatrix(matrix); + instance->SetScale(invScale); + instance->SetLocalMatrix(localMatrix.OrthoInverse()); return instance; } -ndShapeInstance* UNewtonCollisionConvexHull::CreateInstanceShape() const +ndShapeInstance* UNewtonCollisionConvexHull::CreateBodyInstanceShape(const ndMatrix& bodyMatrix) const { ndShapeInstance* const instance = new ndShapeInstance(m_shape); - const FVector uScale(GetComponentTransform().GetScale3D()); - const ndVector scale(ndFloat32(1.0f / uScale.X), ndFloat32(1.0f / uScale.Y), ndFloat32(1.0f / uScale.Z), ndFloat32(0.0f)); - instance->SetScale(scale); + instance->SetScale(ndVector(ndFloat32(1.0f))); + instance->SetLocalMatrix(ndGetIdentityMatrix()); return instance; } @@ -116,7 +147,12 @@ void UNewtonCollisionConvexHull::ApplyPropertyChanges() const USceneComponent* const parent = Cast(GetAttachParent()); if (parent) { - InitStaticMeshCompoment(parent); + const FTransform localTransform; + SetComponentToWorld(parent->GetComponentToWorld()); + SetRelativeScale3D_Direct(localTransform.GetScale3D()); + SetRelativeRotation_Direct(FRotator(localTransform.GetRotation())); + SetRelativeLocation_Direct(localTransform.GetLocation()); + GenerateMesh(parent); } } } diff --git a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCollisionConvexHull.h b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCollisionConvexHull.h index ff3fc34b7..ad6eed879 100644 --- a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCollisionConvexHull.h +++ b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCollisionConvexHull.h @@ -20,16 +20,19 @@ class UNewtonCollisionConvexHull : public UNewtonCollision public: // Sets default values for this component's properties UNewtonCollisionConvexHull(); + virtual void InitStaticMeshCompoment(const USceneComponent* const meshComponent) override; protected: virtual void Serialize(FArchive& ar) override; - virtual void ApplyPropertyChanges(); - virtual ndShape* CreateShape() const; - virtual long long CalculateHash() const; - virtual ndShapeInstance* CreateInstanceShape() const; - virtual ndShapeInstance* CreateBodyInstanceShape(const ndMatrix& bodyMatrix) const; + virtual void ApplyPropertyChanges() override; + virtual ndShape* CreateShape() const override; + virtual long long CalculateHash() const override; + + void GenerateMesh(const USceneComponent* const meshComponent); + virtual ndShapeInstance* CreateInstanceShape() const override; + virtual ndShapeInstance* CreateBodyInstanceShape(const ndMatrix& bodyMatrix) const override; UPROPERTY(EditAnywhere, Category = Newton, meta = (ClampMin = 0.001f)) float Tolerance; diff --git a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCollisionLandscape.cpp b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCollisionLandscape.cpp index cebdc5063..f0f3c0eec 100644 --- a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCollisionLandscape.cpp +++ b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCollisionLandscape.cpp @@ -166,7 +166,7 @@ ndShapeInstance* UNewtonCollisionLandscape::CreateBodyInstanceShape(const ndMatr { ndShapeInstance* const instance = new ndShapeInstance(m_shape); const FTransform transform(GetComponentToWorld()); - const ndMatrix matrix (UNewtonRigidBody::ToNewtonMatrix(transform)); + const ndMatrix matrix (ToNewtonMatrix(transform)); const FVector uScale(transform.GetScale3D()); ndMatrix origin(ndGetIdentityMatrix()); origin.m_posit.m_y = ndFloat32(ndFloat32(m_tileSize_y - 1) * m_scale_y * uScale.Y * UNREAL_INV_UNIT_SYSTEM); diff --git a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCollisionPolygonalMesh.cpp b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCollisionPolygonalMesh.cpp index 7b3484029..2032dfde3 100644 --- a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCollisionPolygonalMesh.cpp +++ b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCollisionPolygonalMesh.cpp @@ -192,7 +192,7 @@ ndShapeInstance* UNewtonCollisionPolygonalMesh::CreateBodyInstanceShape(const nd const ndVector scale(ndFloat32(1.0f)); const FTransform transform(GetComponentToWorld()); - const ndMatrix matrix(UNewtonRigidBody::ToNewtonMatrix(transform) * bodyMatrix.OrthoInverse()); + const ndMatrix matrix(ToNewtonMatrix(transform) * bodyMatrix.OrthoInverse()); instance->SetScale(scale); instance->SetLocalMatrix(matrix); diff --git a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCommons.cpp b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCommons.cpp index 504158740..dbb78ae96 100644 --- a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCommons.cpp +++ b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCommons.cpp @@ -1,7 +1,8 @@ -// Copyright Epic Games, Inc. All Rights Reserved. + #include "NewtonCommons.h" +#include "ThirdParty/newtonLibrary/Public/dNewton/ndNewton.h" ndHullPoints::ndHullPoints() :TArray() @@ -25,3 +26,26 @@ ndConvexHullSet::~ndConvexHullSet() } } +FTransform ToUnRealTransform(const ndMatrix& matrix) +{ + const ndQuaternion rotation(matrix); + const ndVector posit(matrix.m_posit.Scale(UNREAL_UNIT_SYSTEM)); + const FVector uPosit(posit.m_x, posit.m_y, posit.m_z); + const FQuat uRot(rotation.m_x, rotation.m_y, rotation.m_z, rotation.m_w); + + FTransform transform; + transform.SetRotation(uRot); + transform.SetLocation(uPosit); + return transform; +} + +ndMatrix ToNewtonMatrix(const FTransform& tranform) +{ + const FVector location(tranform.GetLocation()); + const FQuat rotation(tranform.Rotator().Quaternion()); + + const ndQuaternion quat(ndFloat32(rotation.X), ndFloat32(rotation.Y), ndFloat32(rotation.Z), ndFloat32(rotation.W)); + const ndVector posit(UNREAL_INV_UNIT_SYSTEM * ndFloat32(location.X), UNREAL_INV_UNIT_SYSTEM * ndFloat32(location.Y), UNREAL_INV_UNIT_SYSTEM * ndFloat32(location.Z), ndFloat32(1.0f)); + const ndMatrix matrix(ndCalculateMatrix(quat, posit)); + return matrix; +} diff --git a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonJointCylinder.cpp b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonJointCylinder.cpp index 72d699bf6..2badedd0f 100644 --- a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonJointCylinder.cpp +++ b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonJointCylinder.cpp @@ -29,7 +29,7 @@ void UNewtonJointCylinder::DrawAngularGizmo(float timestep) const { ndFloat32 scale = DebugScale * UNREAL_UNIT_SYSTEM; const FTransform transform(GetComponentTransform()); - const ndMatrix matrix(UNewtonRigidBody::ToNewtonMatrix(transform)); + const ndMatrix matrix(ToNewtonMatrix(transform)); const FColor pinColor(255.0f, 255.0f, 0.0f); const ndVector pinDir(matrix.m_front.Scale(scale * 0.9f)); const FVector coneDir(matrix.m_front.m_x, matrix.m_front.m_y, matrix.m_front.m_z); @@ -54,7 +54,7 @@ void UNewtonJointCylinder::DrawAngularGizmo(float timestep) const ndFloat32 angleStep = ndMin(deltaTwist, 360.0f) / subdiv; const FVector parentOrigin(transform.GetLocation()); - const ndMatrix parentMatrix(UNewtonRigidBody::ToNewtonMatrix(transform)); + const ndMatrix parentMatrix(ToNewtonMatrix(transform)); verts.Push(parentOrigin); for (ndInt32 i = 0; i <= subdiv; ++i) { @@ -79,7 +79,7 @@ void UNewtonJointCylinder::DrawLinearGizmo(float timestep) const { ndFloat32 scale = DebugScale * UNREAL_UNIT_SYSTEM; const FTransform transform(GetComponentTransform()); - const ndMatrix matrix(UNewtonRigidBody::ToNewtonMatrix(transform)); + const ndMatrix matrix(ToNewtonMatrix(transform)); const FColor pinColor(255.0f, 255.0f, 0.0f); const ndVector pinDir(matrix.m_front.Scale(scale * 0.9f)); const FVector pingStart(transform.GetLocation()); @@ -127,7 +127,7 @@ void UNewtonJointCylinder::CreateJoint(ANewtonWorldActor* const newtonWorldActor if (body0 && body1) { const FTransform transform(GetRelativeTransform()); - const ndMatrix matrix(UNewtonRigidBody::ToNewtonMatrix(transform) * body1->GetMatrix()); + const ndMatrix matrix(ToNewtonMatrix(transform) * body1->GetMatrix()); ndJointCylinder* const joint = new ndJointCylinder(matrix, body0, body1); joint->SetLimitStateAngle(EnableAngularLimits); diff --git a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonJointHinge.cpp b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonJointHinge.cpp index 0311907cc..dd6394f6b 100644 --- a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonJointHinge.cpp +++ b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonJointHinge.cpp @@ -23,7 +23,7 @@ void UNewtonJointHinge::DrawGizmo(float timestep) const { ndFloat32 scale = DebugScale * UNREAL_UNIT_SYSTEM; const FTransform transform(GetComponentTransform()); - const ndMatrix matrix(UNewtonRigidBody::ToNewtonMatrix(transform)); + const ndMatrix matrix(ToNewtonMatrix(transform)); const FColor pinColor(255.0f, 255.0f, 0.0f); const ndVector pinDir(matrix.m_front.Scale(scale * 0.9f)); const FVector coneDir(matrix.m_front.m_x, matrix.m_front.m_y, matrix.m_front.m_z); @@ -48,7 +48,7 @@ void UNewtonJointHinge::DrawGizmo(float timestep) const ndFloat32 angleStep = ndMin(deltaTwist, 360.0f) / subdiv; const FVector parentOrigin(transform.GetLocation()); - const ndMatrix parentMatrix(UNewtonRigidBody::ToNewtonMatrix(transform)); + const ndMatrix parentMatrix(ToNewtonMatrix(transform)); verts.Push(parentOrigin); for (ndInt32 i = 0; i <= subdiv; ++i) { @@ -83,7 +83,7 @@ void UNewtonJointHinge::CreateJoint(ANewtonWorldActor* const newtonWorldActor) if (body0 && body1) { const FTransform transform(GetRelativeTransform()); - const ndMatrix matrix(UNewtonRigidBody::ToNewtonMatrix(transform) * body1->GetMatrix()); + const ndMatrix matrix(ToNewtonMatrix(transform) * body1->GetMatrix()); ndJointHinge* const joint = new ndJointHinge(matrix, body0, body1); joint->SetLimitState(EnableLimits); diff --git a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonJointSlider.cpp b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonJointSlider.cpp index 6ffedeb48..1245e9a4f 100644 --- a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonJointSlider.cpp +++ b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonJointSlider.cpp @@ -23,7 +23,7 @@ void UNewtonJointSlider::DrawGizmo(float timestep) const { ndFloat32 scale = DebugScale * UNREAL_UNIT_SYSTEM; const FTransform transform(GetComponentTransform()); - const ndMatrix matrix(UNewtonRigidBody::ToNewtonMatrix(transform)); + const ndMatrix matrix(ToNewtonMatrix(transform)); const FColor pinColor(255.0f, 255.0f, 0.0f); const ndVector pinDir(matrix.m_front.Scale(scale * 0.9f)); const FVector pingStart(transform.GetLocation()); @@ -60,7 +60,7 @@ void UNewtonJointSlider::CreateJoint(ANewtonWorldActor* const newtonWorldActor) if (body0 && body1) { const FTransform transform(GetRelativeTransform()); - const ndMatrix matrix(UNewtonRigidBody::ToNewtonMatrix(transform) * body1->GetMatrix()); + const ndMatrix matrix(ToNewtonMatrix(transform) * body1->GetMatrix()); ndJointSlider* const joint = new ndJointSlider(matrix, body0, body1); diff --git a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonRigidBody.cpp b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonRigidBody.cpp index f9e1e5e3e..e1d6a717b 100644 --- a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonRigidBody.cpp +++ b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonRigidBody.cpp @@ -143,30 +143,6 @@ UNewtonRigidBody::UNewtonRigidBody() SetMobility(EComponentMobility::Movable); } -FTransform UNewtonRigidBody::ToUnRealTransform(const ndMatrix& matrix) -{ - const ndQuaternion rotation(matrix); - const ndVector posit(matrix.m_posit.Scale(UNREAL_UNIT_SYSTEM)); - const FVector uPosit(posit.m_x, posit.m_y, posit.m_z); - const FQuat uRot(rotation.m_x, rotation.m_y, rotation.m_z, rotation.m_w); - - FTransform transform; - transform.SetRotation(uRot); - transform.SetLocation(uPosit); - return transform; -} - -ndMatrix UNewtonRigidBody::ToNewtonMatrix(const FTransform& tranform) -{ - const FVector location(tranform.GetLocation()); - const FQuat rotation(tranform.Rotator().Quaternion()); - - const ndQuaternion quat(ndFloat32(rotation.X), ndFloat32(rotation.Y), ndFloat32(rotation.Z), ndFloat32(rotation.W)); - const ndVector posit(UNREAL_INV_UNIT_SYSTEM * ndFloat32(location.X), UNREAL_INV_UNIT_SYSTEM * ndFloat32(location.Y), UNREAL_INV_UNIT_SYSTEM * ndFloat32(location.Z), ndFloat32(1.0f)); - const ndMatrix matrix(ndCalculateMatrix(quat, posit)); - return matrix; -} - void UNewtonRigidBody::BeginDestroy() { Super::BeginDestroy(); diff --git a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonRigidBody.h b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonRigidBody.h index 400669c28..e595e0885 100644 --- a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonRigidBody.h +++ b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonRigidBody.h @@ -45,9 +45,6 @@ class UNewtonRigidBody : public USceneComponent // Sets default values for this component's properties UNewtonRigidBody(); - static ndMatrix ToNewtonMatrix(const FTransform& tranform); - static FTransform ToUnRealTransform(const ndMatrix& matrix); - protected: // Called every frame virtual void PostLoad() override; diff --git a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonWorldActor.cpp b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonWorldActor.cpp index 019724261..6c8debbf0 100644 --- a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonWorldActor.cpp +++ b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonWorldActor.cpp @@ -53,11 +53,11 @@ ANewtonWorldActor::ANewtonWorldActor() :m_world(nullptr) ,m_timeAccumulator(0.0f) ,m_interpolationParam(1.0f) - ,m_beginPlay(false) { // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it. //PrimaryActorTick.bCanEverTick = true; + m_beginPlay = false; SolverPasses = 2; UpdateRate = 60.0f; ParallelThreads = 1; @@ -89,6 +89,34 @@ void ANewtonWorldActor::BeginPlay() m_world->Sync(); } +void ANewtonWorldActor::EndPlay(const EEndPlayReason::Type EndPlayReason) +{ + Super::EndPlay(EndPlayReason); + if (m_world) + { + Cleanup(); + switch (EndPlayReason) + { + case EEndPlayReason::Destroyed: + ndAssert(0); + break; + case EEndPlayReason::LevelTransition: + ndAssert(0); + break; + case EEndPlayReason::EndPlayInEditor: + ndAssert(0); + break; + case EEndPlayReason::RemovedFromWorld: + ndAssert(0); + break; + case EEndPlayReason::Quit: + m_world->Sync(); + delete m_world; + break; + } + } +} + void ANewtonWorldActor::StartGame() { m_beginPlay = false; @@ -176,34 +204,6 @@ void ANewtonWorldActor::Cleanup() } } -void ANewtonWorldActor::EndPlay(const EEndPlayReason::Type EndPlayReason) -{ - Super::EndPlay(EndPlayReason); - if (m_world) - { - Cleanup(); - switch (EndPlayReason) - { - case EEndPlayReason::Destroyed: - ndAssert(0); - break; - case EEndPlayReason::LevelTransition: - ndAssert(0); - break; - case EEndPlayReason::EndPlayInEditor: - ndAssert(0); - break; - case EEndPlayReason::RemovedFromWorld: - ndAssert(0); - break; - case EEndPlayReason::Quit: - m_world->Sync(); - delete m_world; - break; - } - } -} - void ANewtonWorldActor::Destroyed() { Super::Destroyed(); diff --git a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Public/NewtonCommons.h b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Public/NewtonCommons.h index 7db6b3ac6..b5a59ffc5 100644 --- a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Public/NewtonCommons.h +++ b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Public/NewtonCommons.h @@ -7,6 +7,7 @@ #define UNREAL_INV_UNIT_SYSTEM ndFloat32 (1.0f / UNREAL_UNIT_SYSTEM) class ndShape; +class ndMatrix; class ANewtonWorldActor; template @@ -30,3 +31,6 @@ class ndConvexHullSet : public TArray ndConvexHullSet(); ~ndConvexHullSet(); }; + +ndMatrix ToNewtonMatrix(const FTransform& tranform); +FTransform ToUnRealTransform(const ndMatrix& matrix);