CombinedProxy.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. #include "CombinedProxy.h"
  2. #include "PhysicsScene.h"
  3. CombinedProxy::CombinedProxy(const char * cppfile, long cppline, const Matrix & transform, bool isDynamic, float density, PhysicsScene& _scene) :
  4. IPhysCombined(&_scene),
  5. CONSTRUCT_MCALLS,//m_calls(__FILE__, __LINE__),
  6. m_helper(m_calls),
  7. IProxy(_scene),
  8. m_realCombined(NULL),
  9. m_isBuilt(false),
  10. m_haveBuildCommand(false),
  11. m_totalMass(0.0f),
  12. m_descs(__FILE__, __LINE__),
  13. m_density(density)
  14. {
  15. SetFileLine(cppfile, cppline);
  16. m_helper.m_transform = transform;
  17. m_realCombined = NEW PhysCombined(GetFileName(), GetFileLine(), transform, isDynamic, m_density, &m_scene);
  18. SetRealPhysObject(m_realCombined);
  19. Assert(m_realCombined);
  20. }
  21. CombinedProxy::~CombinedProxy(void)
  22. {
  23. if (m_realCombined)
  24. m_realCombined->Release(), m_realCombined = NULL;
  25. DELETE_MCALLS
  26. //for (unsigned int i = 0; i < m_calls.Size(); ++i) def_delete(m_calls[i]);
  27. //m_calls.DelAll();
  28. }
  29. //////////////////////////////////////////////////////////////////////////
  30. // IPhysRigidBody
  31. //////////////////////////////////////////////////////////////////////////
  32. //Получить позицию и ориентацию в мире
  33. void CombinedProxy::GetTransform(Matrix & mtx)
  34. {
  35. if (m_haveBuildCommand && m_isBuilt)
  36. m_realCombined->GetTransform(mtx);
  37. else
  38. m_helper.GetTransform(mtx);
  39. }
  40. //Получить массу
  41. float CombinedProxy::GetMass()
  42. {
  43. if (m_haveBuildCommand && m_isBuilt)
  44. return m_realCombined->GetMass();
  45. else
  46. return m_totalMass;
  47. }
  48. //Получить центр масс
  49. Vector CombinedProxy::GetCenterMass()
  50. {
  51. if (m_haveBuildCommand && m_isBuilt)
  52. return m_realCombined->GetCenterMass();
  53. else
  54. return m_helper.GetCenterMass();
  55. }
  56. //Получить группы
  57. PhysicsCollisionGroup CombinedProxy::GetGroup()
  58. {
  59. if (m_haveBuildCommand && m_isBuilt)
  60. return m_realCombined->GetGroup();
  61. else
  62. return m_helper.GetGroup();
  63. }
  64. //Приложить силу к кости в заданной локальной точке
  65. void CombinedProxy::ApplyForce(const Vector & force, const Vector & localPosition)
  66. {
  67. m_helper.ApplyForce(force, localPosition);
  68. }
  69. //Приложить импульс в заданной локальной точке
  70. void CombinedProxy::ApplyImpulse(const Vector & force, const Vector & localPosition)
  71. {
  72. m_helper.ApplyImpulse(force, localPosition);
  73. }
  74. // приложить вращающий момент в СК актера
  75. void CombinedProxy::ApplyLocalTorque(const Vector& torque)
  76. {
  77. m_helper.ApplyLocalTorque(torque);
  78. }
  79. // установить затухание движения
  80. void CombinedProxy::SetMotionDamping ( float fDamping )
  81. {
  82. m_helper.SetMotionDamping(fDamping);
  83. }
  84. void CombinedProxy::SetMotionDamping ( float fLinDamping, float fAngDamping )
  85. {
  86. m_helper.SetMotionDamping(fLinDamping, fAngDamping);
  87. }
  88. // включить-выключить гравитацию
  89. void CombinedProxy::EnableGravity ( bool bEnable )
  90. {
  91. m_helper.EnableGravity(bEnable);
  92. }
  93. // включить-выключить кинематическое поведение
  94. void CombinedProxy::EnableKinematic(bool bEnable)
  95. {
  96. m_helper.EnableKinematic(bEnable);
  97. }
  98. //Включить-выключить объект
  99. void CombinedProxy::Activate(bool isActive)
  100. {
  101. m_helper.Activate(isActive);
  102. }
  103. // получить внутренности - !только для использования внутри сервиса
  104. void CombinedProxy::GetInternals(PhysInternal & internals) const
  105. {
  106. return m_realCombined->GetInternals(internals);
  107. }
  108. // установить материал
  109. void CombinedProxy::SetPhysMaterial(IPhysMaterial * material)
  110. {
  111. m_helper.SetPhysMaterial(material);
  112. }
  113. // получить материал
  114. IPhysMaterial * CombinedProxy::GetPhysMaterial() const
  115. {
  116. return m_realCombined->GetPhysMaterial();
  117. }
  118. // получить линейную составляющую скорости
  119. Vector CombinedProxy::GetLinearVelocity() const
  120. {
  121. return m_velocity;
  122. //return m_realCombined->GetLinearVelocity();
  123. }
  124. //////////////////////////////////////////////////////////////////////////
  125. // IPhysCombined
  126. //////////////////////////////////////////////////////////////////////////
  127. //Добавить ящик
  128. void CombinedProxy::AddBox(const Vector & size, const Matrix & transform)
  129. {
  130. m_realCombined->AddBox(size, transform);
  131. AddObjectDesc(transform, NxComputeBoxMass(Nx(size), m_density), size * 0.5f);
  132. }
  133. //Добавить шар
  134. void CombinedProxy::AddSphere(float radius, const Matrix & transform)
  135. {
  136. m_realCombined->AddSphere(radius, transform);
  137. AddObjectDesc(transform, NxComputeSphereMass(radius, m_density), 0.0f, radius);
  138. }
  139. //Добавить капсулу
  140. void CombinedProxy::AddCapsule(float radius, float height, const Matrix & transform)
  141. {
  142. m_realCombined->AddCapsule(radius, height, transform);
  143. AddObjectDesc(transform, NxComputeSphereMass(radius, m_density) + NxComputeCylinderMass(radius, height, m_density), 0.0f, radius, height);
  144. }
  145. void CombinedProxy::AddObjectDesc(const Matrix & transform, float fMass, const Vector & size, float radius, float height)
  146. {
  147. ObjectDesc & obj = m_descs[m_descs.Add()];
  148. obj.mTransform = transform;
  149. obj.fMass = fMass;
  150. obj.size = size;
  151. obj.radius = radius;
  152. obj.height = height;
  153. m_totalMass += fMass;
  154. }
  155. bool CombinedProxy::SetMass(unsigned int index, float mass)
  156. {
  157. m_calls.Add
  158. (
  159. MakeDeferrer<IPhysCombined, bool, unsigned int, float>(DEF_FREF(IPhysCombined::SetMass), index, mass)
  160. );
  161. m_totalMass -= m_descs[index].fMass;
  162. m_descs[index].fMass = mass;
  163. m_totalMass += mass;
  164. return true;
  165. }
  166. //Получить текущее число фигур для билда
  167. unsigned int CombinedProxy::GetCountForBuild()
  168. {
  169. return m_realCombined->GetCountForBuild();
  170. }
  171. //Сконструировать объект
  172. void CombinedProxy::Build()
  173. {
  174. m_calls.Add
  175. (
  176. MakeDeferrer<IPhysCombined, void>(DEF_FREF(IPhysCombined::Build))
  177. );
  178. m_haveBuildCommand = true;
  179. }
  180. //Получить количество фигур
  181. long CombinedProxy::GetCount()
  182. {
  183. return m_realCombined->GetCount();
  184. }
  185. //Установить локальную позицию
  186. bool CombinedProxy::SetLocalTransform(long index, const Matrix & transform)
  187. {
  188. m_calls.Add
  189. (
  190. MakeDeferrer<IPhysCombined, bool, long, const Matrix&>(DEF_FREF(IPhysCombined::SetLocalTransform), index, transform)
  191. );
  192. m_descs[index].mTransform = transform;
  193. return true;
  194. }
  195. //Получить локальную позицию
  196. bool CombinedProxy::GetLocalTransform(long index, Matrix & transform)
  197. {
  198. if (m_haveBuildCommand && m_isBuilt)
  199. return m_realCombined->GetLocalTransform(index, transform);
  200. transform = m_descs[index].mTransform;
  201. return true;
  202. }
  203. //Получить тип
  204. IPhysCombined::Type CombinedProxy::GetType(long index)
  205. {
  206. return m_realCombined->GetType(index);
  207. }
  208. //Получить параметры ящика
  209. bool CombinedProxy::GetBox(long index, Vector & size)
  210. {
  211. if (m_haveBuildCommand && m_isBuilt)
  212. return m_realCombined->GetBox(index, size);
  213. size = m_descs[index].size;
  214. return true;
  215. }
  216. //Получить параметры шара
  217. bool CombinedProxy::GetSphere(long index, float & radius)
  218. {
  219. if (m_haveBuildCommand && m_isBuilt)
  220. return m_realCombined->GetSphere(index, radius);
  221. radius = m_descs[index].radius;
  222. return true;
  223. }
  224. //Получить параметры капсулы
  225. bool CombinedProxy::GetCapsule(long index, float & radius, float & height)
  226. {
  227. return m_realCombined->GetCapsule(index, radius, height);
  228. radius = m_descs[index].radius;
  229. height = m_descs[index].height;
  230. return true;
  231. }
  232. //Установить глобальную позицию
  233. bool CombinedProxy::SetGlobalTransform(unsigned int index, const Matrix & transform)
  234. {
  235. m_calls.Add
  236. (
  237. MakeDeferrer<IPhysCombined, bool, unsigned int, const Matrix&>(DEF_FREF(IPhysCombined::SetGlobalTransform), index, transform)
  238. );
  239. if (m_haveBuildCommand && m_isBuilt) return true;
  240. Matrix mGlobal;
  241. GetTransform(mGlobal);
  242. mGlobal.Inverse();
  243. m_descs[index].mTransform.EqMultiplyFast(transform, mGlobal);
  244. return true;
  245. }
  246. //Получить глобальную позицию
  247. bool CombinedProxy::GetGlobalTransform(unsigned int index, Matrix & transform)
  248. {
  249. if (m_haveBuildCommand && m_isBuilt)
  250. return m_realCombined->GetGlobalTransform(index, transform);
  251. Matrix mGlobal;
  252. GetTransform(mGlobal);
  253. transform.EqMultiplyFast(m_descs[index].mTransform, mGlobal);
  254. return true;
  255. }
  256. //Включить/выключить коллизии
  257. void CombinedProxy::EnableCollision(bool enable)
  258. {
  259. m_calls.Add
  260. (
  261. MakeDeferrer<IPhysCombined, void, bool>(DEF_FREF(IPhysCombined::EnableCollision), enable)
  262. );
  263. }
  264. void CombinedProxy::EnableCollision(unsigned int index, bool enable)
  265. {
  266. m_calls.Add
  267. (
  268. MakeDeferrer<IPhysCombined, void, unsigned int, bool>(DEF_FREF(IPhysCombined::EnableCollision), index, enable)
  269. );
  270. }
  271. //Включить/выключить реакцию на коллизии
  272. void CombinedProxy::EnableResponse(unsigned int index, bool enable)
  273. {
  274. m_calls.Add
  275. (
  276. MakeDeferrer<IPhysCombined, void, unsigned int, bool>(DEF_FREF(IPhysCombined::EnableResponse), index, enable)
  277. );
  278. }
  279. //Включить/выключить рэйкаст
  280. void CombinedProxy::EnableRaycast(unsigned int index, bool enable)
  281. {
  282. m_calls.Add
  283. (
  284. MakeDeferrer<IPhysCombined, void, unsigned int, bool>(DEF_FREF(IPhysCombined::EnableRaycast), index, enable)
  285. );
  286. }
  287. //Включить/выключить визуализацию
  288. void CombinedProxy::EnableVisualization(unsigned int index, bool enable)
  289. {
  290. m_calls.Add
  291. (
  292. MakeDeferrer<IPhysCombined, void, unsigned int, bool>(DEF_FREF(IPhysCombined::EnableVisualization), index, enable)
  293. );
  294. }
  295. //Включить/выключить визуализацию для актера
  296. void CombinedProxy::EnableVisualization(bool enable)
  297. {
  298. m_calls.Add
  299. (
  300. MakeDeferrer<IPhysCombined, void, bool>(DEF_FREF(IPhysCombined::EnableVisualization), enable)
  301. );
  302. }
  303. // установить кол-во итерация солвера для актера
  304. void CombinedProxy::SetSolverIterations(unsigned int count)
  305. {
  306. m_calls.Add
  307. (
  308. MakeDeferrer<IPhysCombined, void, unsigned int>(DEF_FREF(IPhysCombined::SetSolverIterations), count)
  309. );
  310. }
  311. //Протестировать элемент на пересечение с лучом
  312. bool CombinedProxy::Raycast(unsigned int index, const Vector& from, const Vector& to, RaycastResult * details)
  313. {
  314. return m_realCombined->Raycast(index, from, to, details);
  315. }
  316. //Протестировать элемент на пересечение с боксом
  317. bool CombinedProxy::OverlapBox(unsigned int index, const Vector& size, const Matrix& transform )
  318. {
  319. //m_scene.lockQueries();
  320. bool res = m_realCombined->OverlapBox(index, size, transform);
  321. //m_scene.unlockQueries();
  322. return res;
  323. }
  324. //Протестировать элемент на пересечение со сферой
  325. bool CombinedProxy::OverlapSphere(unsigned int index, const Vector& center, float radius)
  326. {
  327. //m_scene.lockQueries();
  328. bool res = m_realCombined->OverlapSphere(index, center, radius);
  329. //m_scene.unlockQueries();
  330. return res;
  331. }
  332. //////////////////////////////////////////////////////////////////////////
  333. // IPhysBase
  334. //////////////////////////////////////////////////////////////////////////
  335. bool CombinedProxy::Release()
  336. {
  337. return IProxy::AddReleaseCount();
  338. //m_bReleaseCall = true;
  339. //return false;
  340. }
  341. //////////////////////////////////////////////////////////////////////////
  342. // IProxy
  343. //////////////////////////////////////////////////////////////////////////
  344. void CombinedProxy::OnSyncCreate()
  345. {
  346. }
  347. void CombinedProxy::OnSyncCalls()
  348. {
  349. Assert(m_realCombined);
  350. for (unsigned int i = 0; i < m_calls.Size(); ++i)
  351. {
  352. m_calls[i]->Call(m_realCombined);
  353. def_delete(m_calls[i]);
  354. }
  355. if ( m_haveBuildCommand )
  356. m_isBuilt = true;
  357. PhysInternal internals;
  358. m_realCombined->GetInternals(internals);
  359. internals.actor->userData = (IPhysBase*)this;
  360. m_velocity = m_realCombined->GetLinearVelocity();
  361. m_calls.Empty();
  362. //float fNewMass = m_realCombined->GetMass();
  363. }
  364. bool CombinedProxy::OnSyncRelease()
  365. {
  366. return IProxy::ReleaseCounts(this);
  367. //if (m_bReleaseCall)
  368. // return IPhysBase::Release();
  369. //return false;
  370. }
  371. void CombinedProxy::OnSyncTrace()
  372. {
  373. TRACE_MCALLS
  374. }