TrbStateVec.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. Copyright (C) 2009 Sony Computer Entertainment Inc.
  3. All rights reserved.
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #ifndef BT_TRBSTATEVEC_H__
  14. #define BT_TRBSTATEVEC_H__
  15. #include <stdlib.h>
  16. #ifdef PFX_USE_FREE_VECTORMATH
  17. #include "vecmath/vmInclude.h"
  18. #else
  19. #include "bullet/vectormath/vmInclude.h"
  20. #endif //PFX_USE_FREE_VECTORMATH
  21. #include "PlatformDefinitions.h"
  22. static inline vmVector3 read_Vector3(const float* p)
  23. {
  24. vmVector3 v;
  25. loadXYZ(v, p);
  26. return v;
  27. }
  28. static inline vmQuat read_Quat(const float* p)
  29. {
  30. vmQuat vq;
  31. loadXYZW(vq, p);
  32. return vq;
  33. }
  34. static inline void store_Vector3(const vmVector3 &src, float* p)
  35. {
  36. vmVector3 v = src;
  37. storeXYZ(v, p);
  38. }
  39. static inline void store_Quat(const vmQuat &src, float* p)
  40. {
  41. vmQuat vq = src;
  42. storeXYZW(vq, p);
  43. }
  44. // Motion Type
  45. enum {
  46. PfxMotionTypeFixed = 0,
  47. PfxMotionTypeActive,
  48. PfxMotionTypeKeyframe,
  49. PfxMotionTypeOneWay,
  50. PfxMotionTypeTrigger,
  51. PfxMotionTypeCount
  52. };
  53. #define PFX_MOTION_MASK_DYNAMIC 0x0a // Active,OneWay
  54. #define PFX_MOTION_MASK_STATIC 0x95 // Fixed,Keyframe,Trigger,Sleeping
  55. #define PFX_MOTION_MASK_SLEEP 0x0e // Can sleep
  56. #define PFX_MOTION_MASK_TYPE 0x7f
  57. //
  58. // Rigid Body state
  59. //
  60. #ifdef __CELLOS_LV2__
  61. ATTRIBUTE_ALIGNED128(class) TrbState
  62. #else
  63. ATTRIBUTE_ALIGNED16(class) TrbState
  64. #endif
  65. {
  66. public:
  67. TrbState()
  68. {
  69. setMotionType(PfxMotionTypeActive);
  70. contactFilterSelf=contactFilterTarget=0xffffffff;
  71. deleted = 0;
  72. mSleeping = 0;
  73. useSleep = 1;
  74. trbBodyIdx=0;
  75. mSleepCount=0;
  76. useCcd = 0;
  77. useContactCallback = 0;
  78. useSleepCallback = 0;
  79. linearDamping = 1.0f;
  80. angularDamping = 0.99f;
  81. }
  82. TrbState(const uint8_t m, const vmVector3& x, const vmQuat& q, const vmVector3& v, const vmVector3& omega );
  83. uint16_t mSleepCount;
  84. uint8_t mMotionType;
  85. uint8_t deleted : 1;
  86. uint8_t mSleeping : 1;
  87. uint8_t useSleep : 1;
  88. uint8_t useCcd : 1;
  89. uint8_t useContactCallback : 1;
  90. uint8_t useSleepCallback : 1;
  91. uint16_t trbBodyIdx;
  92. uint32_t contactFilterSelf;
  93. uint32_t contactFilterTarget;
  94. float center[3]; // AABB center(World)
  95. float half[3]; // AABB half(World)
  96. float linearDamping;
  97. float angularDamping;
  98. float deltaLinearVelocity[3];
  99. float deltaAngularVelocity[3];
  100. float fX[3]; // position
  101. float fQ[4]; // orientation
  102. float fV[3]; // velocity
  103. float fOmega[3]; // angular velocity
  104. inline void setZero(); // Zeroes out the elements
  105. inline void setIdentity(); // Sets the rotation to identity and zeroes out the other elements
  106. bool isDeleted() const {return deleted==1;}
  107. uint16_t getRigidBodyId() const {return trbBodyIdx;}
  108. void setRigidBodyId(uint16_t i) {trbBodyIdx = i;}
  109. uint32_t getContactFilterSelf() const {return contactFilterSelf;}
  110. void setContactFilterSelf(uint32_t filter) {contactFilterSelf = filter;}
  111. uint32_t getContactFilterTarget() const {return contactFilterTarget;}
  112. void setContactFilterTarget(uint32_t filter) {contactFilterTarget = filter;}
  113. float getLinearDamping() const {return linearDamping;}
  114. float getAngularDamping() const {return angularDamping;}
  115. void setLinearDamping(float damping) {linearDamping=damping;}
  116. void setAngularDamping(float damping) {angularDamping=damping;}
  117. uint8_t getMotionType() const {return mMotionType;}
  118. void setMotionType(uint8_t t) {mMotionType = t;mSleeping=0;mSleepCount=0;}
  119. uint8_t getMotionMask() const {return (1<<mMotionType)|(mSleeping<<7);}
  120. bool isAsleep() const {return mSleeping==1;}
  121. bool isAwake() const {return mSleeping==0;}
  122. void wakeup() {mSleeping=0;mSleepCount=0;}
  123. void sleep() {if(useSleep) {mSleeping=1;mSleepCount=0;}}
  124. uint8_t getUseSleep() const {return useSleep;}
  125. void setUseSleep(uint8_t b) {useSleep=b;}
  126. uint8_t getUseCcd() const {return useCcd;}
  127. void setUseCcd(uint8_t b) {useCcd=b;}
  128. uint8_t getUseContactCallback() const {return useContactCallback;}
  129. void setUseContactCallback(uint8_t b) {useContactCallback=b;}
  130. uint8_t getUseSleepCallback() const {return useSleepCallback;}
  131. void setUseSleepCallback(uint8_t b) {useSleepCallback=b;}
  132. void incrementSleepCount() {mSleepCount++;}
  133. void resetSleepCount() {mSleepCount=0;}
  134. uint16_t getSleepCount() const {return mSleepCount;}
  135. vmVector3 getPosition() const {return read_Vector3(fX);}
  136. vmQuat getOrientation() const {return read_Quat(fQ);}
  137. vmVector3 getLinearVelocity() const {return read_Vector3(fV);}
  138. vmVector3 getAngularVelocity() const {return read_Vector3(fOmega);}
  139. vmVector3 getDeltaLinearVelocity() const {return read_Vector3(deltaLinearVelocity);}
  140. vmVector3 getDeltaAngularVelocity() const {return read_Vector3(deltaAngularVelocity);}
  141. void setPosition(const vmVector3 &pos) {store_Vector3(pos, fX);}
  142. void setLinearVelocity(const vmVector3 &vel) {store_Vector3(vel, fV);}
  143. void setAngularVelocity(const vmVector3 &vel) {store_Vector3(vel, fOmega);}
  144. void setDeltaLinearVelocity(const vmVector3 &vel) {store_Vector3(vel, deltaLinearVelocity);}
  145. void setDeltaAngularVelocity(const vmVector3 &vel) {store_Vector3(vel, deltaAngularVelocity);}
  146. void setOrientation(const vmQuat &rot) {store_Quat(rot, fQ);}
  147. inline void setAuxils(const vmVector3 &centerLocal,const vmVector3 &halfLocal);
  148. inline void setAuxilsCcd(const vmVector3 &centerLocal,const vmVector3 &halfLocal,float timeStep);
  149. inline void reset();
  150. };
  151. inline
  152. TrbState::TrbState(const uint8_t m, const vmVector3& x, const vmQuat& q, const vmVector3& v, const vmVector3& omega)
  153. {
  154. setMotionType(m);
  155. fX[0] = x[0];
  156. fX[1] = x[1];
  157. fX[2] = x[2];
  158. fQ[0] = q[0];
  159. fQ[1] = q[1];
  160. fQ[2] = q[2];
  161. fQ[3] = q[3];
  162. fV[0] = v[0];
  163. fV[1] = v[1];
  164. fV[2] = v[2];
  165. fOmega[0] = omega[0];
  166. fOmega[1] = omega[1];
  167. fOmega[2] = omega[2];
  168. contactFilterSelf=contactFilterTarget=0xffff;
  169. trbBodyIdx=0;
  170. mSleeping = 0;
  171. deleted = 0;
  172. useSleep = 1;
  173. useCcd = 0;
  174. useContactCallback = 0;
  175. useSleepCallback = 0;
  176. mSleepCount=0;
  177. linearDamping = 1.0f;
  178. angularDamping = 0.99f;
  179. }
  180. inline void
  181. TrbState::setIdentity()
  182. {
  183. fX[0] = 0.0f;
  184. fX[1] = 0.0f;
  185. fX[2] = 0.0f;
  186. fQ[0] = 0.0f;
  187. fQ[1] = 0.0f;
  188. fQ[2] = 0.0f;
  189. fQ[3] = 1.0f;
  190. fV[0] = 0.0f;
  191. fV[1] = 0.0f;
  192. fV[2] = 0.0f;
  193. fOmega[0] = 0.0f;
  194. fOmega[1] = 0.0f;
  195. fOmega[2] = 0.0f;
  196. }
  197. inline void
  198. TrbState::setZero()
  199. {
  200. fX[0] = 0.0f;
  201. fX[1] = 0.0f;
  202. fX[2] = 0.0f;
  203. fQ[0] = 0.0f;
  204. fQ[1] = 0.0f;
  205. fQ[2] = 0.0f;
  206. fQ[3] = 0.0f;
  207. fV[0] = 0.0f;
  208. fV[1] = 0.0f;
  209. fV[2] = 0.0f;
  210. fOmega[0] = 0.0f;
  211. fOmega[1] = 0.0f;
  212. fOmega[2] = 0.0f;
  213. }
  214. inline void
  215. TrbState::setAuxils(const vmVector3 &centerLocal,const vmVector3 &halfLocal)
  216. {
  217. vmVector3 centerW = getPosition() + rotate(getOrientation(),centerLocal);
  218. vmVector3 halfW = absPerElem(vmMatrix3(getOrientation())) * halfLocal;
  219. center[0] = centerW[0];
  220. center[1] = centerW[1];
  221. center[2] = centerW[2];
  222. half[0] = halfW[0];
  223. half[1] = halfW[1];
  224. half[2] = halfW[2];
  225. }
  226. inline void
  227. TrbState::setAuxilsCcd(const vmVector3 &centerLocal,const vmVector3 &halfLocal,float timeStep)
  228. {
  229. vmVector3 centerW = getPosition() + rotate(getOrientation(),centerLocal);
  230. vmVector3 halfW = absPerElem(vmMatrix3(getOrientation())) * halfLocal;
  231. vmVector3 diffvec = getLinearVelocity()*timeStep;
  232. vmVector3 newCenter = centerW + diffvec;
  233. vmVector3 aabbMin = minPerElem(newCenter - halfW,centerW - halfW);
  234. vmVector3 aabbMax = maxPerElem(newCenter + halfW,centerW + halfW);
  235. centerW = 0.5f * (aabbMin + aabbMax);
  236. halfW =0.5f * (aabbMax - aabbMin);
  237. center[0] = centerW[0];
  238. center[1] = centerW[1];
  239. center[2] = centerW[2];
  240. half[0] = halfW[0];
  241. half[1] = halfW[1];
  242. half[2] = halfW[2];
  243. }
  244. inline
  245. void TrbState::reset()
  246. {
  247. #if 0
  248. mSleepCount = 0;
  249. mMotionType = PfxMotionTypeActive;
  250. mDeleted = 0;
  251. mSleeping = 0;
  252. mUseSleep = 1;
  253. mUseCcd = 0;
  254. mUseContactCallback = 0;
  255. mUseSleepCallback = 0;
  256. mRigidBodyId = 0;
  257. mContactFilterSelf = 0xffffffff;
  258. mContactFilterTarget = 0xffffffff;
  259. mLinearDamping = 1.0f;
  260. mAngularDamping = 0.99f;
  261. mPosition = vmVector3(0.0f);
  262. mOrientation = vmQuat::identity();
  263. mLinearVelocity = vmVector3(0.0f);
  264. mAngularVelocity = vmVector3(0.0f);
  265. #endif
  266. setMotionType(PfxMotionTypeActive);
  267. contactFilterSelf=contactFilterTarget=0xffffffff;
  268. deleted = 0;
  269. mSleeping = 0;
  270. useSleep = 1;
  271. trbBodyIdx=0;
  272. mSleepCount=0;
  273. useCcd = 0;
  274. useContactCallback = 0;
  275. useSleepCallback = 0;
  276. linearDamping = 1.0f;
  277. angularDamping = 0.99f;
  278. }
  279. #endif //BT_TRBSTATEVEC_H__