From 8b789fc9b6e20e74093ad6984ebf154f58a1cacc Mon Sep 17 00:00:00 2001 From: maelstrom Date: Mon, 14 Jul 2025 00:53:02 +0200 Subject: [PATCH] feat(part): made wedge part mesh (not working) --- core/src/objects/part/wedgepart.cpp | 71 +++++++++++++++++++++++++- core/src/objects/part/wedgepart.h | 2 + core/src/objects/service/workspace.cpp | 4 ++ 3 files changed, 75 insertions(+), 2 deletions(-) diff --git a/core/src/objects/part/wedgepart.cpp b/core/src/objects/part/wedgepart.cpp index fdaee8c..a5ff599 100644 --- a/core/src/objects/part/wedgepart.cpp +++ b/core/src/objects/part/wedgepart.cpp @@ -1,5 +1,9 @@ #include "wedgepart.h" #include "physics/util.h" +#include +#include + +rp::ConvexMesh* wedgePhysMesh; WedgePart::WedgePart(): BasePart(&TYPE) { } @@ -9,11 +13,11 @@ WedgePart::WedgePart(PartConstructParams params): BasePart(&TYPE, params) { } void WedgePart::updateCollider(rp::PhysicsCommon* common) { - rp::BoxShape* shape = common->createBoxShape(glmToRp(size * glm::vec3(0.5f))); + rp::ConvexMeshShape* shape = common->createConvexMeshShape(wedgePhysMesh, glmToRp(size * glm::vec3(0.5f))); // Recreate the rigidbody if the shape changes if (rigidBody->getNbColliders() > 0 - && dynamic_cast(rigidBody->getCollider(0)->getCollisionShape())->getHalfExtents() != shape->getHalfExtents()) { + && dynamic_cast(rigidBody->getCollider(0)->getCollisionShape())->getScale() != shape->getScale()) { // TODO: This causes Touched to get called twice. Fix this. rigidBody->removeCollider(rigidBody->getCollider(0)); rigidBody->addCollider(shape, rp::Transform()); @@ -21,4 +25,67 @@ void WedgePart::updateCollider(rp::PhysicsCommon* common) { if (rigidBody->getNbColliders() == 0) rigidBody->addCollider(shape, rp::Transform()); +} + +void WedgePart::createWedgeShape(rp::PhysicsCommon* common) { + // https://www.reactphysics3d.com/documentation/index.html#creatingbody + float vertices[] = { + // X Y Z + /*0*/ -1, 1, 1, // 0 + /*1*/ -1, -1, 1, // | + /*2*/ -1, -1, -1, // 1---2 + + /*3*/ 1, 1, 1, + /*4*/ 1, -1, 1, + /*5*/ 1, -1, -1, + }; + + // -x +x + // +z 1----------4 + // | bottom | + // -z 2----------5 + + // -x +x + // +y 0----------3 + // | front | + // -y 1----------4 + + // -x +x + // +yz 0----------3 + // | slope | + // -yz 2----------5 + + int indices[] = { + // Base + 1, 2, 5, 4, + + // Back-face + 0, 1, 4, 3, + // 4, 1, 0, 3, + + // Slope + 0, 2, 5, 3, + // 3, 5, 2, 0, + + // Sides + 0, 1, 2, + 3, 4, 5, + }; + + // Description of the six faces of the convex mesh + rp::PolygonVertexArray::PolygonFace* polygonFaces = new rp::PolygonVertexArray::PolygonFace[5]; + polygonFaces[0] = { 4, 0 }; // Bottom + polygonFaces[1] = { 4, 4 }; // Front + polygonFaces[2] = { 4, 8 }; // Slope + polygonFaces[3] = { 3, 12 }; // Side + polygonFaces[4] = { 3, 15 }; // Side + + // Create the polygon vertex array + rp::PolygonVertexArray polygonVertexArray(6, vertices, 3 * sizeof(float), indices, sizeof(int), 5, polygonFaces, + rp::PolygonVertexArray::VertexDataType::VERTEX_FLOAT_TYPE, + rp::PolygonVertexArray::IndexDataType::INDEX_INTEGER_TYPE); + + // Create the convex mesh + std::vector messages; + wedgePhysMesh = common->createConvexMesh(polygonVertexArray, messages); } \ No newline at end of file diff --git a/core/src/objects/part/wedgepart.h b/core/src/objects/part/wedgepart.h index a86824b..f5c8e42 100644 --- a/core/src/objects/part/wedgepart.h +++ b/core/src/objects/part/wedgepart.h @@ -8,7 +8,9 @@ class DEF_INST WedgePart : public BasePart { protected: void updateCollider(rp::PhysicsCommon* common) override; + static void createWedgeShape(rp::PhysicsCommon* common); + friend Workspace; public: WedgePart(); WedgePart(PartConstructParams params); diff --git a/core/src/objects/service/workspace.cpp b/core/src/objects/service/workspace.cpp index bf3171d..5a8584f 100644 --- a/core/src/objects/service/workspace.cpp +++ b/core/src/objects/service/workspace.cpp @@ -5,6 +5,7 @@ #include "logger.h" #include "objects/base/instance.h" #include "objects/part/part.h" +#include "objects/part/wedgepart.h" #include "objects/service/jointsservice.h" #include "objects/joint/jointinstance.h" #include "objects/datamodel.h" @@ -82,6 +83,9 @@ void Workspace::InitService() { physicsWorld->setEventListener(&physicsEventListener); + // Create meshes + WedgePart::createWedgeShape(physicsCommon); + // Sync all parts for (auto it = this->GetDescendantsStart(); it != this->GetDescendantsEnd(); it++) { std::shared_ptr obj = *it;