diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2023-10-24 13:43:58 -0400 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2023-11-15 13:11:11 -0500 |
| commit | 18e65823663af6e2a1472b66486526a23d5e9c30 (patch) | |
| tree | bcd8ce9cec02ce56d644549a33951c20e7228423 /src | |
| parent | bd924c70c7c6e98b53c784f3b06f6b315741b8d0 (diff) | |
don't convert bullet to glm unless needed
Diffstat (limited to 'src')
| -rw-r--r-- | src/components.hpp | 6 | ||||
| -rw-r--r-- | src/ecs.cpp | 32 | ||||
| -rw-r--r-- | src/game.cpp | 77 |
3 files changed, 47 insertions, 68 deletions
diff --git a/src/components.hpp b/src/components.hpp index f10dd63..0903492 100644 --- a/src/components.hpp +++ b/src/components.hpp @@ -7,7 +7,6 @@ #include <BulletCollision/CollisionShapes/btCollisionShape.h> #include <LinearMath/btDefaultMotionState.h> #include <BulletDynamics/Dynamics/btRigidBody.h> -#include <glm/gtc/quaternion.hpp> #include <vulkan/vulkan_core.h> const uint64_t ECS_UNSET_VAL = 0xFFFFFFFFFFFFFFFF; @@ -55,9 +54,8 @@ struct CompGrBinds { }; struct InstPos { - glm::vec3 pos; - glm::quat rot; - glm::vec3 scale; + btTransform posRot; + btVector3 scale; }; struct CompInstance { diff --git a/src/ecs.cpp b/src/ecs.cpp index 9de5c44..b5b0ce4 100644 --- a/src/ecs.cpp +++ b/src/ecs.cpp @@ -403,20 +403,10 @@ CompInstance &ECS_CreateInstance(EntityHandle entHandle, EntityHandle entityType if (btDynamicsWorld) { comp->bt.localInertia = btVector3(0, 0, 0); - btVector3 scale; - GlmToBullet(instPos.scale, scale); comp->bt.collisionShape = Pke_New<btBoxShape>(bulletBucket); - new (comp->bt.collisionShape) btBoxShape(scale); + new (comp->bt.collisionShape) btBoxShape(instPos.scale); comp->bt.collisionShape->calculateLocalInertia(btScalar(1.f), comp->bt.localInertia); - btTransform transform; - transform.setIdentity(); - btVector3 origin; - GlmToBullet(instPos.pos, origin); - transform.setOrigin(origin); - btQuaternion rot; - GlmToBullet(instPos.rot, rot); - transform.setRotation(rot); - comp->bt.defaultMotionState = btDefaultMotionState(transform); + comp->bt.defaultMotionState = btDefaultMotionState(instPos.posRot); comp->bt.rigidBody = Pke_New<btRigidBody>(bulletBucket); new (comp->bt.rigidBody) btRigidBody(btScalar(1.f), &comp->bt.defaultMotionState, comp->bt.collisionShape, comp->bt.localInertia); comp->bt.rigidBody->setLinearVelocity(btVector3(0,0,0)); @@ -456,22 +446,8 @@ void ECS_UpdateInstance(EntityHandle entHandle, const InstPos &instPos, bool ove auto *inst = &Comp_Instance_BucketContainer.buckets[b].instances[i]; if (btDynamicsWorld && overridePhysics) { - btTransform transform; - transform.setIdentity(); - - btVector3 origin; - GlmToBullet(instPos.pos, origin); - transform.setOrigin(origin); - - btQuaternion rot; - GlmToBullet(instPos.rot, rot); - transform.setRotation(rot); - inst->bt.rigidBody->setWorldTransform(transform); - - btVector3 scale; - GlmToBullet(instPos.scale, scale); - inst->bt.collisionShape->setLocalScaling(scale); - + inst->bt.rigidBody->setWorldTransform(instPos.posRot); + inst->bt.collisionShape->setLocalScaling(instPos.scale); inst->bt.rigidBody->setLinearVelocity(btVector3(0,0,0)); inst->bt.rigidBody->setAngularVelocity(btVector3(0,0,0)); btDynamicsWorld->getPairCache()->cleanProxyFromPairs(inst->bt.rigidBody->getBroadphaseProxy(), btDynamicsWorld->getDispatcher()); diff --git a/src/game.cpp b/src/game.cpp index e1accc9..f0fac6c 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -130,35 +130,34 @@ void SerializeInstance(std::ofstream &stream, const CompInstance &comp) { assert(et != nullptr); CompInstance c{}; InstPos baseInst{}; - baseInst.pos = glm::vec3(0, 0, 0); - baseInst.rot = glm::quat(1, 0, 0, 0); - baseInst.scale = glm::vec3(1, 1, 1); + baseInst.posRot = btTransform{}; + baseInst.posRot.setIdentity(); + baseInst.scale = btVector3(1, 1, 1); if (comp.entHandle != c.entHandle) stream << PKE_FILE_INSTANCE_ENTITY_HANDLE << handleStr << std::endl; stream << PKE_FILE_INSTANCE_ENTITY_TYPE_CODE << et->entityTypeCode << std::endl; btTransform trans; comp.bt.defaultMotionState.getWorldTransform(trans); - InstPos inst; - BulletToGlm(trans.getOrigin(), inst.pos); - BulletToGlm(trans.getRotation(), inst.rot); - BulletToGlm(comp.bt.collisionShape->getLocalScaling(), inst.scale); - if (inst.pos != baseInst.pos) + btVector3 scale = comp.bt.collisionShape->getLocalScaling(); + if (trans != baseInst.posRot) { + btVector3 pos = trans.getOrigin(); + btQuaternion rot = trans.getRotation(); stream << PKE_FILE_INSTANCE_POS_POS << "[" - << std::setw(10) << inst.pos[0] << "," - << std::setw(10) << inst.pos[1] << "," - << std::setw(10) << inst.pos[2] << "]" << std::endl; - if (inst.rot != baseInst.rot) - stream << PKE_FILE_INSTANCE_POS_ROT << "[" - << std::setw(10) << inst.rot[0] << "," - << std::setw(10) << inst.rot[1] << "," - << std::setw(10) << inst.rot[2] << "," - << std::setw(10) << inst.rot[3] << "]" << std::endl; - if (inst.scale != baseInst.scale) + << std::setw(10) << pos[0] << "," + << std::setw(10) << pos[1] << "," + << std::setw(10) << pos[2] << "]" << std::endl + << PKE_FILE_INSTANCE_POS_ROT << "[" + << std::setw(10) << rot[0] << "," + << std::setw(10) << rot[1] << "," + << std::setw(10) << rot[2] << "," + << std::setw(10) << rot[3] << "]" << std::endl; + } + if (scale != baseInst.scale) stream << PKE_FILE_INSTANCE_POS_SCALE << "[" - << std::setw(10) << inst.scale[0] << "," - << std::setw(10) << inst.scale[1] << "," - << std::setw(10) << inst.scale[2] << "]" << std::endl; + << std::setw(10) << scale[0] << "," + << std::setw(10) << scale[1] << "," + << std::setw(10) << scale[2] << "]" << std::endl; } void ParseEntityType(std::ifstream &stream) { @@ -245,9 +244,9 @@ void ParseEntityType(std::ifstream &stream) { void ParseInstance(std::ifstream &stream) { CompInstance comp{}; InstPos instPos{}; - instPos.pos = glm::vec3(0, 0, 0); - instPos.rot = glm::quat(1, 0, 0, 0); - instPos.scale = glm::vec3(1, 1, 1); + instPos.posRot = btTransform{}; + instPos.posRot.setIdentity(); + instPos.scale = btVector3(1, 1, 1); char entTypeCode[21]; memset(reinterpret_cast<void *>(entTypeCode), '\0', 21); while (stream.getline(readLine, readLineLength)) { @@ -286,13 +285,15 @@ void ParseInstance(std::ifstream &stream) { assert(startingChar != nullptr); char *pEnd = nullptr; long index = 0; + btVector3 pos; do { assert(index < 3); - STR2NUM_ERROR result = str2num(instPos.pos[index], startingChar, pEnd); + STR2NUM_ERROR result = str2num(pos[index], startingChar, pEnd); assert(result == STR2NUM_ERROR::SUCCESS); startingChar = pEnd + 1; ++index; } while (*pEnd != ']'); + instPos.posRot.setOrigin(pos); continue; } if (strstr(readLine, PKE_FILE_INSTANCE_POS_ROT)) { @@ -301,13 +302,15 @@ void ParseInstance(std::ifstream &stream) { assert(startingChar != nullptr); char *pEnd = nullptr; long index = 0; + btQuaternion rot; do { assert(index < 4); - STR2NUM_ERROR result = str2num(instPos.rot[index], startingChar, pEnd); + STR2NUM_ERROR result = str2num(rot[index], startingChar, pEnd); assert(result == STR2NUM_ERROR::SUCCESS); startingChar = pEnd + 1; ++index; } while (*pEnd != ']'); + instPos.posRot.setRotation(rot); continue; } if (strstr(readLine, PKE_FILE_INSTANCE_POS_SCALE)) { @@ -437,9 +440,9 @@ void Game_Tick(double delta) { auto createInfo = entityInstancesToCreate.Pop(); EntityHandle newEntity = ECS_CreateEntity(); InstPos instPos; - instPos.pos = glm::vec3(0, 0, 0); - instPos.rot = glm::quat(1, 0, 0, 0); - instPos.scale = glm::vec3(1); + instPos.posRot = btTransform{}; + instPos.posRot.setIdentity(); + instPos.scale = btVector3(1, 1, 1); ECS_CreateInstance(newEntity, createInfo.entityTypeEntityHandle, instPos); } @@ -830,19 +833,21 @@ void RecordImGui_CompInstPos(bool readonly, CompInstance *component) { ImGui::Separator(); bool changed = false; - btTransform trans; - component->bt.defaultMotionState.getWorldTransform(trans); InstPos instPos; - BulletToGlm(trans.getOrigin(), instPos.pos); - BulletToGlm(trans.getRotation(), instPos.rot); - BulletToGlm(component->bt.collisionShape->getLocalScaling(), instPos.scale); + component->bt.defaultMotionState.getWorldTransform(instPos.posRot); + instPos.scale = component->bt.collisionShape->getLocalScaling(); + + btVector3 pos = instPos.posRot.getOrigin(); + btQuaternion rot = instPos.posRot.getRotation(); changed = ImGui::InputScalar("Instance Index", ImGuiDataType_U64, &component->index, nullptr, nullptr, nullptr, ImGuiInputTextFlags_ReadOnly) || changed; - changed = ImGui::InputScalarN("pos", ImGuiDataType_Float, &instPos.pos, 3, nullptr, nullptr, nullptr, inputTextFlags) || changed; - changed = ImGui::InputScalarN("rot", ImGuiDataType_Float, &instPos.rot, 4, nullptr, nullptr, nullptr, inputTextFlags) || changed; + changed = ImGui::InputScalarN("pos", ImGuiDataType_Float, &pos, 3, nullptr, nullptr, nullptr, inputTextFlags) || changed; + changed = ImGui::InputScalarN("rot", ImGuiDataType_Float, &rot, 4, nullptr, nullptr, nullptr, inputTextFlags) || changed; changed = ImGui::InputScalarN("scale", ImGuiDataType_Float, &instPos.scale, 3, nullptr, nullptr, nullptr, inputTextFlags) || changed; if (changed) { + instPos.posRot.setOrigin(pos); + instPos.posRot.setRotation(rot); ECS_UpdateInstance(component->entHandle, instPos, true); } |
