Skip to content

Commit

Permalink
made convex hull bake only the scale
Browse files Browse the repository at this point in the history
  • Loading branch information
JulioJerez committed Nov 4, 2024
1 parent e1841a3 commit 496dc6c
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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
Expand Down Expand Up @@ -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)) &&
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ class FnewtonModule::ResourceCache
ndTree<ndShape*, long long> m_shapeCache;
Cache<ndConvexHullSet> m_convexVhacdCache;
Cache<UE::Geometry::FDynamicMesh3> m_visualMeshCache;

ndMovingAverage<16> m_renderTime;
ndMovingAverage<16> m_physicsTime;
};

void FnewtonModule::StartupModule()
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,34 +58,20 @@ 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));

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<UNewtonRigidBody>(parent))
{
break;
}
}

const FTransform globalTransform(GetComponentToWorld());
const FTransform localTransform(globalTransform * bodyTransform.Inverse());
const ndMatrix localMatrix(ToNewtonMatrix(localTransform));

ndArray<ndBigVector> 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<ndBigVector>& convexVertex = convexHull.GetVertexPool();
Expand All @@ -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();
Expand All @@ -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()
Expand All @@ -160,4 +136,24 @@ void UNewtonCollisionConvexHull::ApplyPropertyChanges()

BuildNewtonShape();
Super::ApplyPropertyChanges();
}
}

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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 496dc6c

Please sign in to comment.