Skip to content

Commit

Permalink
more plugin work (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
JulioJerez committed Oct 24, 2024
1 parent 92ed2b5 commit be940c4
Show file tree
Hide file tree
Showing 15 changed files with 164 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<UNewtonRigidBody>(parent))
{
break;
}
}
const FTransform globalTransform(meshComponent->GetComponentToWorld());
const FTransform localTransform(globalTransform * bodyTransform.Inverse());

Expand Down Expand Up @@ -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);
Expand All @@ -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<UNewtonRigidBody>(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<UNewtonRigidBody>(component);
if (rigidBody)
{
return rigidBody->ShowDebug;
}
}
return false;
};

bool showDebug = GetDebugMode();
if (showDebug)
{
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<UStaticMeshComponent>(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<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(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<ndBigVector>& convexVertex = convexHull.GetVertexPool();
Expand All @@ -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<UNewtonRigidBody>(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;
}

Expand All @@ -116,7 +147,12 @@ void UNewtonCollisionConvexHull::ApplyPropertyChanges()
const USceneComponent* const parent = Cast<UStaticMeshComponent>(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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Copyright Epic Games, Inc. All Rights Reserved.


#include "NewtonCommons.h"

#include "ThirdParty/newtonLibrary/Public/dNewton/ndNewton.h"

ndHullPoints::ndHullPoints()
:TArray<FVector3f>()
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
{
Expand All @@ -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());
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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);

Expand Down
Loading

0 comments on commit be940c4

Please sign in to comment.