feat(physics): added wedges and cylinders
This commit is contained in:
parent
f16f3a8b79
commit
cbeaa4d2bb
6 changed files with 65 additions and 112 deletions
|
@ -7,6 +7,7 @@
|
|||
enum class DEF_ENUM PartType {
|
||||
Ball = 0,
|
||||
Block = 1,
|
||||
Cylinder = 2,
|
||||
};
|
||||
|
||||
namespace EnumType {
|
||||
|
|
|
@ -9,5 +9,14 @@ Part::Part(PartConstructParams params): BasePart(&TYPE, params) {
|
|||
}
|
||||
|
||||
Vector3 Part::GetEffectiveSize() {
|
||||
return shape == PartType::Ball ? (Vector3)glm::vec3(glm::min(size.X(), size.Y(), size.Z())) : size;
|
||||
float diameter;
|
||||
switch (shape) {
|
||||
case PartType::Ball:
|
||||
return (Vector3)glm::vec3(glm::min(size.X(), size.Y(), size.Z()));
|
||||
case PartType::Cylinder:
|
||||
diameter = glm::min(size.Y(), size.Z());
|
||||
return Vector3(size.X(), diameter, diameter);
|
||||
default:
|
||||
return size;
|
||||
}
|
||||
}
|
|
@ -1,91 +1,6 @@
|
|||
#include "wedgepart.h"
|
||||
// #include <reactphysics3d/collision/ConvexMesh.h>
|
||||
// #include <reactphysics3d/collision/shapes/ConvexMeshShape.h>
|
||||
|
||||
// rp::ConvexMesh* wedgePhysMesh;
|
||||
|
||||
WedgePart::WedgePart(): BasePart(&TYPE) {
|
||||
}
|
||||
|
||||
WedgePart::WedgePart(PartConstructParams params): BasePart(&TYPE, params) {
|
||||
|
||||
}
|
||||
|
||||
// void WedgePart::updateCollider(rp::PhysicsCommon* common) {
|
||||
// Logger::fatalError("Wedges are currently disabled! Please do not use them or your editor may crash\n");
|
||||
// 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<rp::ConvexMeshShape*>(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());
|
||||
// }
|
||||
|
||||
// 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<rp::Message> messages;
|
||||
// // wedgePhysMesh = common->createConvexMesh(polygonVertexArray, messages);
|
||||
// }
|
||||
WedgePart::WedgePart(PartConstructParams params): BasePart(&TYPE, params) {}
|
|
@ -3,13 +3,9 @@
|
|||
#include "basepart.h"
|
||||
#include "objects/annotation.h"
|
||||
|
||||
class DEF_INST_(hidden) WedgePart : public BasePart {
|
||||
class DEF_INST WedgePart : public BasePart {
|
||||
AUTOGEN_PREAMBLE
|
||||
|
||||
protected:
|
||||
// static void createWedgeShape(rp::PhysicsCommon* common);
|
||||
|
||||
friend Workspace;
|
||||
public:
|
||||
WedgePart();
|
||||
WedgePart(PartConstructParams params);
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include "logger.h"
|
||||
#include "objects/part/basepart.h"
|
||||
#include "objects/part/part.h"
|
||||
#include "objects/part/wedgepart.h"
|
||||
#include "objects/service/workspace.h"
|
||||
#include "physics/convert.h"
|
||||
#include "timeutil.h"
|
||||
|
@ -21,6 +22,10 @@
|
|||
#include <Jolt/Physics/Collision/RayCast.h>
|
||||
#include <Jolt/Physics/Collision/Shape/BoxShape.h>
|
||||
#include <Jolt/Physics/Collision/Shape/SphereShape.h>
|
||||
#include <Jolt/Physics/Collision/Shape/CylinderShape.h>
|
||||
#include <Jolt/Physics/Collision/Shape/ScaledShape.h>
|
||||
#include <Jolt/Physics/Collision/Shape/ConvexHullShape.h>
|
||||
#include <Jolt/Physics/Collision/Shape/RotatedTranslatedShape.h>
|
||||
#include <Jolt/Physics/EActivation.h>
|
||||
#include <Jolt/Physics/PhysicsSettings.h>
|
||||
#include <Jolt/RegisterTypes.h>
|
||||
|
@ -36,12 +41,11 @@
|
|||
static JPH::TempAllocator* allocator;
|
||||
static JPH::JobSystem* jobSystem;
|
||||
|
||||
|
||||
namespace Layers
|
||||
{
|
||||
static constexpr JPH::ObjectLayer DYNAMIC = 0;
|
||||
static constexpr JPH::ObjectLayer ANCHORED = 1;
|
||||
static constexpr JPH::ObjectLayer NUM_LAYERS = 2;
|
||||
// static constexpr JPH::ObjectLayer NUM_LAYERS = 2;
|
||||
};
|
||||
|
||||
namespace BPLayers
|
||||
|
@ -51,6 +55,37 @@ namespace BPLayers
|
|||
static constexpr uint NUM_LAYERS(2);
|
||||
};
|
||||
|
||||
static JPH::Ref<JPH::Shape> wedgeShape;
|
||||
|
||||
void physicsInit() {
|
||||
JPH::RegisterDefaultAllocator();
|
||||
JPH::Factory::sInstance = new JPH::Factory();
|
||||
JPH::RegisterTypes();
|
||||
|
||||
allocator = new JPH::TempAllocatorImpl(10 * 1024 * 1024);
|
||||
jobSystem = new JPH::JobSystemThreadPool(JPH::cMaxPhysicsJobs, JPH::cMaxPhysicsBarriers, std::thread::hardware_concurrency() - 1);
|
||||
|
||||
// Create special shapes
|
||||
JPH::Array<JPH::Vec3> wedgeVerts;
|
||||
wedgeVerts.push_back({-1, -1, -1});
|
||||
wedgeVerts.push_back({ 1, -1, -1});
|
||||
wedgeVerts.push_back({-1, -1, 1});
|
||||
wedgeVerts.push_back({ 1, -1, 1});
|
||||
wedgeVerts.push_back({ 1, 1, 1});
|
||||
wedgeVerts.push_back({-1, 1, 1});
|
||||
// // Invisible bevel to avoid phasing
|
||||
// wedgeVerts.push_back({1, 1, 0.9});
|
||||
// wedgeVerts.push_back({0, 1, 0.9});
|
||||
|
||||
wedgeShape = JPH::ConvexHullShapeSettings(wedgeVerts).Create().Get();
|
||||
}
|
||||
|
||||
void physicsDeinit() {
|
||||
JPH::UnregisterTypes();
|
||||
delete JPH::Factory::sInstance;
|
||||
JPH::Factory::sInstance = nullptr;
|
||||
}
|
||||
|
||||
PhysWorld::PhysWorld() {
|
||||
worldImpl.Init(4096, 0, 4096, 4096, broadPhaseLayerInterface, objectBroadPhasefilter, objectLayerPairFilter);
|
||||
worldImpl.SetGravity(JPH::Vec3(0, -196, 0));
|
||||
|
@ -79,8 +114,11 @@ JPH::Shape* makeShape(std::shared_ptr<BasePart> basePart) {
|
|||
return new JPH::BoxShape(convert<JPH::Vec3>(part->size / 2.f), JPH::cDefaultConvexRadius);
|
||||
case PartType::Ball:
|
||||
return new JPH::SphereShape(glm::min(part->size.X(), part->size.Y(), part->size.Z()) / 2.f);
|
||||
break;
|
||||
case PartType::Cylinder:
|
||||
return new JPH::RotatedTranslatedShape(JPH::Vec3(), JPH::Quat::sEulerAngles(JPH::Vec3(0, 0, JPH::JPH_PI * 0.5)), new JPH::CylinderShape(part->size.X() / 2.f, glm::min(part->size.Z(), part->size.Y()) / 2.f));
|
||||
}
|
||||
} else if (std::shared_ptr<WedgePart> part = std::dynamic_pointer_cast<WedgePart>(basePart)) {
|
||||
return new JPH::ScaledShape(wedgeShape, convert<JPH::Vec3>(part->size / 2.f));
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
@ -123,21 +161,9 @@ void PhysWorld::syncBodyProperties(std::shared_ptr<BasePart> part) {
|
|||
interface.SetMotionType(body->GetID(), motionType, activationMode);
|
||||
interface.SetPositionRotationAndVelocity(body->GetID(), convert<JPH::Vec3>(part->position()), convert<JPH::Quat>((glm::quat)part->cframe.RotMatrix()), convert<JPH::Vec3>(part->velocity), /* Angular velocity is NYI: */ body->GetAngularVelocity());
|
||||
}
|
||||
}
|
||||
|
||||
void physicsInit() {
|
||||
JPH::RegisterDefaultAllocator();
|
||||
JPH::Factory::sInstance = new JPH::Factory();
|
||||
JPH::RegisterTypes();
|
||||
|
||||
allocator = new JPH::TempAllocatorImpl(10 * 1024 * 1024);
|
||||
jobSystem = new JPH::JobSystemThreadPool(JPH::cMaxPhysicsJobs, JPH::cMaxPhysicsBarriers, std::thread::hardware_concurrency() - 1);
|
||||
}
|
||||
|
||||
void physicsDeinit() {
|
||||
JPH::UnregisterTypes();
|
||||
delete JPH::Factory::sInstance;
|
||||
JPH::Factory::sInstance = nullptr;
|
||||
part->rigidBody._lastSize = part->size;
|
||||
if (std::shared_ptr<Part> part2 = std::dynamic_pointer_cast<Part>(part)) part->rigidBody._lastShape = part2->shape;
|
||||
}
|
||||
|
||||
tu_time_t physTime;
|
||||
|
@ -168,15 +194,15 @@ PhysJoint PhysWorld::createJoint(PhysJointInfo& type, std::shared_ptr<BasePart>
|
|||
) { Logger::fatalError("Failed to create joint between two parts due to the call being invalid"); panic(); };
|
||||
|
||||
JPH::TwoBodyConstraint* constraint;
|
||||
if (PhysJointGlueInfo* info = dynamic_cast<PhysJointGlueInfo*>(&type)) {
|
||||
if (PhysJointGlueInfo* _ = dynamic_cast<PhysJointGlueInfo*>(&type)) {
|
||||
JPH::FixedConstraintSettings settings;
|
||||
settings.mAutoDetectPoint = true; // TODO: Replace this with anchor point
|
||||
constraint = settings.Create(*part0->rigidBody.bodyImpl, *part1->rigidBody.bodyImpl);
|
||||
} else if (PhysJointWeldInfo* info = dynamic_cast<PhysJointWeldInfo*>(&type)) {
|
||||
} else if (PhysJointWeldInfo* _ = dynamic_cast<PhysJointWeldInfo*>(&type)) {
|
||||
JPH::FixedConstraintSettings settings;
|
||||
settings.mAutoDetectPoint = true; // TODO: Replace this with anchor point
|
||||
constraint = settings.Create(*part0->rigidBody.bodyImpl, *part1->rigidBody.bodyImpl);
|
||||
} else if (PhysJointSnapInfo* info = dynamic_cast<PhysJointSnapInfo*>(&type)) {
|
||||
} else if (PhysJointSnapInfo* _ = dynamic_cast<PhysJointSnapInfo*>(&type)) {
|
||||
JPH::FixedConstraintSettings settings;
|
||||
settings.mAutoDetectPoint = true; // TODO: Replace this with anchor point
|
||||
constraint = settings.Create(*part0->rigidBody.bodyImpl, *part1->rigidBody.bodyImpl);
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
#include <glm/ext/matrix_clip_space.hpp>
|
||||
#include <glm/ext/matrix_float4x4.hpp>
|
||||
#include <glm/ext/matrix_transform.hpp>
|
||||
#include <glm/ext/scalar_constants.hpp>
|
||||
#include <glm/ext/vector_float3.hpp>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/quaternion.hpp>
|
||||
|
@ -127,6 +128,7 @@ static void renderPart(std::shared_ptr<BasePart> part) {
|
|||
glm::mat4 model = part->cframe;
|
||||
Vector3 size = part->GetEffectiveSize();
|
||||
model = glm::scale(model, (glm::vec3)size);
|
||||
if (std::shared_ptr<Part> part2 = std::dynamic_pointer_cast<Part>(part)) if (part2->shape == PartType::Cylinder) model = glm::rotate(model, glm::pi<float>() * 0.5f, glm::vec3(0, 1, 0)); // Lazy hack
|
||||
shader->set("model", model);
|
||||
shader->set("material", Material {
|
||||
.diffuse = part->color,
|
||||
|
@ -157,6 +159,10 @@ static void renderPart(std::shared_ptr<BasePart> part) {
|
|||
glFrontFace(GL_CW);
|
||||
CUBE_MESH->bind();
|
||||
glDrawArrays(GL_TRIANGLES, 0, CUBE_MESH->vertexCount);
|
||||
} else if (shape == PartType::Cylinder) {
|
||||
glFrontFace(GL_CW);
|
||||
CYLINDER_MESH->bind();
|
||||
glDrawArrays(GL_TRIANGLES, 0, CYLINDER_MESH->vertexCount);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue