Skip to content

Commit

Permalink
ConvexHullShape
Browse files Browse the repository at this point in the history
  • Loading branch information
LPGhatguy committed Apr 29, 2024
1 parent 759d880 commit 6647048
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 8 deletions.
4 changes: 2 additions & 2 deletions HelloWorld/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ static JPC_ObjectLayerPairFilterFns Hello_OVO = {
};

void Hello_Debug_DrawLine(const void *self, JPC_RVec3 inFrom, JPC_RVec3 inTo, JPC_Color inColor) {
printf("Draw line from (%f, %f, %f) to (%f, %f, %f) with color (%d, %d, %d)\n",
inFrom.x, inFrom.y, inFrom.z, inTo.x, inTo.y, inTo.z, inColor.r, inColor.g, inColor.b);
// printf("Draw line from (%f, %f, %f) to (%f, %f, %f) with color (%d, %d, %d)\n",
// inFrom.x, inFrom.y, inFrom.z, inTo.x, inTo.y, inTo.z, inColor.r, inColor.g, inColor.b);
}

static JPC_DebugRendererSimpleFns Hello_DebugRenderer = {
Expand Down
26 changes: 24 additions & 2 deletions JoltC/Functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,28 @@ typedef struct JPC_CylinderShapeSettings {
JPC_API void JPC_CylinderShapeSettings_default(JPC_CylinderShapeSettings* object);
JPC_API bool JPC_CylinderShapeSettings_Create(const JPC_CylinderShapeSettings* self, JPC_Shape** outShape, JPC_String** outError);

////////////////////////////////////////////////////////////////////////////////
// ConvexHullShapeSettings -> ConvexShapeSettings -> ShapeSettings

typedef struct JPC_ConvexHullShapeSettings {
// ShapeSettings
uint64_t UserData;

// ConvexShapeSettings
// TODO: Material
float Density;

// ConvexHullShapeSettings
JPC_Vec3* Points;
size_t PointsLen;
float MaxConvexRadius;
float MaxErrorConvexRadius;
float HullTolerance;
} JPC_ConvexHullShapeSettings;

JPC_API void JPC_ConvexHullShapeSettings_default(JPC_ConvexHullShapeSettings* object);
JPC_API bool JPC_ConvexHullShapeSettings_Create(const JPC_ConvexHullShapeSettings* self, JPC_Shape** outShape, JPC_String** outError);

////////////////////////////////////////////////////////////////////////////////
// BodyCreationSettings

Expand Down Expand Up @@ -593,14 +615,14 @@ JPC_API void JPC_BodyInterface_AddLinearVelocity(JPC_BodyInterface *self, JPC_Bo
JPC_API void JPC_BodyInterface_AddLinearAndAngularVelocity(JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_Vec3 inLinearVelocity, JPC_Vec3 inAngularVelocity);
JPC_API void JPC_BodyInterface_SetAngularVelocity(JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_Vec3 inAngularVelocity);
JPC_API JPC_Vec3 JPC_BodyInterface_GetAngularVelocity(const JPC_BodyInterface *self, JPC_BodyID inBodyID);
JPC_API JPC_Vec3 JPC_BodyInterface_GetPointVelocity(const JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_RVec3 inPoint);
JPC_API JPC_Vec3 JPC_BodyInterface_GetPointVelocity(const JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_RVec3 inPoint);
JPC_API void JPC_BodyInterface_SetPositionRotationAndVelocity(JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_RVec3 inPosition, JPC_Quat inRotation, JPC_Vec3 inLinearVelocity, JPC_Vec3 inAngularVelocity);
JPC_API void JPC_BodyInterface_AddForce(JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_Vec3 inForce);

// JPC_API void JPC_BodyInterface_AddForce(JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_Vec3 inForce, JPC_RVec3 inPoint);

JPC_API void JPC_BodyInterface_AddTorque(JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_Vec3 inTorque);
JPC_API void JPC_BodyInterface_AddForceAndTorque(JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_Vec3 inForce, JPC_Vec3 inTorque);
JPC_API void JPC_BodyInterface_AddForceAndTorque(JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_Vec3 inForce, JPC_Vec3 inTorque);
JPC_API void JPC_BodyInterface_AddImpulse(JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_Vec3 inImpulse);

// JPC_API void JPC_BodyInterface_AddImpulse(JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_Vec3 inImpulse, JPC_RVec3 inPoint);
Expand Down
55 changes: 51 additions & 4 deletions JoltC/JoltC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <Jolt/Physics/Collision/Shape/CylinderShape.h>
#include <Jolt/Physics/Collision/Shape/SphereShape.h>
#include <Jolt/Physics/Collision/Shape/TriangleShape.h>
#include <Jolt/Physics/Collision/Shape/ConvexHullShape.h>
#include <Jolt/Physics/PhysicsSettings.h>
#include <Jolt/Physics/PhysicsSystem.h>
#include <Jolt/RegisterTypes.h>
Expand Down Expand Up @@ -62,7 +63,7 @@
static_assert(!std::is_polymorphic_v<cpp_type>, #cpp_type " is polymorphic and cannot be made layout compatible");

template<typename E>
constexpr auto to_integral(E e) -> typename std::underlying_type<E>::type
constexpr auto to_integral(E e) -> typename std::underlying_type<E>::type
{
return static_cast<typename std::underlying_type<E>::type>(e);
}
Expand Down Expand Up @@ -111,6 +112,17 @@ static JPH::Vec3 to_jph(JPC_Vec3 in) {
return JPH::Vec3(in.x, in.y, in.z);
}

static JPH::Array<JPH::Vec3> to_jph(const JPC_Vec3* src, size_t n) {
JPH::Array<JPH::Vec3> vec;
vec.resize(n);

if (src != nullptr) {
memcpy(vec.data(), src, n * sizeof(*src));
}

return vec;
}

static JPC_DVec3 to_jpc(JPH::DVec3 in) {
return JPC_DVec3{in.GetX(), in.GetY(), in.GetZ(), in.GetZ()};
}
Expand Down Expand Up @@ -537,6 +549,41 @@ JPC_API bool JPC_CylinderShapeSettings_Create(const JPC_CylinderShapeSettings* s
return HandleShapeResult(settings.Create(), outShape, outError);
}

////////////////////////////////////////////////////////////////////////////////
// ConvexHullShapeSettings

static void to_jph(const JPC_ConvexHullShapeSettings* input, JPH::ConvexHullShapeSettings* output) {
output->mUserData = input->UserData;

// TODO: Material
output->mDensity = input->Density;

output->mPoints = to_jph(input->Points, input->PointsLen);
output->mMaxConvexRadius = input->MaxConvexRadius;
output->mMaxErrorConvexRadius = input->MaxErrorConvexRadius;
output->mHullTolerance = input->HullTolerance;
}

JPC_API void JPC_ConvexHullShapeSettings_default(JPC_ConvexHullShapeSettings* object) {
object->UserData = 0;

// TODO: Material
object->Density = 1000.0;

object->Points = nullptr;
object->PointsLen = 0;
object->MaxConvexRadius = 0.0;
object->MaxErrorConvexRadius = 0.05f;
object->HullTolerance = 1.0e-3f;
}

JPC_API bool JPC_ConvexHullShapeSettings_Create(const JPC_ConvexHullShapeSettings* self, JPC_Shape** outShape, JPC_String** outError) {
JPH::ConvexHullShapeSettings settings;
to_jph(self, &settings);

return HandleShapeResult(settings.Create(), outShape, outError);
}

////////////////////////////////////////////////////////////////////////////////
// BodyCreationSettings

Expand Down Expand Up @@ -1044,7 +1091,7 @@ JPC_API void JPC_BodyInterface_SetLinearAndAngularVelocity(JPC_BodyInterface *se
JPC_API void JPC_BodyInterface_GetLinearAndAngularVelocity(const JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_Vec3 *outLinearVelocity, JPC_Vec3 *outAngularVelocity) {
JPH::Vec3 outLinVel;
JPH::Vec3 outAngVel;

to_jph(self)->GetLinearAndAngularVelocity(to_jph(inBodyID), outLinVel, outAngVel);

*outLinearVelocity = to_jpc(outLinVel);
Expand Down Expand Up @@ -1078,7 +1125,7 @@ JPC_API JPC_Vec3 JPC_BodyInterface_GetAngularVelocity(const JPC_BodyInterface *s
JPC_API JPC_Vec3 JPC_BodyInterface_GetPointVelocity(const JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_RVec3 inPoint) {
return to_jpc(to_jph(self)->GetPointVelocity(to_jph(inBodyID), to_jph(inPoint)));
}

JPC_API void JPC_BodyInterface_SetPositionRotationAndVelocity(JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_RVec3 inPosition, JPC_Quat inRotation, JPC_Vec3 inLinearVelocity, JPC_Vec3 inAngularVelocity) {
return to_jph(self)->SetPositionRotationAndVelocity(to_jph(inBodyID), to_jph(inPosition), to_jph(inRotation), to_jph(inLinearVelocity), to_jph(inAngularVelocity));
}
Expand All @@ -1096,7 +1143,7 @@ JPC_API void JPC_BodyInterface_AddTorque(JPC_BodyInterface *self, JPC_BodyID inB
JPC_API void JPC_BodyInterface_AddForceAndTorque(JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_Vec3 inForce, JPC_Vec3 inTorque) {
return to_jph(self)->AddForceAndTorque(to_jph(inBodyID), to_jph(inForce), to_jph(inTorque));
}

JPC_API void JPC_BodyInterface_AddImpulse(JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_Vec3 inImpulse) {
return to_jph(self)->AddImpulse(to_jph(inBodyID), to_jph(inImpulse));
}
Expand Down

0 comments on commit 6647048

Please sign in to comment.