Skip to content

Commit

Permalink
Update joltc to d5d88fb16a03f3dd236eaa54d60a0a34bfe898c9
Browse files Browse the repository at this point in the history
  • Loading branch information
seep committed Aug 31, 2024
1 parent 21c82f4 commit 2b44b29
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Jolt.Native~/lib/README
Original file line number Diff line number Diff line change
@@ -1 +1 @@
joltc forked from https://github.com/amerkoleci/JoltPhysicsSharp/tree/c80f11729ae8011f22825b34db90926351b2dd32/src/joltc
joltc forked from https://github.com/amerkoleci/JoltPhysicsSharp/tree/d5d88fb16a03f3dd236eaa54d60a0a34bfe898c9/src/joltc
139 changes: 126 additions & 13 deletions Jolt.Native~/lib/joltc/joltc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ JPH_SUPPRESS_WARNINGS
#include "Jolt/Physics/Collision/CollideShape.h"
#include <Jolt/Physics/Collision/CollisionCollectorImpl.h>
#include <Jolt/Physics/Collision/ShapeCast.h>
#include "Jolt/Physics/Collision/Shape/PlaneShape.h"
#include "Jolt/Physics/Collision/Shape/BoxShape.h"
#include "Jolt/Physics/Collision/Shape/SphereShape.h"
#include "Jolt/Physics/Collision/Shape/TriangleShape.h"
Expand Down Expand Up @@ -66,16 +67,26 @@ JPH_SUPPRESS_WARNINGS
using namespace JPH;

// Callback for traces, connect this to your own trace function if you have one
static void TraceImpl(const char* inFMT, ...)
static JPH_TraceFunc s_TraceFunc = nullptr;

static void TraceImpl(const char* fmt, ...)
{
// Format the message
va_list list;
va_start(list, inFMT);
char buffer[1024];
vsnprintf(buffer, sizeof(buffer), inFMT, list);
va_list list;
va_start(list, fmt);
char buffer[1024];
vsnprintf(buffer, sizeof(buffer), fmt, list);
va_end(list);

// Print to the TTY
std::cout << buffer << std::endl;
if(s_TraceFunc)
{
s_TraceFunc(buffer);
}
else
{
std::cout << buffer << std::endl;
}
}

#ifdef JPH_ENABLE_ASSERTS
Expand Down Expand Up @@ -310,7 +321,9 @@ void JPH_MassProperties_DecomposePrincipalMomentsOfInertia(JPH_MassProperties* p
void JPH_MassProperties_ScaleToMass(JPH_MassProperties* properties, float mass)
{
JPH::MassProperties joltProperties = ToJolt(properties);
joltProperties.ScaleToMass(mass);
joltProperties.ScaleToMass(mass);
properties->mass = joltProperties.mMass;
FromJolt(joltProperties.mInertia, &properties->inertia);
}

static JPH::Triangle ToTriangle(const JPH_Triangle& triangle)
Expand All @@ -320,13 +333,14 @@ static JPH::Triangle ToTriangle(const JPH_Triangle& triangle)

static JPH::IndexedTriangle ToIndexedTriangle(const JPH_IndexedTriangle& triangle)
{
return JPH::IndexedTriangle(triangle.i1, triangle.i2, triangle.i3, triangle.materialIndex);
return JPH::IndexedTriangle(triangle.i1, triangle.i2, triangle.i3, triangle.materialIndex, triangle.userData);
}

static JPH::TempAllocatorImpl* s_TempAllocator = nullptr;
static JPH::JobSystemThreadPool* s_JobSystem = nullptr;
// 10 MB was not enough for large simulation, let's use TempAllocatorMalloc
static TempAllocator* s_TempAllocator = nullptr;
static JobSystemThreadPool* s_JobSystem = nullptr;

JPH_Bool32 JPH_Init(uint32_t tempAllocatorSize)
JPH_Bool32 JPH_Init(void)
{
JPH::RegisterDefaultAllocator();

Expand All @@ -341,7 +355,7 @@ JPH_Bool32 JPH_Init(uint32_t tempAllocatorSize)
JPH::RegisterTypes();

// Init temp allocator
s_TempAllocator = new TempAllocatorImpl(tempAllocatorSize ? tempAllocatorSize : 10 * 1024 * 1024);
s_TempAllocator = new TempAllocatorImplWithMallocFallback(8 * 1024 * 1024);

// Init Job system.
s_JobSystem = new JPH::JobSystemThreadPool(JPH::cMaxPhysicsJobs, JPH::cMaxPhysicsBarriers, (int)std::thread::hardware_concurrency() - 1);
Expand All @@ -362,6 +376,11 @@ void JPH_Shutdown(void)
JPH::Factory::sInstance = nullptr;
}

void JPH_SetTraceHandler(JPH_TraceFunc handler)
{
s_TraceFunc = handler;
}

void JPH_SetAssertFailureHandler(JPH_AssertFailureFunc handler)
{
#ifdef JPH_ENABLE_ASSERTS
Expand Down Expand Up @@ -794,6 +813,24 @@ void JPH_Quaternion_FromTo(const JPH_Vec3* from, const JPH_Vec3* to, JPH_Quat* q
FromJolt(JPH::Quat::sFromTo(ToJolt(from), ToJolt(to)), quat);
}

/* Material */
JPH_PhysicsMaterial* JPH_PhysicsMaterial_Create(void)
{
auto material = new JPH::PhysicsMaterial();
material->AddRef();

return reinterpret_cast<JPH_PhysicsMaterial*>(material);
}

void JPH_PhysicsMaterial_Destroy(JPH_PhysicsMaterial* material)
{
if (material)
{
auto joltMaterial = reinterpret_cast<JPH::PhysicsMaterial*>(material);
joltMaterial->Release();
}
}

/* ShapeSettings */
void JPH_ShapeSettings_Destroy(JPH_ShapeSettings* settings)
{
Expand Down Expand Up @@ -917,7 +954,49 @@ float JPH_SphereShape_GetRadius(const JPH_SphereShape* shape)
return reinterpret_cast<const JPH::SphereShape*>(shape)->GetRadius();
}

/* TriangleShapeSettings */
/* PlaneShape */
JPH_PlaneShapeSettings* JPH_PlaneShapeSettings_Create(const JPH_Plane* plane, const JPH_PhysicsMaterial* material, float halfExtent)
{
const JPH::PhysicsMaterial* joltMaterial = material != nullptr ? reinterpret_cast<const JPH::PhysicsMaterial*>(material) : nullptr;

auto settings = new JPH::PlaneShapeSettings(ToJolt(plane), joltMaterial, halfExtent);
settings->AddRef();

return reinterpret_cast<JPH_PlaneShapeSettings*>(settings);
}

JPH_PlaneShape* JPH_PlaneShapeSettings_CreateShape(const JPH_PlaneShapeSettings* settings)
{
const JPH::PlaneShapeSettings* joltSettings = reinterpret_cast<const JPH::PlaneShapeSettings*>(settings);
auto shape_res = joltSettings->Create();

auto shape = shape_res.Get().GetPtr();
shape->AddRef();

return reinterpret_cast<JPH_PlaneShape*>(shape);
}

JPH_PlaneShape* JPH_PlaneShape_Create(const JPH_Plane* plane, const JPH_PhysicsMaterial* material, float halfExtent)
{
const JPH::PhysicsMaterial* joltMaterial = material != nullptr ? reinterpret_cast<const JPH::PhysicsMaterial*>(material) : nullptr;

auto shape = new JPH::PlaneShape(ToJolt(plane), joltMaterial, halfExtent);
shape->AddRef();

return reinterpret_cast<JPH_PlaneShape*>(shape);
}

void JPH_PlaneShape_GetPlane(const JPH_PlaneShape* shape, JPH_Plane* result)
{
FromJolt(reinterpret_cast<const JPH::PlaneShape*>(shape)->GetPlane(), result);
}

float JPH_PlaneShape_GetHalfExtent(const JPH_PlaneShape* shape)
{
return reinterpret_cast<const JPH::PlaneShape*>(shape)->GetHalfExtent();
}

/* TriangleShape */
JPH_TriangleShapeSettings* JPH_TriangleShapeSettings_Create(const JPH_Vec3* v1, const JPH_Vec3* v2, const JPH_Vec3* v3, float convexRadius)
{
auto settings = new JPH::TriangleShapeSettings(ToJolt(v1), ToJolt(v2), ToJolt(v3), convexRadius);
Expand Down Expand Up @@ -1510,6 +1589,40 @@ float JPH_Shape_GetVolume(const JPH_Shape* shape)
return reinterpret_cast<const JPH::Shape*>(shape)->GetVolume();
}

JPH_Bool32 JPH_Shape_CastRay(const JPH_Shape* shape, const JPH_Vec3* origin, const JPH_Vec3* direction, JPH_RayCastResult* hit)
{
JPH_ASSERT(shape && origin && direction && hit);

auto joltShape = reinterpret_cast<const JPH::Shape*>(shape);
JPH::RayCast ray(ToJolt(origin), ToJolt(direction));
SubShapeIDCreator creator;
RayCastResult result;

bool hadHit = joltShape->CastRay(ray, creator, result);

if (hadHit)
{
hit->fraction = result.mFraction;
hit->bodyID = result.mBodyID.GetIndexAndSequenceNumber();
hit->subShapeID2 = result.mSubShapeID2.GetValue();
}

return static_cast<JPH_Bool32>(hadHit);
}

JPH_Bool32 JPH_Shape_CollidePoint(const JPH_Shape* shape, JPH_Vec3* point)
{
JPH_ASSERT(shape && point);

auto joltShape = reinterpret_cast<const JPH::Shape*>(shape);
SubShapeIDCreator creator;
AnyHitCollisionCollector<CollidePointCollector> collector;

joltShape->CollidePoint(ToJolt(point), creator, collector);

return static_cast<JPH_Bool32>(collector.HadHit());
}

/* JPH_BodyCreationSettings */
JPH_BodyCreationSettings* JPH_BodyCreationSettings_Create(void)
{
Expand Down
22 changes: 20 additions & 2 deletions Jolt.Native~/lib/joltc/joltc.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ typedef struct JPH_IndexedTriangle {
uint32_t i2;
uint32_t i3;
uint32_t materialIndex;
uint32_t userData;
} JPH_IndexedTriangle;

typedef struct JPH_MassProperties {
Expand Down Expand Up @@ -406,6 +407,7 @@ typedef struct JPH_ShapeSettings JPH_ShapeSettings;
typedef struct JPH_ConvexShapeSettings JPH_ConvexShapeSettings;
typedef struct JPH_SphereShapeSettings JPH_SphereShapeSettings;
typedef struct JPH_BoxShapeSettings JPH_BoxShapeSettings;
typedef struct JPH_PlaneShapeSettings JPH_PlaneShapeSettings;
typedef struct JPH_TriangleShapeSettings JPH_TriangleShapeSettings;
typedef struct JPH_CapsuleShapeSettings JPH_CapsuleShapeSettings;
typedef struct JPH_TaperedCapsuleShapeSettings JPH_TaperedCapsuleShapeSettings;
Expand All @@ -423,6 +425,7 @@ typedef struct JPH_Shape JPH_Shape;
typedef struct JPH_ConvexShape JPH_ConvexShape;
typedef struct JPH_SphereShape JPH_SphereShape;
typedef struct JPH_BoxShape JPH_BoxShape;
typedef struct JPH_PlaneShape JPH_PlaneShape;
typedef struct JPH_CapsuleShape JPH_CapsuleShape;
typedef struct JPH_CylinderShape JPH_CylinderShape;
typedef struct JPH_TriangleShape JPH_TriangleShape;
Expand Down Expand Up @@ -521,10 +524,12 @@ typedef struct JPH_CharacterContactListener JPH_CharacterContactListener;
typedef struct JPH_CharacterVirtualSettings JPH_CharacterVirtualSettings; /* Inherics JPH_CharacterBaseSettings */
typedef struct JPH_CharacterVirtual JPH_CharacterVirtual; /* Inherics JPH_CharacterBase */

typedef void(JPH_API_CALL* JPH_TraceFunc)(const char* mssage);
typedef JPH_Bool32(JPH_API_CALL* JPH_AssertFailureFunc)(const char* expression, const char* mssage, const char* file, uint32_t line);

JPH_CAPI JPH_Bool32 JPH_Init(uint32_t tempAllocatorSize);
JPH_CAPI JPH_Bool32 JPH_Init(void);
JPH_CAPI void JPH_Shutdown(void);
JPH_CAPI void JPH_SetTraceHandler(JPH_TraceFunc handler);
JPH_CAPI void JPH_SetAssertFailureHandler(JPH_AssertFailureFunc handler);

/* JPH_BroadPhaseLayerInterface */
Expand Down Expand Up @@ -638,6 +643,10 @@ JPH_CAPI void JPH_PhysicsSystem_GetConstraints(const JPH_PhysicsSystem* system,
/* Math */
JPH_CAPI void JPH_Quaternion_FromTo(const JPH_Vec3* from, const JPH_Vec3* to, JPH_Quat* quat);

/* Material */
JPH_CAPI JPH_PhysicsMaterial* JPH_PhysicsMaterial_Create(void);
JPH_CAPI void JPH_PhysicsMaterial_Destroy(JPH_PhysicsMaterial* material);

/* JPH_ShapeSettings */
JPH_CAPI void JPH_ShapeSettings_Destroy(JPH_ShapeSettings* settings);

Expand All @@ -655,14 +664,21 @@ JPH_CAPI void JPH_BoxShape_GetHalfExtent(const JPH_BoxShape* shape, JPH_Vec3* ha
JPH_CAPI float JPH_BoxShape_GetVolume(const JPH_BoxShape* shape);
JPH_CAPI float JPH_BoxShape_GetConvexRadius(const JPH_BoxShape* shape);

/* SphereShapeSettings */
/* SphereShape */
JPH_CAPI JPH_SphereShapeSettings* JPH_SphereShapeSettings_Create(float radius);
JPH_CAPI JPH_SphereShape* JPH_SphereShapeSettings_CreateShape(const JPH_SphereShapeSettings* settings);
JPH_CAPI float JPH_SphereShapeSettings_GetRadius(const JPH_SphereShapeSettings* settings);
JPH_CAPI void JPH_SphereShapeSettings_SetRadius(JPH_SphereShapeSettings* settings, float radius);
JPH_CAPI JPH_SphereShape* JPH_SphereShape_Create(float radius);
JPH_CAPI float JPH_SphereShape_GetRadius(const JPH_SphereShape* shape);

/* PlaneShape */
JPH_CAPI JPH_PlaneShapeSettings* JPH_PlaneShapeSettings_Create(const JPH_Plane* plane, const JPH_PhysicsMaterial* material, float halfExtent);
JPH_CAPI JPH_PlaneShape* JPH_PlaneShapeSettings_CreateShape(const JPH_PlaneShapeSettings* settings);
JPH_CAPI JPH_PlaneShape* JPH_PlaneShape_Create(const JPH_Plane* plane, const JPH_PhysicsMaterial* material, float halfExtent);
JPH_CAPI void JPH_PlaneShape_GetPlane(const JPH_PlaneShape* shape, JPH_Plane* result);
JPH_CAPI float JPH_PlaneShape_GetHalfExtent(const JPH_PlaneShape* shape);

/* TriangleShape */
JPH_CAPI JPH_TriangleShapeSettings* JPH_TriangleShapeSettings_Create(const JPH_Vec3* v1, const JPH_Vec3* v2, const JPH_Vec3* v3, float convexRadius);
JPH_CAPI JPH_TriangleShape* JPH_TriangleShapeSettings_CreateShape(const JPH_TriangleShapeSettings* settings);
Expand Down Expand Up @@ -764,6 +780,8 @@ JPH_CAPI float JPH_Shape_GetInnerRadius(const JPH_Shape* shape);
JPH_CAPI void JPH_Shape_GetMassProperties(const JPH_Shape* shape, JPH_MassProperties* result);
JPH_CAPI void JPH_Shape_GetSurfaceNormal(const JPH_Shape* shape, JPH_SubShapeID subShapeID, JPH_Vec3* localPosition, JPH_Vec3* normal);
JPH_CAPI float JPH_Shape_GetVolume(const JPH_Shape* shape);
JPH_CAPI JPH_Bool32 JPH_Shape_CastRay(const JPH_Shape* shape, const JPH_Vec3* origin, const JPH_Vec3* direction, JPH_RayCastResult* hit);
JPH_CAPI JPH_Bool32 JPH_Shape_CollidePoint(const JPH_Shape* shape, JPH_Vec3* point);

/* JPH_BodyCreationSettings */
JPH_CAPI JPH_BodyCreationSettings* JPH_BodyCreationSettings_Create(void);
Expand Down
9 changes: 0 additions & 9 deletions Jolt.Native~/lib/joltc/joltc_assert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ __pragma(warning(push, 0))
__pragma(warning(pop))
#endif

#ifdef JPH_COMPILER_GCC
JPH_GCC_SUPPRESS_WARNING("-Winvalid-offsetof")
#endif

#define ENSURE_SIZE_ALIGN(type0, type1) \
static_assert(sizeof(type0) == sizeof(type1)); \
static_assert(alignof(type0) == alignof(type1))
Expand Down Expand Up @@ -181,9 +177,4 @@ static_assert(JPH_BackFaceMode_CollideWithBackFaces == (int)JPH::EBackFaceMode::
static_assert(sizeof(JPH::SubShapeIDPair) == sizeof(JPH_SubShapeIDPair));
static_assert(alignof(JPH::SubShapeIDPair) == alignof(JPH_SubShapeIDPair));

ENSURE_SIZE_ALIGN(JPH::RayCastResult, JPH_RayCastResult);
static_assert(offsetof(JPH::RayCastResult, mBodyID) == offsetof(JPH_RayCastResult, bodyID));
static_assert(offsetof(JPH::RayCastResult, mFraction) == offsetof(JPH_RayCastResult, fraction));
static_assert(offsetof(JPH::RayCastResult, mSubShapeID2) == offsetof(JPH_RayCastResult, subShapeID2));

//static_assert(offsetof(JPH::MassProperties, mMass) == offsetof(JPH_MassProperties, mass));

0 comments on commit 2b44b29

Please sign in to comment.