00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef NVBLASTEXTPXMANAGERIMPL_H
00030 #define NVBLASTEXTPXMANAGERIMPL_H
00031
00032 #include "NvBlastExtPxManager.h"
00033 #include "NvBlastArray.h"
00034 #include "NvBlastHashMap.h"
00035 #include "NvBlastExtPxListener.h"
00036 #include "NvBlastExtPxFamily.h"
00037
00038 #include "PxRigidDynamic.h"
00039
00040
00041 using namespace physx;
00042
00043
00044 namespace Nv
00045 {
00046 namespace Blast
00047 {
00048
00049
00050 class TkActor;
00051
00052 class ExtPxManagerImpl final : public ExtPxManager
00053 {
00054 NV_NOCOPY(ExtPxManagerImpl)
00055
00056 public:
00057 friend class ExtPxActorImpl;
00058 friend class ExtPxFamilyImpl;
00059
00060 ExtPxManagerImpl(PxPhysics& physics, TkFramework&framework, ExtPxCreateJointFunction createFn, bool usePxUserData)
00061 : m_physics(physics), m_framework(framework), m_createJointFn(createFn), m_usePxUserData(usePxUserData), m_actorCountLimit(0)
00062 {
00063 }
00064
00065 ~ExtPxManagerImpl()
00066 {
00067 }
00068
00069 virtual void release() override;
00070
00071
00073
00074 virtual ExtPxFamily* createFamily(const ExtPxFamilyDesc& desc) override;
00075
00076 virtual bool createJoint(TkJoint& joint) override;
00077
00078 virtual void destroyJoint(TkJoint& joint) override;
00079
00080 virtual void setCreateJointFunction(ExtPxCreateJointFunction createFn) override
00081 {
00082 m_createJointFn = createFn;
00083 }
00084
00085 virtual uint32_t getFamilyCount() const override
00086 {
00087 return m_tkFamiliesMap.size();
00088 }
00089
00090 virtual uint32_t getFamilies(ExtPxFamily** buffer, uint32_t bufferSize) const override
00091 {
00092 uint32_t index = 0;
00093 for (auto it = const_cast<ExtPxManagerImpl*>(this)->m_tkFamiliesMap.getIterator(); !it.done() && index < bufferSize; ++it)
00094 {
00095 buffer[index++] = it->second;
00096 }
00097 return index;
00098 }
00099
00100 virtual ExtPxFamily* getFamilyFromTkFamily(TkFamily& family) const override
00101 {
00102 auto entry = m_tkFamiliesMap.find(&family);
00103 return entry != nullptr ? entry->second : nullptr;
00104 }
00105
00106 virtual ExtPxActor* getActorFromPhysXActor(const PxRigidDynamic& pxActor) const override
00107 {
00108 auto it = m_physXActorsMap.find(&pxActor);
00109 return it != nullptr ? it->second : nullptr;
00110 }
00111
00112 virtual PxPhysics& getPhysics() const override
00113 {
00114 return m_physics;
00115 }
00116
00117 virtual TkFramework& getFramework() const override
00118 {
00119 return m_framework;
00120 }
00121
00122 virtual bool isPxUserDataUsed() const override
00123 {
00124 return m_usePxUserData;
00125 }
00126
00127 virtual void subscribe(ExtPxListener& listener) override
00128 {
00129 m_listeners.pushBack(&listener);
00130 }
00131
00132 virtual void unsubscribe(ExtPxListener& listener) override
00133 {
00134 m_listeners.findAndReplaceWithLast(&listener);
00135 }
00136
00137 virtual void setActorCountLimit(uint32_t limit) override
00138 {
00139 m_actorCountLimit = limit;
00140 }
00141
00142 virtual uint32_t getActorCountLimit() override
00143 {
00144 return m_actorCountLimit;
00145 }
00146
00147 virtual uint32_t getPxActorCount() const override
00148 {
00149 return m_physXActorsMap.size();
00150 }
00151
00152
00154
00155 void registerActor(PxRigidDynamic* pxActor, ExtPxActor* actor)
00156 {
00157 if (m_usePxUserData)
00158 {
00159 pxActor->userData = actor;
00160 }
00161 m_physXActorsMap[pxActor] = actor;
00162 }
00163
00164 void unregisterActor(PxRigidDynamic* pxActor)
00165 {
00166 if (m_usePxUserData)
00167 {
00168 pxActor->userData = nullptr;
00169 }
00170 m_physXActorsMap.erase(pxActor);
00171 }
00172
00173 void registerFamily(ExtPxFamily& family)
00174 {
00175 m_tkFamiliesMap[&family.getTkFamily()] = &family;
00176 }
00177
00178 void unregisterFamily(ExtPxFamily& family)
00179 {
00180 m_tkFamiliesMap.erase(&family.getTkFamily());
00181 }
00182
00183 void updateJoint(TkJoint& joint);
00184
00185
00187
00188 void dispatchActorCreated(ExtPxFamily& family, ExtPxActor& actor)
00189 {
00190 for (ExtPxListener* listener : m_listeners)
00191 listener->onActorCreated(family, actor);
00192 }
00193
00194 void dispatchActorDestroyed(ExtPxFamily& family, ExtPxActor&actor)
00195 {
00196 for (ExtPxListener* listener : m_listeners)
00197 listener->onActorDestroyed(family, actor);
00198 }
00199
00200
00201 private:
00202
00204
00205 PxPhysics& m_physics;
00206 TkFramework& m_framework;
00207 ExtPxCreateJointFunction m_createJointFn;
00208 bool m_usePxUserData;
00209 InlineArray<ExtPxListener*, 8>::type m_listeners;
00210 HashMap<const PxRigidDynamic*, ExtPxActor*>::type m_physXActorsMap;
00211 HashMap<TkFamily*, ExtPxFamily*>::type m_tkFamiliesMap;
00212 HashMap<TkActor*, Array<TkJoint*>::type >::type m_incompleteJointMultiMap;
00213 uint32_t m_actorCountLimit;
00214 };
00215
00216 }
00217 }
00218
00219
00220 #endif // ifndef NVBLASTEXTPXMANAGERIMPL_H