From c97cf995efbcee49cdda4f4b4cb4921f63670f3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Mon, 21 Mar 2022 23:51:40 +0100 Subject: [PATCH] ksys/phys: Add more StaticCompound stuff, part 1 --- data/uking_functions.csv | 14 +-- src/KingSystem/Physics/CMakeLists.txt | 2 + .../Physics/RigidBody/physRigidBody.h | 2 +- .../physStaticCompoundBodyGroup.cpp | 97 +++++++++++++++++++ .../physStaticCompoundBodyGroup.h | 31 ++++-- .../StaticCompound/physStaticCompoundMgr.cpp | 22 +++++ .../StaticCompound/physStaticCompoundMgr.h | 30 ++++++ src/KingSystem/Physics/System/physSystem.h | 11 ++- 8 files changed, 191 insertions(+), 18 deletions(-) create mode 100644 src/KingSystem/Physics/StaticCompound/physStaticCompoundMgr.cpp create mode 100644 src/KingSystem/Physics/StaticCompound/physStaticCompoundMgr.h diff --git a/data/uking_functions.csv b/data/uking_functions.csv index 7e241d30..9e60e0eb 100644 --- a/data/uking_functions.csv +++ b/data/uking_functions.csv @@ -84236,10 +84236,10 @@ Address,Quality,Size,Name 0x0000007100fcb1ec,O,000048,_ZNK4ksys4phys18StaticCompoundInfo12getShapeInfoEi 0x0000007100fcb21c,O,000120,_ZN4ksys4phys16getBodyLayerTypeENS0_12ContactLayerE 0x0000007100fcb294,O,000396,_GLOBAL__sub_I_physStaticCompoundAutogen.cpp -0x0000007100fcb420,U,000080, -0x0000007100fcb470,U,000076, -0x0000007100fcb4bc,U,000084, -0x0000007100fcb510,U,000068, +0x0000007100fcb420,O,000080,_ZN4ksys4phys17StaticCompoundMgrC1Ev +0x0000007100fcb470,O,000076,_ZN4ksys4phys17StaticCompoundMgrD1Ev +0x0000007100fcb4bc,O,000084,_ZN4ksys4phys17StaticCompoundMgrD0Ev +0x0000007100fcb510,O,000068,_ZN4ksys4phys17StaticCompoundMgr4initEv 0x0000007100fcb554,U,000140, 0x0000007100fcb5e0,U,000120, 0x0000007100fcb658,U,000124, @@ -84255,8 +84255,8 @@ Address,Quality,Size,Name 0x0000007100fcbc94,U,000324, 0x0000007100fcbdd8,U,000104, 0x0000007100fcbe40,O,000168,_ZN4ksys4phys9BodyGroupC1Ev -0x0000007100fcbee8,U,000288,BodyGroup::dtor -0x0000007100fcc008,U,001492,FieldBodyGroup::init +0x0000007100fcbee8,O,000288,_ZN4ksys4phys9BodyGroupD1Ev +0x0000007100fcc008,O,001492,_ZN4ksys4phys9BodyGroup4initERK16hkpPhysicsSystemPN4sead8Matrix34IfEEPNS0_14StaticCompoundEPNS5_4HeapE 0x0000007100fcc5dc,U,000120,BodyGroup::x 0x0000007100fcc654,U,000120,BodyGroup::x_0 0x0000007100fcc6cc,U,000248,BodyGroup::x_1 @@ -84269,7 +84269,7 @@ Address,Quality,Size,Name 0x0000007100fccc7c,U,000312,BodyGroup::x_8 0x0000007100fccdb4,U,000944,BodyGroup::x_9 0x0000007100fcd164,U,000244,BodyGroup::x_10 -0x0000007100fcd258,U,000288,BodyGroup::x_11 +0x0000007100fcd258,O,000288,_ZN4ksys4phys9BodyGroup12modifyMatrixERKN4sead8Matrix34IfEEi 0x0000007100fcd378,U,000028,BodyGroup::x_12 0x0000007100fcd394,U,000144,BodyGroup::x_16 0x0000007100fcd424,U,000164,BodyGroup::x_13 diff --git a/src/KingSystem/Physics/CMakeLists.txt b/src/KingSystem/Physics/CMakeLists.txt index c5ca57de..a9a37fb2 100644 --- a/src/KingSystem/Physics/CMakeLists.txt +++ b/src/KingSystem/Physics/CMakeLists.txt @@ -96,6 +96,8 @@ target_sources(uking PRIVATE StaticCompound/physStaticCompoundBodyGroup.h StaticCompound/physStaticCompoundInfo.cpp StaticCompound/physStaticCompoundInfo.h + StaticCompound/physStaticCompoundMgr.cpp + StaticCompound/physStaticCompoundMgr.h StaticCompound/physStaticCompoundUtil.cpp StaticCompound/physStaticCompoundUtil.h diff --git a/src/KingSystem/Physics/RigidBody/physRigidBody.h b/src/KingSystem/Physics/RigidBody/physRigidBody.h index 304c3129..88a56ec2 100644 --- a/src/KingSystem/Physics/RigidBody/physRigidBody.h +++ b/src/KingSystem/Physics/RigidBody/physRigidBody.h @@ -59,7 +59,7 @@ public: _1 = 1, _2 = 2, TerrainHeightField = 3, - _4 = 4, + StaticCompoundBody = 4, CharacterController = 5, TeraMesh = 6, }; diff --git a/src/KingSystem/Physics/StaticCompound/physStaticCompoundBodyGroup.cpp b/src/KingSystem/Physics/StaticCompound/physStaticCompoundBodyGroup.cpp index dc0e4ef1..3a95859d 100644 --- a/src/KingSystem/Physics/StaticCompound/physStaticCompoundBodyGroup.cpp +++ b/src/KingSystem/Physics/StaticCompound/physStaticCompoundBodyGroup.cpp @@ -1,7 +1,104 @@ #include "KingSystem/Physics/StaticCompound/physStaticCompoundBodyGroup.h" +#include +#include +#include "KingSystem/Physics/RigidBody/physRigidBodyFromResource.h" +#include "KingSystem/Physics/RigidBody/physRigidBodyParam.h" +#include "KingSystem/Physics/StaticCompound/physStaticCompoundInfo.h" +#include "KingSystem/Physics/StaticCompound/physStaticCompoundMgr.h" +#include "KingSystem/Physics/System/physSystem.h" namespace ksys::phys { +// TODO: rename +constexpr int BodyGroupNumMatrices = 8; + BodyGroup::BodyGroup() = default; +BodyGroup::~BodyGroup() { + mFlags.reset(Flag::Initialised); + + for (int i = 0, n = mRigidBodiesPerBodyLayerType.size(); i < n; ++i) { + delete mRigidBodiesPerBodyLayerType[i]; + } + + mRigidBodiesPerBodyLayerType.freeBuffer(); + mShapesPerBodyLayerType.freeBuffer(); + mMatrices2.freeBuffer(); + mMatrices.freeBuffer(); + _e8.freeBuffer(); + mRigidBodies.freeBuffer(); +} + +void BodyGroup::init(const hkpPhysicsSystem& system, sead::Matrix34f* mtx, StaticCompound* sc, + sead::Heap* heap) { + mStaticCompound = sc; + mMtxPtr = mtx; + + const int num_rigid_bodies = system.getRigidBodies().getSize(); + + mRigidBodies.allocBuffer(num_rigid_bodies + NumBodyLayerTypes, heap); + + if (num_rigid_bodies > 0) { + auto* group_handler = System::instance()->getStaticCompoundMgr()->getGroupHandler(); + + mRigidBodiesPerBodyLayerType.allocBufferAssert(NumBodyLayerTypes, heap); + mShapesPerBodyLayerType.allocBufferAssert(NumBodyLayerTypes, heap); + for (int i = 0; i < NumBodyLayerTypes; ++i) { + mRigidBodiesPerBodyLayerType[i] = nullptr; + mShapesPerBodyLayerType[i] = nullptr; + } + + for (int i = 0; i < num_rigid_bodies; ++i) { + hkpRigidBody* hk_body = system.getRigidBodies()[i]; + + bool is_entity = sead::SafeString(hk_body->getName()).include("Entity"); + auto layer_type = is_entity ? ContactLayerType::Entity : ContactLayerType::Sensor; + + auto* body = new (heap) RigidBodyFromResource(0.0, hk_body, layer_type, heap, + RigidBody::Type::StaticCompoundBody); + + RigidBodyInstanceParam param; + param.max_linear_velocity = 1000; + body->initMotionAccessor(param, heap, /* init_motion */ false); + + BodyLayerType body_layer_type = getBodyLayerType(body->getContactLayer()); + mRigidBodiesPerBodyLayerType[int(body_layer_type)] = body; + + body->updateCollidableQualityType(true); + body->changeMotionType(MotionType::Fixed); + if (body->isEntity()) { + body->setSystemGroupHandler(group_handler); + body->enableGroundCollision(false); + body->enableWaterCollision(false); + } + + mRigidBodies.pushBack(body); + + // XXX: eww, const_cast + auto* shape = + const_cast(system.getRigidBodies()[i]->getCollidable()->getShape()); + shape->m_userData = reinterpret_cast(this); + mShapesPerBodyLayerType[int(body_layer_type)] = shape; + } + } + + mMatrices2.allocBufferAssert(BodyGroupNumMatrices, heap); + mMatrices.allocBufferAssert(BodyGroupNumMatrices, heap); + for (int i = 0, n = mMatrices2.size(); i < n; ++i) { + mMatrices2[i].makeIdentity(); + mMatrices[i].makeIdentity(); + } + + mFlags.set(Flag::Initialised); +} + +void BodyGroup::modifyMatrix(const sead::Matrix34f& matrix, int index) { + if (mMatrices[index] == matrix) + return; + + mMatrices[index] = matrix; + mModifiedMatrices |= 1 << index; + mFlags.set(Flag::HasModifiedMatrix); +} + } // namespace ksys::phys diff --git a/src/KingSystem/Physics/StaticCompound/physStaticCompoundBodyGroup.h b/src/KingSystem/Physics/StaticCompound/physStaticCompoundBodyGroup.h index a5f1764a..f6be6605 100644 --- a/src/KingSystem/Physics/StaticCompound/physStaticCompoundBodyGroup.h +++ b/src/KingSystem/Physics/StaticCompound/physStaticCompoundBodyGroup.h @@ -5,10 +5,12 @@ #include #include #include +#include #include #include "KingSystem/Utils/Types.h" class hkpPhysicsSystem; +class hkpShape; namespace ksys::phys { @@ -29,12 +31,27 @@ public: void disableCollision(BodyLayerType body_layer_type, int instance_id, bool x); - sead::Atomic _0; - sead::Atomic _4; - sead::Buffer _8; - sead::Buffer _18; - sead::Buffer _28; - sead::Buffer _38; + void modifyMatrix(const sead::Matrix34f& matrix, int index); + +private: + enum class Flag { + Initialised = 1 << 0, + HasModifiedMatrix = 1 << 3, + }; + + struct Unk1 { + virtual ~Unk1(); + + u8 _8[0xc0]; + }; + + sead::TypedBitFlag> mFlags; + sead::Atomic mModifiedMatrices; + sead::Buffer mRigidBodiesPerBodyLayerType; + sead::Buffer mShapesPerBodyLayerType; + // TODO: rename + sead::Buffer mMatrices; + sead::Buffer mMatrices2; sead::Matrix34f mMtx0 = sead::Matrix34f::ident; sead::Matrix34f mMtx1 = sead::Matrix34f::ident; sead::Matrix34f* mMtxPtr{}; @@ -43,7 +60,7 @@ public: sead::Vector3f _c8 = sead::Vector3f::zero; sead::Vector3f _d4 = sead::Vector3f::zero; u32 _e0{}; - sead::Buffer _e8; + sead::Buffer _e8; }; KSYS_CHECK_SIZE_NX150(BodyGroup, 0xf8); diff --git a/src/KingSystem/Physics/StaticCompound/physStaticCompoundMgr.cpp b/src/KingSystem/Physics/StaticCompound/physStaticCompoundMgr.cpp new file mode 100644 index 00000000..f5d79b25 --- /dev/null +++ b/src/KingSystem/Physics/StaticCompound/physStaticCompoundMgr.cpp @@ -0,0 +1,22 @@ +#include "KingSystem/Physics/StaticCompound/physStaticCompoundMgr.h" +#include "KingSystem/Physics/System/physSystem.h" + +namespace ksys::phys { + +StaticCompoundMgr::StaticCompoundMgr() = default; + +StaticCompoundMgr::~StaticCompoundMgr() { + if (mGroupHandler) { + System::instance()->removeSystemGroupHandler(mGroupHandler); + mGroupHandler = nullptr; + } +} + +bool StaticCompoundMgr::init() { + if (!mGroupHandler) + mGroupHandler = System::instance()->addSystemGroupHandler(ContactLayerType::Entity); + + return true; +} + +} // namespace ksys::phys diff --git a/src/KingSystem/Physics/StaticCompound/physStaticCompoundMgr.h b/src/KingSystem/Physics/StaticCompound/physStaticCompoundMgr.h new file mode 100644 index 00000000..7cab39f5 --- /dev/null +++ b/src/KingSystem/Physics/StaticCompound/physStaticCompoundMgr.h @@ -0,0 +1,30 @@ +#pragma once + +#include +#include +#include + +namespace ksys::phys { + +class StaticCompound; +class SystemGroupHandler; + +/// Provides convenient access to all loaded StaticCompounds. +class StaticCompoundMgr : public sead::hostio::Node { +public: + StaticCompoundMgr(); + virtual ~StaticCompoundMgr(); + + bool init(); + + SystemGroupHandler* getGroupHandler() const { return mGroupHandler; } + + // TODO: more functions + +private: + sead::FixedPtrArray mStaticCompounds; + sead::CriticalSection mCS; + SystemGroupHandler* mGroupHandler{}; +}; + +} // namespace ksys::phys diff --git a/src/KingSystem/Physics/System/physSystem.h b/src/KingSystem/Physics/System/physSystem.h index b9439b99..4e01e60b 100644 --- a/src/KingSystem/Physics/System/physSystem.h +++ b/src/KingSystem/Physics/System/physSystem.h @@ -15,13 +15,14 @@ class CollisionInfo; class ContactLayerCollisionInfo; class ContactLayerCollisionInfoGroup; class ContactMgr; +class ContactPointInfo; class GroupFilter; +class LayerContactPointInfo; class MaterialTable; class RayCastForRequest; class RigidBody; class RigidBodyRequestMgr; -class ContactPointInfo; -class LayerContactPointInfo; +class StaticCompoundMgr; class SystemData; class SystemGroupHandler; @@ -42,6 +43,7 @@ public: float get64() const { return _64; } float getTimeFactor() const { return mTimeFactor; } ContactMgr* getContactMgr() const { return mContactMgr; } + StaticCompoundMgr* getStaticCompoundMgr() const { return mStaticCompoundMgr; } RigidBodyRequestMgr* getRigidBodyRequestMgr() const { return mRigidBodyRequestMgr; } SystemData* getSystemData() const { return mSystemData; } MaterialTable* getMaterialTable() const { return mMaterialTable; } @@ -83,6 +85,9 @@ public: // 0x0000007101216a20 void removeRigidBodyFromContactSystem(RigidBody* body); + // 0x000000710121686c + SystemGroupHandler* addSystemGroupHandler(ContactLayerType layer_type, int free_list_idx = 0); + // 0x0000007101215b68 void removeSystemGroupHandler(SystemGroupHandler* handler); hkpWorld* getHavokWorld(ContactLayerType type) const; @@ -138,7 +143,7 @@ private: sead::FixedPtrArray _128; ContactMgr* mContactMgr; void* _150; - void* _158; + StaticCompoundMgr* mStaticCompoundMgr; RigidBodyRequestMgr* mRigidBodyRequestMgr; void* _168; void* mRigidBodyDividedMeshShapeMgr;