diff --git a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/ThirdParty/newtonLibrary/Public/dCollision/ndShapeConvex.cpp b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/ThirdParty/newtonLibrary/Public/dCollision/ndShapeConvex.cpp index 7cb8d4fe0..e9aeb91da 100644 --- a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/ThirdParty/newtonLibrary/Public/dCollision/ndShapeConvex.cpp +++ b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/ThirdParty/newtonLibrary/Public/dCollision/ndShapeConvex.cpp @@ -67,8 +67,10 @@ void ndShapeConvex::DebugShape(const ndMatrix& matrix, ndShapeDebugNotify& debug ndAssert(m_edgeCount < D_MAX_EDGE_COUNT); ndAssert(m_vertexCount < D_MAX_EDGE_COUNT); - memset(mark, 0, sizeof(mark)); - memset(edgeType, ndShapeDebugNotify::m_shared, sizeof(edgeType)); + //memset(mark, 0, sizeof(mark)); + ndMemSet(mark, ndInt8(0), D_MAX_EDGE_COUNT); + //memset(edgeType, ndShapeDebugNotify::m_shared, sizeof(edgeType)); + ndMemSet(edgeType, ndShapeDebugNotify::m_shared, D_MAX_EDGE_COUNT); matrix.TransformTriplex(&tmp[0].m_x, sizeof(ndVector), &m_vertex[0].m_x, sizeof(ndVector), m_vertexCount); for (ndInt32 i = 0; i < m_edgeCount; ++i) { @@ -94,13 +96,15 @@ void ndShapeConvex::SetVolumeAndCG() { ndVector faceVertex[D_MAX_EDGE_COUNT]; ndInt8* const edgeMarks = ndAlloca(ndInt8, m_edgeCount + 32); - memset(&edgeMarks[0], 0, sizeof(ndInt8) * m_edgeCount); + //memset(&edgeMarks[0], 0, sizeof(ndInt8) * m_edgeCount); + ndMemSet(edgeMarks, ndInt8(0), m_edgeCount); ndPolyhedraMassProperties localData; for (ndInt32 i = 0; i < m_edgeCount; ++i) { ndConvexSimplexEdge* const face = &m_simplex[i]; - if (!edgeMarks[i]) { + if (!edgeMarks[i]) + { ndConvexSimplexEdge* edge = face; ndInt32 count = 0; do @@ -190,11 +194,12 @@ ndFloat32 ndShapeConvex::CalculateMassProperties(const ndMatrix& offset, ndVecto ndMatrix ndShapeConvex::CalculateInertiaAndCenterOfMass(const ndMatrix& alignMatrix, const ndVector& localScale, const ndMatrix& matrix) const { +#if 0 bool implicitTest = true; implicitTest = implicitTest && (ndAbs(localScale.m_x - localScale.m_y) < ndFloat32(1.0e-5f)); implicitTest = implicitTest && (ndAbs(localScale.m_x - localScale.m_z) < ndFloat32(1.0e-5f)); implicitTest = implicitTest && (ndAbs(localScale.m_y - localScale.m_z) < ndFloat32(1.0e-5f)); - implicitTest = ((ndShape*)this)->GetAsShapeConvexHull() ? false : true; + implicitTest = implicitTest && (((ndShape*)this)->GetAsShapeConvexHull() ? false : true); //if ((ndAbs(localScale.m_x - localScale.m_y) < ndFloat32(1.0e-5f)) && // (ndAbs(localScale.m_x - localScale.m_z) < ndFloat32(1.0e-5f)) && @@ -272,6 +277,42 @@ ndMatrix ndShapeConvex::CalculateInertiaAndCenterOfMass(const ndMatrix& alignMat inertia[3] = centerOfMass; return inertia; } +#else + + //not using implicit shapes for mass propeties. + ndVector inertiaII; + ndVector crossInertia; + ndVector centerOfMass; + ndMatrix scaledMatrix(matrix); + scaledMatrix[0] = scaledMatrix[0].Scale(localScale.m_x); + scaledMatrix[1] = scaledMatrix[1].Scale(localScale.m_y); + scaledMatrix[2] = scaledMatrix[2].Scale(localScale.m_z); + scaledMatrix = alignMatrix * scaledMatrix; + + ndFloat32 volume = CalculateMassProperties(scaledMatrix, inertiaII, crossInertia, centerOfMass); + if (volume < D_MAX_MIN_VOLUME) + { + volume = D_MAX_MIN_VOLUME; + } + + ndFloat32 invVolume = ndFloat32(1.0f) / volume; + centerOfMass = centerOfMass.Scale(invVolume); + centerOfMass.m_w = ndFloat32(1.0f); + inertiaII = inertiaII.Scale(invVolume); + crossInertia = crossInertia.Scale(invVolume); + ndMatrix inertia(ndGetIdentityMatrix()); + inertia[0][0] = inertiaII[0]; + inertia[1][1] = inertiaII[1]; + inertia[2][2] = inertiaII[2]; + inertia[0][1] = crossInertia[2]; + inertia[1][0] = crossInertia[2]; + inertia[0][2] = crossInertia[1]; + inertia[2][0] = crossInertia[1]; + inertia[1][2] = crossInertia[0]; + inertia[2][1] = crossInertia[0]; + inertia[3] = centerOfMass; + return inertia; +#endif } void ndShapeConvex::CalculateAabb(const ndMatrix& matrix, ndVector& p0, ndVector& p1) const @@ -298,7 +339,8 @@ ndVector ndShapeConvex::SupportVertex(const ndVector& dir) const ndAssert(ndAbs(dir.DotProduct(dir).GetScalar() - ndFloat32(1.0f)) < ndFloat32(1.0e-3f)); ndInt16 cache[16]; - memset(cache, -1, sizeof(cache)); + //memset(cache, -1, sizeof(cache)); + ndMemSet(cache, ndInt16(-1), 16); ndConvexSimplexEdge* edge = &m_simplex[0]; ndInt32 index = edge->m_vertex; @@ -865,7 +907,8 @@ ndVector ndShapeConvex::CalculateVolumeIntegral(const ndPlane& plane) const ndConvexSimplexEdge* capEdge = nullptr; ndVector cg(ndVector::m_zero); - memset(mark, 0, m_edgeCount); + //memset(mark, 0, m_edgeCount); + ndMemSet(mark, ndInt8(0), m_edgeCount); for (ndInt32 i = 0; i < m_edgeCount; ++i) { if (!mark[i]) diff --git a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/Newton.cpp b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/Newton.cpp index 901334227..704ecacf6 100644 --- a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/Newton.cpp +++ b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/Newton.cpp @@ -136,6 +136,9 @@ class FnewtonModule::ResourceCache ndTree m_shapeCache; Cache m_convexVhacdCache; Cache m_visualMeshCache; + + ndMovingAverage<16> m_renderTime; + ndMovingAverage<16> m_physicsTime; }; void FnewtonModule::StartupModule() @@ -529,12 +532,18 @@ bool FnewtonModule::Tick(float timestep) ANewtonWorldActor* const newtonWorld = FindNewtonWorldActor(); if (newtonWorld) { - char tmp[256]; + char tmp0[256]; + char tmp1[256]; float simTime = newtonWorld->GetSimTime(); - snprintf(tmp, sizeof(tmp), "timestep:%f(ms) physicsTime:%f(ms)", timestep * 1.0e3f, simTime * 1.0e3f); - - FString msg(tmp); - GEngine->AddOnScreenDebugMessage(INDEX_NONE, 0.0f, FColor::Yellow, msg); + m_resourceCache->m_physicsTime.Update(simTime * 1.0e3f); + m_resourceCache->m_renderTime.Update(timestep * 1.0e3f); + snprintf(tmp1, sizeof(tmp1), "physicsTime: %g(ms)", m_resourceCache->m_physicsTime.GetAverage()); + snprintf(tmp0, sizeof(tmp0), "renderTime: %g(ms)", m_resourceCache->m_renderTime.GetAverage()); + + FString msg0(tmp0); + FString msg1(tmp1); + GEngine->AddOnScreenDebugMessage(INDEX_NONE, 0.0f, FColor::Yellow, msg1); + GEngine->AddOnScreenDebugMessage(INDEX_NONE, 0.0f, FColor::Yellow, msg0); CleanupDebugLines(newtonWorld->GetWorld(), timestep); if (newtonWorld->m_beginPlay) 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 b025c86ba..23dd3aebd 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,8 +121,6 @@ void UNewtonCollision::OnUnregister() void UNewtonCollision::SetTransform(const USceneComponent* const meshComponent) { - //const AActor* const owner = GetOwner(); - //check(owner); FTransform bodyTransform(GetComponentTransform()); for (USceneComponent* parent = GetAttachParent(); parent; parent = parent->GetAttachParent()) { @@ -138,7 +136,6 @@ void UNewtonCollision::SetTransform(const USceneComponent* const meshComponent) SetComponentToWorld(globalTransform); // for some reason, this does not work in the unreal editor - //SetRelativeTransform(localTransform); SetRelativeScale3D_Direct(localTransform.GetScale3D()); SetRelativeRotation_Direct(FRotator(localTransform.GetRotation())); SetRelativeLocation_Direct(localTransform.GetLocation()); 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 385d795e5..5ab3fd46b 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 @@ -50,7 +50,7 @@ class UNewtonCollision : public UDynamicMeshComponent virtual long long CalculateHash() const; virtual ndShapeInstance* CreateInstanceShape() const; virtual ndShapeInstance* CreateBodyInstanceShape(const ndMatrix& bodyMatrix) const; - void SetTransform(const USceneComponent* const meshComponent); + virtual void SetTransform(const USceneComponent* const meshComponent); //public: long long m_hash; 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 040db24ea..af99752de 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 @@ -58,7 +58,7 @@ void UNewtonCollisionConvexHull::GenerateMesh(const USceneComponent* const meshC const FStaticMeshRenderData* const renderData = staticMesh->GetRenderData(); const FStaticMeshLODResourcesArray& renderResource = renderData->LODResources; - const FVector uScale(GetComponentTransform().GetScale3D()); + const FVector uScale(meshComponent->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)); @@ -66,26 +66,12 @@ void UNewtonCollisionConvexHull::GenerateMesh(const USceneComponent* const meshC 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(1.0f)); - points.PushBack(localMatrix.TransformVector(q * bakedScale)); + points.PushBack(q * bakedScale); } ndConvexHull3d convexHull(&points[0].m_x, sizeof(ndBigVector), points.GetCount(), Tolerance, MaxVertexCount); const ndArray& convexVertex = convexHull.GetVertexPool(); @@ -97,18 +83,11 @@ void UNewtonCollisionConvexHull::GenerateMesh(const USceneComponent* const meshC } } -void UNewtonCollisionConvexHull::InitStaticMeshCompoment(const USceneComponent* const meshComponent) -{ - SetTransform(meshComponent); - GenerateMesh(meshComponent); -} - -ndShapeInstance* UNewtonCollisionConvexHull::CreateInstanceShape() const +void UNewtonCollisionConvexHull::SetTransform(const USceneComponent* const meshComponent) { - ndShapeInstance* const instance = new ndShapeInstance(m_shape); - //ndShapeInstance* const instance = new ndShapeInstance(new ndShapeSphere(0.5f)); - - FTransform bodyTransform(GetComponentToWorld()); + //AActor* xxx0 = GetOwner(); + //AActor* xxx1 = meshComponent->GetOwner(); + FTransform bodyTransform(GetComponentTransform()); for (USceneComponent* parent = GetAttachParent(); parent; parent = parent->GetAttachParent()) { bodyTransform = parent->GetComponentTransform(); @@ -117,25 +96,22 @@ ndShapeInstance* UNewtonCollisionConvexHull::CreateInstanceShape() const break; } } + FTransform meshGlobalTransform(meshComponent->GetComponentToWorld()); + meshGlobalTransform.SetScale3D(FVector(1.0f, 1.0f, 1.0f)); + const FTransform localTransform(meshGlobalTransform * bodyTransform.Inverse()); - 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)); + SetComponentToWorld(meshGlobalTransform); - instance->SetScale(invScale); - instance->SetLocalMatrix(localMatrix.OrthoInverse()); - return instance; + // for some reason, this does not work in the unreal editor + SetRelativeScale3D_Direct(localTransform.GetScale3D()); + SetRelativeRotation_Direct(FRotator(localTransform.GetRotation())); + SetRelativeLocation_Direct(localTransform.GetLocation()); } -ndShapeInstance* UNewtonCollisionConvexHull::CreateBodyInstanceShape(const ndMatrix& bodyMatrix) const +void UNewtonCollisionConvexHull::InitStaticMeshCompoment(const USceneComponent* const meshComponent) { - ndShapeInstance* const instance = new ndShapeInstance(m_shape); - instance->SetScale(ndVector(ndFloat32(1.0f))); - instance->SetLocalMatrix(ndGetIdentityMatrix()); - return instance; + SetTransform(meshComponent); + GenerateMesh(meshComponent); } void UNewtonCollisionConvexHull::ApplyPropertyChanges() @@ -160,4 +136,24 @@ void UNewtonCollisionConvexHull::ApplyPropertyChanges() BuildNewtonShape(); Super::ApplyPropertyChanges(); -} \ No newline at end of file +} + +ndShapeInstance* UNewtonCollisionConvexHull::CreateInstanceShape() const +{ + ndShapeInstance* const instance = new ndShapeInstance(m_shape); + instance->SetScale(ndVector(ndFloat32(1.0f))); + instance->SetLocalMatrix(ndGetIdentityMatrix()); + return instance; +} + +ndShapeInstance* UNewtonCollisionConvexHull::CreateBodyInstanceShape(const ndMatrix& bodyMatrix) const +{ + ndShapeInstance* const instance = new ndShapeInstance(m_shape); + + const FTransform transform(GetComponentToWorld()); + const ndMatrix matrix(ToNewtonMatrix(transform) * bodyMatrix.OrthoInverse()); + + instance->SetLocalMatrix(matrix); + instance->SetScale(ndVector(ndFloat32(1.0f))); + return instance; +} 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 ad6eed879..67bf761c5 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 @@ -29,6 +29,7 @@ class UNewtonCollisionConvexHull : public UNewtonCollision virtual void ApplyPropertyChanges() override; virtual ndShape* CreateShape() const override; virtual long long CalculateHash() const override; + void SetTransform(const USceneComponent* const meshComponent) override; void GenerateMesh(const USceneComponent* const meshComponent); virtual ndShapeInstance* CreateInstanceShape() const override; diff --git a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCollisionWheel.cpp b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCollisionWheel.cpp index d98b39e9c..e4786c321 100644 --- a/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCollisionWheel.cpp +++ b/newton-4.00/applications/toolsAndWrapers/newtonUnreal/newton/Source/newton/Private/NewtonCollisionWheel.cpp @@ -16,7 +16,7 @@ UNewtonCollisionWheel::UNewtonCollisionWheel() ndShape* UNewtonCollisionWheel::CreateShape() const { - return new ndShapeChamferCylinder(0.25f, 0.5f); + return new ndShapeChamferCylinder(0.5f, 1.0f); } long long UNewtonCollisionWheel::CalculateHash() const @@ -66,8 +66,10 @@ ndShapeInstance* UNewtonCollisionWheel::CreateBodyInstanceShape(const ndMatrix& { ndShapeInstance* const instance = Super::CreateBodyInstanceShape(bodyMatrix); - const ndVector scale(ndFloat32(Width * UNREAL_INV_UNIT_SYSTEM), ndFloat32(Radio * UNREAL_INV_UNIT_SYSTEM), ndFloat32(Radio * UNREAL_INV_UNIT_SYSTEM), ndFloat32(0.0f)); - instance->SetScale(scale); + const FVector uScale(GetComponentTransform().GetScale3D()); + const ndVector scale1(ndFloat32(uScale.X), ndFloat32(uScale.Y), ndFloat32(uScale.Z), ndFloat32(1.0f)); + const ndVector scale2(ndFloat32(Width * UNREAL_INV_UNIT_SYSTEM), ndFloat32(Radio * UNREAL_INV_UNIT_SYSTEM), ndFloat32(Radio * UNREAL_INV_UNIT_SYSTEM), ndFloat32(0.0f)); + instance->SetScale(scale1 * scale2); const ndMatrix aligment(ndYawMatrix(ndPi * 0.5f)); instance->SetLocalMatrix(aligment * instance->GetLocalMatrix());