Skip to content

Commit

Permalink
some modification for unreal plugin (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
JulioJerez committed Oct 6, 2024
1 parent 009db3d commit 7bd65cf
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 123 deletions.
73 changes: 37 additions & 36 deletions newton-4.00/applications/ndSandbox/demos/ndStaticMeshCollision.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
#include "ndPhysicsWorld.h"
#include "ndCompoundScene.h"
#include "ndMakeStaticMap.h"
#include "ndDemoDebugMesh.h"
#include "ndDemoEntityManager.h"
#include "ndBasicPlayerCapsule.h"
#include "ndHeightFieldPrimitive.h"

#if 1
#if 0
void ndStaticMeshCollisionDemo (ndDemoEntityManager* const scene)
{
ndMatrix heighfieldLocation (ndGetIdentityMatrix());
Expand Down Expand Up @@ -93,85 +94,79 @@ void ndStaticMeshCollisionDemo (ndDemoEntityManager* const scene)
ndVector origin(-3.0f, 0.0f, 2.0f, 1.0f);
scene->SetCameraMatrix(rot, origin);
}
#else

#if 1
#elif 1

#include "ndDemoDebugMesh.h"
class CConvexCaster : public ndModel
class CConvexCasterModelNotify : public ndModelNotify
{
public:
class CConvexCastCallBack : public ndConvexCastNotify
{
public:
CConvexCastCallBack()
: ndConvexCastNotify()
{}
virtual ndUnsigned32 OnRayPrecastAction(const ndBody* const body, const ndShapeInstance* const) override
{
// filter the floor
ndUnsigned32 ret = ndUnsigned32(body->GetInvMass() ? 1 : 0);
return ret;
}
virtual ndFloat32 OnRayCastAction(const ndContactPoint&, ndFloat32) override
{
return 0;
}
};

CConvexCaster(ndDemoEntityManager* const pScene, ndBodyKinematic* const pParentBody)
: ndModel()
, m_CastShape(new ndShapeSphere(5.0))
, m_pParentBody(pParentBody)
, m_pKinJoint(nullptr)
, m_pScene(pScene)
public:
CConvexCasterModelNotify(ndDemoEntityManager* const scene, ndBodyKinematic* const pParentBody)
:ndModelNotify()
,m_CastShape(new ndShapeSphere(5.0))
,m_pScene(scene)
,m_pParentBody(pParentBody)
,m_pKinJoint(nullptr)
{
}

virtual void OnAddToWorld() override {}
virtual void OnRemoveFromToWorld() override {}
virtual void Debug(ndConstraintDebugCallback&) const override
{
ndWireFrameDebugMesh sharedEdgeMesh (m_pScene->GetShaderCache(), &m_CastShape);
const ndVector color(0.5f, 0.5f, 0.5f, 1.0f);
sharedEdgeMesh.SetColor(color);
sharedEdgeMesh.Render(m_pScene, ndGetIdentityMatrix());
}

virtual void Update(ndWorld* const, ndFloat32) override
virtual void Update(ndWorld* const world, ndFloat32) override
{
CConvexCastCallBack castCallback;
//if (m_world->ConvexCast(castCallback, m_CastShape, ndGetIdentityMatrix(), ndVector(0.0, 0.001, 0.0, 1.0)))
//if (m_world->ConvexCast(castCallback, m_CastShape, ndGetIdentityMatrix(), ndVector(0.0, 0.0, 0.001, 1.0)))
if (m_world->ConvexCast(castCallback, m_CastShape, ndGetIdentityMatrix(), ndVector(0.001, 0.0, 0.0, 1.0)))
if (world->ConvexCast(castCallback, m_CastShape, ndGetIdentityMatrix(), ndVector(0.001, 0.0, 0.0, 1.0)))
{
if (castCallback.m_contacts.GetCount() > 0)
{
ndBodyKinematic* const pHitBody = (ndBodyKinematic*)castCallback.m_contacts[0].m_body1;
ndAssert(pHitBody);
auto pUserData = static_cast<ndDemoEntity*>(pHitBody->GetNotifyCallback()->GetUserData());
ndAssert(pUserData->GetName() == "My Collision Object");
//auto pUserData = static_cast<ndDemoEntity*>(pHitBody->GetNotifyCallback()->GetUserData());
//ndAssert(pUserData->GetName() == "My Collision Object");
if (!m_pKinJoint)
{
m_pKinJoint = new ndJointKinematicController(pHitBody, m_pParentBody, castCallback.m_contacts[0].m_point);
m_pKinJoint->SetMaxAngularFriction(1000);
m_pKinJoint->SetMaxLinearFriction(1000);
ndSharedPtr<ndJointBilateralConstraint> jointPtr(m_pKinJoint);
m_world->AddJoint(jointPtr);
world->AddJoint(jointPtr);
}
}
}
}

virtual void Debug(ndConstraintDebugCallback&) const override
{
ndWireFrameDebugMesh sharedEdgeMesh = ndWireFrameDebugMesh(m_pScene->GetShaderCache(), &m_CastShape);
const ndVector color(0.5f, 0.5f, 0.5f, 1.0f);
sharedEdgeMesh.SetColor(color);
sharedEdgeMesh.Render(m_pScene, ndGetIdentityMatrix());
}
private:
ndShapeInstance m_CastShape;
ndDemoEntityManager* m_pScene;
ndBodyKinematic* m_pParentBody;
ndJointKinematicController* m_pKinJoint;
ndDemoEntityManager* m_pScene;
};

static ndBodyKinematic* BuildHeightField(ndDemoEntityManager* const scene)
Expand All @@ -187,7 +182,9 @@ static ndBodyKinematic* BuildHeightField(ndDemoEntityManager* const scene)
auto pShapeHeightField = shape.GetShape()->GetAsShapeHeightfield();

for (int i = 0; i < iDim * iDim; ++i)
pShapeHeightField->GetElevationMap()[i] = ndReal (rand() * 2.0 * dMaxHeight / RAND_MAX);
{
pShapeHeightField->GetElevationMap()[i] = ndReal(rand() * 2.0 * dMaxHeight / RAND_MAX);
}

pShapeHeightField->UpdateElevationMapAabb();
ndMatrix uvMatrix(ndGetIdentityMatrix());
Expand Down Expand Up @@ -225,15 +222,20 @@ void ndStaticMeshCollisionDemo(ndDemoEntityManager* const scene)
// build the height field
auto pFloorBody = BuildHeightField(scene);
AddBody(scene);
ndSharedPtr<ndModel> convexCaster(new CConvexCaster(scene, pFloorBody));
scene->GetWorld()->AddModel(convexCaster);

ndModel* const model = new ndModel();
model->SetNotifyCallback(new CConvexCasterModelNotify(scene, pFloorBody));
//ndSharedPtr<ndModel> convexCaster(new CConvexCaster(scene, pFloorBody));
//scene->GetWorld()->AddModel(convexCaster);
scene->GetWorld()->AddModel(model);

ndQuaternion rot;
ndVector origin(-10.0f, 5.0f, 0.0f, 1.0f);
scene->SetCameraMatrix(rot, origin);
}

#else

ndBodyKinematic* AddChamferCylinder(ndDemoEntityManager* const scene, const ndMatrix& location, ndFloat32 mass, ndFloat32 radius, ndFloat32 width, const char* const textName)
{
ndShapeInstance shape(new ndShapeChamferCylinder(0.5, 1.0));
Expand Down Expand Up @@ -300,5 +302,4 @@ void ndStaticMeshCollisionDemo(ndDemoEntityManager* const scene)
scene->SetCameraMatrix(rot, origin);
}

#endif
#endif
18 changes: 11 additions & 7 deletions newton-4.00/applications/ndSandbox/ndDemoEntity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -474,21 +474,25 @@ ndShapeInstance* ndDemoEntity::CreateCompoundFromMesh(bool lowDetail)
mesh->GetVertexArray(points);
mesh->GetIndexArray(indices);

ndArray<ndTriplex> meshPoints;
//ndArray<ndTriplex> meshPoints;
ndArray<ndReal> meshPoints;
for (ndInt32 i = 0; i < points.GetCount(); ++i)
{
ndTriplex p;
p.m_x = points[i].m_x;
p.m_y = points[i].m_y;
p.m_z = points[i].m_z;
meshPoints.PushBack(p);
//ndTriplex p;
//p.m_x = points[i].m_x;
//p.m_y = points[i].m_y;
//p.m_z = points[i].m_z;
//meshPoints.PushBack(p);
meshPoints.PushBack(ndReal(points[i].m_x));
meshPoints.PushBack(ndReal(points[i].m_y));
meshPoints.PushBack(ndReal(points[i].m_z));
}

nd_::VHACD::IVHACD* const interfaceVHACD = nd_::VHACD::CreateVHACD();
nd_::VHACD::IVHACD::Parameters paramsVHACD;
//paramsVHACD.m_maxConvexHulls = 24;
paramsVHACD.m_concavityToVolumeWeigh = lowDetail ? 1.0f : 0.5f;
interfaceVHACD->Compute(&meshPoints[0].m_x, uint32_t(points.GetCount()), (uint32_t*)&indices[0], uint32_t(indices.GetCount()) / 3, paramsVHACD);
interfaceVHACD->Compute(&meshPoints[0], uint32_t(points.GetCount() / 3), (uint32_t*)&indices[0], uint32_t(indices.GetCount() / 3), paramsVHACD);

ndShapeInstance* const compoundShapeInstance = new ndShapeInstance(new ndShapeCompound());

Expand Down
4 changes: 2 additions & 2 deletions newton-4.00/applications/ndSandbox/ndDemoEntityManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
//#define DEFAULT_SCENE 0 // basic rigidbody
//#define DEFAULT_SCENE 1 // gpu basic rigidbody
//#define DEFAULT_SCENE 2 // friction ramp
//#define DEFAULT_SCENE 3 // basic compound shapes
#define DEFAULT_SCENE 3 // basic compound shapes
//#define DEFAULT_SCENE 4 // conservation of momentum
//#define DEFAULT_SCENE 5 // basic Stacks
//#define DEFAULT_SCENE 6 // basic Trigger
Expand All @@ -58,7 +58,7 @@
//#define DEFAULT_SCENE 18 // cart pole continue controller
//#define DEFAULT_SCENE 19 // unit cycle controller
//#define DEFAULT_SCENE 20 // simple industrial robot
#define DEFAULT_SCENE 21 // advanced industrial robot
//#define DEFAULT_SCENE 21 // advanced industrial robot
//#define DEFAULT_SCENE 22 // quadruped test 1
//#define DEFAULT_SCENE 23 // quadruped test 2
//#define DEFAULT_SCENE 24 // quadruped test 3
Expand Down
13 changes: 12 additions & 1 deletion newton-4.00/sdk/dBrain/ndBrainLayerActivationSigmoid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,24 @@ void ndBrainLayerActivationSigmoid::MakePrediction(const ndBrainVector& input, n
output[i] = ndBrainFloat(ndExp(-value));
ndAssert(ndCheckFloat(output[i]));
}

//for (ndInt32 i = ndInt32(input.GetCount() - 1); i >= 0; --i)
//{
// output[i] = ndBrainFloat(1.0f) / (ndBrainFloat(1.0f) + output[i]);
// ndAssert(output[i] <= ndBrainFloat(1.0f));
// ndAssert(output[i] >= ndBrainFloat(0.0f));
//}

for (ndInt32 i = ndInt32(input.GetCount() - 1); i >= 0; --i)
{
output[i] = ndBrainFloat(1.0f) / (ndBrainFloat(1.0f) + output[i]);
ndBrainFloat value = ndClamp(input[i], ndBrainFloat(-30.0f), ndBrainFloat(30.0f));
ndBrainFloat x = ndBrainFloat(ndExp(-value));
output[i] = ndBrainFloat(1.0f) / (ndBrainFloat(1.0f) + x);
ndAssert(ndCheckFloat(output[i]));
ndAssert(output[i] <= ndBrainFloat(1.0f));
ndAssert(output[i] >= ndBrainFloat(0.0f));
}

output.FlushToZero();
}

Expand Down
54 changes: 54 additions & 0 deletions newton-4.00/sdk/dCore/ndCRC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "ndCoreStdafx.h"
#include "ndCRC.h"

#if 0
static ndUnsigned64 randBits0[] =
{
static_cast<ndUnsigned64>(7266447313870364031ULL), static_cast<ndUnsigned64>(4946485549665804864ULL), static_cast<ndUnsigned64>(16945909448695747420ULL), static_cast<ndUnsigned64>(16394063075524226720ULL),
Expand Down Expand Up @@ -128,6 +129,59 @@ ndUnsigned64 ndCRC64 (const void* const buffer, ndInt32 size, ndUnsigned64 crcAc
return crcAcc;
}

#else

ndUnsigned64 ndCRC64(const char* const name, ndUnsigned64 crcAcc)
{
// https://en.wikipedia.org/wiki/Jenkins_hash_function
// the simplest hash function I could find from wikipedia
// no sure how uniform distributed it is,
// but it seems pretty good so far.
//size_t i = 0;
//hash = 0;
//for (ndInt32 i = 0; i < size; ++i)
// hash += key[i++];
// hash += hash << 10;
// hash ^= hash >> 6;
//}
//hash += hash << 3;
//hash ^= hash >> 11;
//hash += hash << 15;
//return hash;

if (name)
{
for (ndInt32 i = 0; name[i]; ++i)
{
char c = name[i];
crcAcc += c;
crcAcc += crcAcc << 10;
crcAcc ^= crcAcc >> 6;
}
crcAcc += crcAcc << 3;
crcAcc ^= crcAcc >> 11;
crcAcc += crcAcc << 15;
}
return crcAcc;
}

ndUnsigned64 ndCRC64(const void* const buffer, ndInt32 size, ndUnsigned64 crcAcc)
{
const unsigned char* const ptr = (unsigned char*)buffer;
for (ndInt32 i = 0; i < size; ++i)
{
unsigned char c = ptr[i];
crcAcc += c;
crcAcc += crcAcc << 10;
crcAcc ^= crcAcc >> 6;
}
crcAcc += crcAcc << 3;
crcAcc ^= crcAcc >> 11;
crcAcc += crcAcc << 15;
return crcAcc;
}

#endif



6 changes: 6 additions & 0 deletions newton-4.00/sdk/dCore/ndSharedPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class ndSharedPtr
const T* operator* () const;

operator bool() const;
ndInt32 GetRefCount() const;

private:
class ndRefCounter : public ndAtomic<ndInt32>, public ndContainersFreeListAlloc<ndRefCounter>
Expand Down Expand Up @@ -161,5 +162,10 @@ ndSharedPtr<T>::operator bool() const
return m_ptr != nullptr;
}

template <typename T>
ndInt32 ndSharedPtr<T>::GetRefCount() const
{
return m_references->load();
}
#endif

46 changes: 8 additions & 38 deletions newton-4.00/sdk/dNewton/dModels/ndModelNotify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,61 +38,31 @@ ndModelNotify::ndModelNotify()
ndModelNotify* ndModelNotify::Clone() const
{
ndAssert(0);
return nullptr;
return new ndModelNotify();
}

ndModelNotify::~ndModelNotify()
{
}

#if 0
ndBody* ndModelNotify::GetBody()
{
return m_body;
}

const ndBody* ndModelNotify::GetBody() const
{
return m_body;
}

void* ndModelNotify::GetUserData() const
{
return nullptr;
}

ndVector ndModelNotify::GetGravity() const
ndModel* ndModelNotify::GetModel() const
{
return m_defaultGravity;
return m_model;
}

void ndModelNotify::SetGravity(const ndVector & defaultGravity)
void ndModelNotify::Debug(ndConstraintDebugCallback&) const
{
m_defaultGravity = defaultGravity;
}

void ndModelNotify::OnTransform(ndInt32, const ndMatrix&)

void ndModelNotify::Update(ndWorld* const, ndFloat32)
{
}

bool ndModelNotify::OnSceneAabbOverlap(const ndBody* const) const
void ndModelNotify::PostUpdate(ndWorld* const, ndFloat32)
{
return true;
}

void ndModelNotify::OnApplyExternalForce(ndInt32, ndFloat32)
void ndModelNotify::PostTransformUpdate(ndWorld* const, ndFloat32)
{
ndBodyKinematic* const body = GetBody()->GetAsBodyKinematic();
ndAssert(body);
if (body->GetInvMass() > 0.0f)
{
ndVector massMatrix(body->GetMassMatrix());
ndVector force (m_defaultGravity.Scale(massMatrix.m_w));
body->SetForce(force);
body->SetTorque(ndVector::m_zero);

//ndVector L(body->CalculateAngularMomentum());
//dTrace(("%f %f %f\n", L.m_x, L.m_y, L.m_z));
}
}
#endif
Loading

0 comments on commit 7bd65cf

Please sign in to comment.