summaryrefslogtreecommitdiff
path: root/src/game.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/game.cpp')
-rw-r--r--src/game.cpp47
1 files changed, 37 insertions, 10 deletions
diff --git a/src/game.cpp b/src/game.cpp
index f0fac6c..6cde580 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -1,6 +1,7 @@
#include "game.hpp"
+#include "BulletCollision/CollisionShapes/btConvexHullShape.h"
#include "camera.hpp"
#include "components.hpp"
#include "dynamic-array.hpp"
@@ -9,6 +10,7 @@
#include "helpers.hpp"
#include "imgui.h"
#include "math-helpers.hpp"
+#include "physics.hpp"
#include "player-input.hpp"
#include "vendor/glm_include.hpp"
#include "window.hpp"
@@ -138,8 +140,8 @@ void SerializeInstance(std::ofstream &stream, const CompInstance &comp) {
stream << PKE_FILE_INSTANCE_ENTITY_TYPE_CODE << et->entityTypeCode << std::endl;
btTransform trans;
- comp.bt.defaultMotionState.getWorldTransform(trans);
- btVector3 scale = comp.bt.collisionShape->getLocalScaling();
+ comp.bt.motionState->getWorldTransform(trans);
+ btVector3 scale = comp.bt.rigidBody->getCollisionShape()->getLocalScaling();
if (trans != baseInst.posRot) {
btVector3 pos = trans.getOrigin();
btQuaternion rot = trans.getRotation();
@@ -262,7 +264,19 @@ void ParseInstance(std::ifstream &stream) {
}
const auto &et = GlobalEntityTypes[existingEntityTypeIndex];
auto entityHandle = ECS_CreateEntity();
- ECS_CreateInstance(entityHandle, et.entityHandle, instPos);
+ auto &compInst = ECS_CreateInstance(entityHandle, et.entityHandle);
+
+ btVector3 localInertia(0, 0, 0);
+ et.bt.shape->calculateLocalInertia(btScalar(1.f), 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);
+ compInst.bt.rigidBody->setLinearVelocity(btVector3(0,0,0));
+ compInst.bt.rigidBody->setAngularVelocity(btVector3(0,0,0));
+ compInst.bt.rigidBody->getCollisionShape()->setLocalScaling(instPos.scale);
+ BtDynamicsWorld->addRigidBody(compInst.bt.rigidBody);
+ compInst.bt.rigidBody->setUserPointer(reinterpret_cast<void *>(compInst.entHandle));
break;
}
if (strstr(readLine, PKE_FILE_INSTANCE_ENTITY_HANDLE)) {
@@ -438,12 +452,25 @@ void Game_Tick(double delta) {
}
while (entityInstancesToCreate.Count() > 0) {
auto createInfo = entityInstancesToCreate.Pop();
+ // TODO needs to be more elegant
+ int64_t etIndex = EntityType_FindByEntityHandle(createInfo.entityTypeEntityHandle);
+ auto &et = GlobalEntityTypes[etIndex];
EntityHandle newEntity = ECS_CreateEntity();
- InstPos instPos;
- instPos.posRot = btTransform{};
- instPos.posRot.setIdentity();
- instPos.scale = btVector3(1, 1, 1);
- ECS_CreateInstance(newEntity, createInfo.entityTypeEntityHandle, instPos);
+ auto &compInst = ECS_CreateInstance(newEntity, createInfo.entityTypeEntityHandle);
+
+ btVector3 localInertia(0, 0, 0);
+ et.bt.shape->calculateLocalInertia(btScalar(1.f), 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);
+ 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));
+ BtDynamicsWorld->addRigidBody(compInst.bt.rigidBody);
+ compInst.bt.rigidBody->setUserPointer(reinterpret_cast<void *>(compInst.entHandle));
}
PkeInput_Tick(delta);
@@ -834,8 +861,8 @@ void RecordImGui_CompInstPos(bool readonly, CompInstance *component) {
bool changed = false;
InstPos instPos;
- component->bt.defaultMotionState.getWorldTransform(instPos.posRot);
- instPos.scale = component->bt.collisionShape->getLocalScaling();
+ component->bt.motionState->getWorldTransform(instPos.posRot);
+ instPos.scale = component->bt.rigidBody->getCollisionShape()->getLocalScaling();
btVector3 pos = instPos.posRot.getOrigin();
btQuaternion rot = instPos.posRot.getRotation();