summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJonathan Bradley <jcb@pikum.xyz>2023-10-31 14:02:33 -0400
committerJonathan Bradley <jcb@pikum.xyz>2023-11-15 13:13:27 -0500
commit75e6b2bd37951e5dea169352e97ad3b4ca813209 (patch)
treeb09089a45c475e886f01a1bcf112e0650d4fcd16 /src
parentf2c808b1235b9d76e4d4753c025f404e7736ca3c (diff)
default and instance-specific mass
Diffstat (limited to 'src')
-rw-r--r--src/components.hpp1
-rw-r--r--src/ecs.cpp3
-rw-r--r--src/entities.hpp1
-rw-r--r--src/game.cpp33
4 files changed, 34 insertions, 4 deletions
diff --git a/src/components.hpp b/src/components.hpp
index a8b4e88..cd79e4b 100644
--- a/src/components.hpp
+++ b/src/components.hpp
@@ -55,6 +55,7 @@ struct CompGrBinds {
struct InstPos {
btTransform posRot;
btVector3 scale;
+ btScalar mass;
};
struct InstBt {
btDefaultMotionState *motionState;
diff --git a/src/ecs.cpp b/src/ecs.cpp
index 654bc71..ee9201e 100644
--- a/src/ecs.cpp
+++ b/src/ecs.cpp
@@ -412,6 +412,9 @@ void ECS_UpdateInstance(EntityHandle entHandle, const InstPos &instPos, bool ove
auto *inst = &Comp_Instance_BucketContainer.buckets[b].instances[i];
if (BtDynamicsWorld && overridePhysics) {
+ btVector3 localInertia(0, 0, 0);
+ inst->bt.rigidBody->getCollisionShape()->calculateLocalInertia(instPos.mass, localInertia);
+ inst->bt.rigidBody->setMassProps(instPos.mass, localInertia);
inst->bt.rigidBody->setWorldTransform(instPos.posRot);
inst->bt.rigidBody->getCollisionShape()->setLocalScaling(instPos.scale);
inst->bt.rigidBody->setLinearVelocity(btVector3(0,0,0));
diff --git a/src/entities.hpp b/src/entities.hpp
index f45f7d3..e16097c 100644
--- a/src/entities.hpp
+++ b/src/entities.hpp
@@ -33,6 +33,7 @@ struct EntityType {
} Importer_GLTF;
struct {
btCollisionShape *shape = nullptr;
+ btScalar startingMass = 1.f;
} bt;
};
extern DynArray<EntityType> GlobalEntityTypes;
diff --git a/src/game.cpp b/src/game.cpp
index 6cde580..3ff3948 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -19,6 +19,7 @@
#include <cstring>
#include <glm/gtc/quaternion.hpp>
#include <iomanip>
+#include <ostream>
const uint64_t consoleBufferCount = 30;
const uint64_t consoleLineLength = 128;
@@ -77,12 +78,14 @@ const char *PKE_FILE_ENTITY_TYPE_IMPORTER_GLTF_ACCESSOR_INDEX_VERTEX = "Importer
const char *PKE_FILE_ENTITY_TYPE_IMPORTER_GLTF_ACCESSOR_INDEX_NORMAL = "Importer_GLTF::AccessorIndexNormal: ";
const char *PKE_FILE_ENTITY_TYPE_IMPORTER_GLTF_ACCESSOR_INDEX_UV = "Importer_GLTF::AccessorIndexUV: ";
const char *PKE_FILE_ENTITY_TYPE_IMPORTER_GLTF_ACCESSOR_INDEX_INDEX = "Importer_GLTF::AccessorIndexIndex: ";
+const char *PKE_FILE_ENTITY_TYPE_PHYSICS_STARTING_MASS = "BT::StartingMass: ";
const char *PKE_FILE_INSTANCE_ENTITY_HANDLE = "EntityHandle: ";
const char *PKE_FILE_INSTANCE_ENTITY_TYPE_CODE = "EntityTypeCode: ";
const char *PKE_FILE_INSTANCE_POS_POS = "InstPos::Pos: ";
const char *PKE_FILE_INSTANCE_POS_ROT = "InstPos::Rot: ";
const char *PKE_FILE_INSTANCE_POS_SCALE = "InstPos::Scale: ";
+const char *PKE_FILE_INSTANCE_MASS = "InstPos::Mass: ";
char consoleBuffer[consoleBufferCount][consoleLineLength];
long consoleBufferIndex = 0;
@@ -117,6 +120,9 @@ void SerializeEntityType(std::ofstream &stream, const EntityType &et) {
stream << PKE_FILE_ENTITY_TYPE_IMPORTER_GLTF_ACCESSOR_INDEX_UV << et.Importer_GLTF.AccessorIndexUV << std::endl;
if (et.Importer_GLTF.AccessorIndexIndex != e.Importer_GLTF.AccessorIndexIndex)
stream << PKE_FILE_ENTITY_TYPE_IMPORTER_GLTF_ACCESSOR_INDEX_INDEX << et.Importer_GLTF.AccessorIndexIndex << std::endl;
+ if (et.bt.startingMass != e.bt.startingMass) {
+ stream << PKE_FILE_ENTITY_TYPE_PHYSICS_STARTING_MASS << et.bt.startingMass << std::endl;
+ }
}
void SerializeInstance(std::ofstream &stream, const CompInstance &comp) {
@@ -142,6 +148,7 @@ void SerializeInstance(std::ofstream &stream, const CompInstance &comp) {
btTransform trans;
comp.bt.motionState->getWorldTransform(trans);
btVector3 scale = comp.bt.rigidBody->getCollisionShape()->getLocalScaling();
+ btScalar mass = comp.bt.rigidBody->getMass();
if (trans != baseInst.posRot) {
btVector3 pos = trans.getOrigin();
btQuaternion rot = trans.getRotation();
@@ -160,6 +167,9 @@ void SerializeInstance(std::ofstream &stream, const CompInstance &comp) {
<< std::setw(10) << scale[0] << ","
<< std::setw(10) << scale[1] << ","
<< std::setw(10) << scale[2] << "]" << std::endl;
+ if (mass != baseInst.mass) {
+ stream << PKE_FILE_INSTANCE_MASS << mass << std::endl;
+ }
}
void ParseEntityType(std::ifstream &stream) {
@@ -240,6 +250,12 @@ void ParseEntityType(std::ifstream &stream) {
assert(result == STR2NUM_ERROR::SUCCESS);
continue;
}
+ if (strstr(readLine, PKE_FILE_ENTITY_TYPE_PHYSICS_STARTING_MASS)) {
+ uint64_t prefixLen = strlen(PKE_FILE_ENTITY_TYPE_PHYSICS_STARTING_MASS);
+ STR2NUM_ERROR result = str2num(et.bt.startingMass, readLine + prefixLen);
+ assert(result == STR2NUM_ERROR::SUCCESS);
+ continue;
+ }
}
}
@@ -249,6 +265,7 @@ void ParseInstance(std::ifstream &stream) {
instPos.posRot = btTransform{};
instPos.posRot.setIdentity();
instPos.scale = btVector3(1, 1, 1);
+ instPos.mass = 1.f;
char entTypeCode[21];
memset(reinterpret_cast<void *>(entTypeCode), '\0', 21);
while (stream.getline(readLine, readLineLength)) {
@@ -267,11 +284,11 @@ void ParseInstance(std::ifstream &stream) {
auto &compInst = ECS_CreateInstance(entityHandle, et.entityHandle);
btVector3 localInertia(0, 0, 0);
- et.bt.shape->calculateLocalInertia(btScalar(1.f), localInertia);
+ et.bt.shape->calculateLocalInertia(instPos.mass, localInertia);
compInst.bt.motionState = Pke_New<btDefaultMotionState>(MemBkt_Bullet);
new (compInst.bt.motionState) btDefaultMotionState(instPos.posRot);
compInst.bt.rigidBody = Pke_New<btRigidBody>(MemBkt_Bullet);
- new (compInst.bt.rigidBody) btRigidBody(btScalar(1.f), compInst.bt.motionState, et.bt.shape, localInertia);
+ new (compInst.bt.rigidBody) btRigidBody(instPos.mass, compInst.bt.motionState, et.bt.shape, localInertia);
compInst.bt.rigidBody->setLinearVelocity(btVector3(0,0,0));
compInst.bt.rigidBody->setAngularVelocity(btVector3(0,0,0));
compInst.bt.rigidBody->getCollisionShape()->setLocalScaling(instPos.scale);
@@ -342,6 +359,12 @@ void ParseInstance(std::ifstream &stream) {
} while (*pEnd != ']');
continue;
}
+ if (strstr(readLine, PKE_FILE_INSTANCE_MASS)) {
+ uint64_t prefixLen = strlen(PKE_FILE_INSTANCE_MASS);
+ STR2NUM_ERROR result = str2num(instPos.mass, readLine + prefixLen);
+ assert(result == STR2NUM_ERROR::SUCCESS);
+ continue;
+ }
}
}
@@ -459,13 +482,13 @@ void Game_Tick(double delta) {
auto &compInst = ECS_CreateInstance(newEntity, createInfo.entityTypeEntityHandle);
btVector3 localInertia(0, 0, 0);
- et.bt.shape->calculateLocalInertia(btScalar(1.f), localInertia);
+ et.bt.shape->calculateLocalInertia(et.bt.startingMass, localInertia);
btTransform posRot{};
posRot.setIdentity();
compInst.bt.motionState = Pke_New<btDefaultMotionState>(MemBkt_Bullet);
new (compInst.bt.motionState) btDefaultMotionState(posRot);
compInst.bt.rigidBody = Pke_New<btRigidBody>(MemBkt_Bullet);
- new (compInst.bt.rigidBody) btRigidBody(btScalar(1.f), compInst.bt.motionState, et.bt.shape, localInertia);
+ new (compInst.bt.rigidBody) btRigidBody(et.bt.startingMass, compInst.bt.motionState, et.bt.shape, localInertia);
compInst.bt.rigidBody->setLinearVelocity(btVector3(0,0,0));
compInst.bt.rigidBody->setAngularVelocity(btVector3(0,0,0));
compInst.bt.rigidBody->getCollisionShape()->setLocalScaling(btVector3(1, 1, 1));
@@ -758,6 +781,7 @@ void RecordImGuiModalCreateEntityType() {
ImGui::InputScalar("GLTF Import Index - Normal", ImGuiDataType_S16, &entityTypeToCreate->Importer_GLTF.AccessorIndexNormal);
ImGui::InputScalar("GLTF Import Index - UV", ImGuiDataType_S16, &entityTypeToCreate->Importer_GLTF.AccessorIndexUV);
ImGui::InputScalar("GLTF Import Index - Index", ImGuiDataType_S16, &entityTypeToCreate->Importer_GLTF.AccessorIndexIndex);
+ ImGui::InputFloat("Physics - Mass", &entityTypeToCreate->bt.startingMass);
ImGui::Separator();
@@ -871,6 +895,7 @@ void RecordImGui_CompInstPos(bool readonly, CompInstance *component) {
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;
+ changed = ImGui::InputFloat("mass", &instPos.mass, 0.0, 0.0, "%.3f", inputTextFlags) || changed;
if (changed) {
instPos.posRot.setOrigin(pos);