2
0

glue.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /* Copyright (c) 2015-2016 Bruce A Henderson
  2. This software is provided 'as-is', without any express or implied
  3. warranty. In no event will the authors be held liable for any damages
  4. arising from the use of this software.
  5. Permission is granted to anyone to use this software for any purpose,
  6. including commercial applications, and to alter it and redistribute it
  7. freely, subject to the following restrictions:
  8. 1. The origin of this software must not be misrepresented; you must not
  9. claim that you wrote the original software. If you use this software
  10. in a product, an acknowledgment in the product documentation would be
  11. appreciated but is not required.
  12. 2. Altered source versions must be plainly marked as such, and must not be
  13. misrepresented as being the original software.
  14. 3. This notice may not be removed or altered from any source
  15. distribution.
  16. */
  17. #include "Newton.h"
  18. #include "dMatrix.h"
  19. extern "C" {
  20. #include "brl.mod/blitz.mod/blitz.h"
  21. NewtonWorld * bmx_newtondynamics_NewtonCreate(BBObject *);
  22. NewtonBody * bmx_newtondynamics_NewtonCreateDynamicBody(BBObject *, const NewtonWorld* const, const NewtonCollision* const, const dFloat* const);
  23. BBObject * bmx_newtondynamics_NewtonBodyGetUserData(const NewtonBody* const);
  24. void bmx_newtondynamics_NewtonBodySetOmega(const NewtonBody* const, float, float, float, float);
  25. void bmx_newtondynamics_NewtonBodySetVelocity(const NewtonBody* const, float, float, float, float);
  26. void bmx_newtondynamics_NewtonBodySetForce(const NewtonBody* const, float, float, float, float);
  27. void bmx_newtondynamics_NewtonBodySetTorque(const NewtonBody* const, float, float, float, float);
  28. void bmx_newtondynamics_NewtonBodySetAngularDamping(const NewtonBody* const, float, float, float);
  29. void bmx_newtondynamics_NewtonBodyGetAngularDamping(const NewtonBody* const, float*, float*, float*);
  30. void bmx_newtondynamics_NewtonBodyGetRotation(const NewtonBody* const, float *, float *, float *, float *);
  31. void bmx_newtondynamics_NewtonBodyGetOmega(const NewtonBody* const, float *, float *, float *);
  32. void bmx_newtondynamics_NewtonBodyGetVelocity(const NewtonBody* const, float *, float *, float *);
  33. void bmx_newtondynamics_NewtonBodyGetForce(const NewtonBody* const, float *, float *, float *);
  34. void bmx_newtondynamics_NewtonBodyGetTorque(const NewtonBody* const, float *, float *, float *);
  35. void bmx_newtondynamics_NewtonBodyGetForceAcc(const NewtonBody* const, float *, float *, float *);
  36. void bmx_newtondynamics_NewtonBodyGetTorqueAcc(const NewtonBody* const, float *, float *, float *);
  37. void bmx_newtondynamics_NewtonBodyGetCentreOfMass(const NewtonBody* const, float *, float *, float *);
  38. void bmx_newtondynamics_NewtonBodyGetAABB(const NewtonBody* const, float *, float *, float *, float *, float *, float *);
  39. void bmx_newtondynamics_NewtonBodyAddForce(const NewtonBody* const, float, float, float);
  40. void bmx_newtondynamics_NewtonBodyAddTorque(const NewtonBody* const, float, float, float);
  41. void bmx_newtondynamics_NewtonBodyCalculateInverseDynamicsForce(const NewtonBody* const, float, float, float, float, float *, float *, float *);
  42. void bmx_newtondynamics_config_body(BBObject *, NewtonBody *);
  43. void bmx_newtondynamics_body_destroycallback(const NewtonBody* const);
  44. NewtonCollision * bmx_newtondynamics_NewtonCreateSphere(BBObject *, const NewtonWorld* const, float, int, float *);
  45. NewtonCollision * bmx_newtondynamics_NewtonCreateBox(BBObject *, const NewtonWorld* const, float, float, float, int, float *);
  46. NewtonCollision * bmx_newtondynamics_NewtonCreateTreeCollision(BBObject *, const NewtonWorld* const, int);
  47. NewtonCollision * bmx_newtondynamics_NewtonCreateCylinder(BBObject *, const NewtonWorld* const, float, float, int, float *);
  48. void bmx_newtondynamics_collision_copycallback(const NewtonWorld* const, NewtonCollision* const, const NewtonCollision* const);
  49. void bmx_newtondynamics_collision_destroycallback(const NewtonWorld* const, const NewtonCollision* const);
  50. void bmx_newtondynamics_config_collision(BBObject *, NewtonCollision *);
  51. void bmx_newtondynamics_matrix_GetEulerAngles(float *, float *, float *, float *, float *, float *, float *);
  52. void bmx_newtondynamics_NewtonWorldRayCast(const NewtonWorld* const, float, float, float, float, float, float,
  53. NewtonWorldRayFilterCallback, BBObject *, NewtonWorldRayPrefilterCallback, int);
  54. BBObject * bmx_newtondynamics_RayCastDelegateFromPtr(void * data);
  55. }
  56. // ********************************************************
  57. void bmx_newtondynamics_body_destroycallback(const NewtonBody* const body) {
  58. BBObject * obj = (BBObject*) NewtonBodyGetUserData(body);
  59. if (obj) {
  60. BBRELEASE(obj);
  61. }
  62. }
  63. void bmx_newtondynamics_config_body(BBObject * obj, NewtonBody * body) {
  64. NewtonBodySetUserData(body, obj);
  65. BBRETAIN(obj);
  66. NewtonBodySetDestructorCallback(body, bmx_newtondynamics_body_destroycallback);
  67. }
  68. void bmx_newtondynamics_collision_copycallback(const NewtonWorld* const world, NewtonCollision* const coll, const NewtonCollision* const source) {
  69. BBObject * obj = (BBObject*) NewtonCollisionGetUserData(source);
  70. bmx_newtondynamics_config_collision(obj, coll);
  71. }
  72. void bmx_newtondynamics_collision_destroycallback(const NewtonWorld* const world, const NewtonCollision* const coll) {
  73. BBObject * obj = (BBObject*) NewtonCollisionGetUserData(coll);
  74. if (obj) {
  75. BBRELEASE(obj);
  76. }
  77. }
  78. void bmx_newtondynamics_config_collision(BBObject * obj, NewtonCollision * coll) {
  79. NewtonCollisionSetUserData(coll, obj);
  80. BBRETAIN(obj);
  81. }
  82. // ********************************************************
  83. NewtonWorld * bmx_newtondynamics_NewtonCreate(BBObject *) {
  84. NewtonWorld * world = NewtonCreate();
  85. NewtonWorldSetCollisionConstructorDestructorCallback(world, bmx_newtondynamics_collision_copycallback, bmx_newtondynamics_collision_destroycallback);
  86. return world;
  87. }
  88. NewtonBody * bmx_newtondynamics_NewtonCreateDynamicBody(BBObject * obj, const NewtonWorld* const world, const NewtonCollision* const coll, const dFloat* const matrix) {
  89. NewtonBody * body = NewtonCreateDynamicBody(world, coll, matrix);
  90. bmx_newtondynamics_config_body(obj, body);
  91. return body;
  92. }
  93. void bmx_newtondynamics_NewtonBodySetOmega(const NewtonBody* const body, float x, float y, float z, float w) {
  94. dVector v(x, y, z, w);
  95. NewtonBodySetOmega(body, &v[0]);
  96. }
  97. void bmx_newtondynamics_NewtonBodySetVelocity(const NewtonBody* const body, float x, float y, float z, float w) {
  98. dVector v(x, y, z, w);
  99. NewtonBodySetVelocity(body, &v[0]);
  100. }
  101. void bmx_newtondynamics_NewtonBodySetForce(const NewtonBody* const body, float x, float y, float z, float w) {
  102. dVector v(x, y, z, w);
  103. NewtonBodySetForce(body, &v[0]);
  104. }
  105. void bmx_newtondynamics_NewtonBodySetTorque(const NewtonBody* const body, float x, float y, float z, float w) {
  106. dVector v(x, y, z, w);
  107. NewtonBodySetTorque(body, &v[0]);
  108. }
  109. BBObject * bmx_newtondynamics_NewtonBodyGetUserData(const NewtonBody* const body) {
  110. return (BBObject*) NewtonBodyGetUserData(body);
  111. }
  112. void bmx_newtondynamics_NewtonBodySetAngularDamping(const NewtonBody* const body, float ax, float ay, float az) {
  113. dVector v(ax, ay, az, 0.0f);
  114. NewtonBodySetAngularDamping(body, &v[0]);
  115. }
  116. void bmx_newtondynamics_NewtonBodyGetAngularDamping(const NewtonBody* const body, float * ax, float * ay, float * az) {
  117. dVector v;
  118. NewtonBodyGetAngularDamping(body, &v[0]);
  119. *ax = v.m_x;
  120. *ay = v.m_y;
  121. *az = v.m_z;
  122. }
  123. void bmx_newtondynamics_NewtonBodyGetRotation(const NewtonBody* const body, float * q0, float * q1, float * q2, float * q3) {
  124. dVector v;
  125. NewtonBodyGetRotation(body, &v[0]);
  126. *q0 = v.m_x;
  127. *q1 = v.m_y;
  128. *q2 = v.m_z;
  129. *q3 = v.m_w;
  130. }
  131. void bmx_newtondynamics_NewtonBodyGetOmega(const NewtonBody* const body, float * ox, float * oy, float * oz) {
  132. dVector v;
  133. NewtonBodyGetOmega(body, &v[0]);
  134. *ox = v.m_x;
  135. *oy = v.m_y;
  136. *oz = v.m_z;
  137. }
  138. void bmx_newtondynamics_NewtonBodyGetVelocity(const NewtonBody* const body, float * vx, float * vy, float * vz) {
  139. dVector v;
  140. NewtonBodyGetVelocity(body, &v[0]);
  141. *vx = v.m_x;
  142. *vy = v.m_y;
  143. *vz = v.m_z;
  144. }
  145. void bmx_newtondynamics_NewtonBodyGetForce(const NewtonBody* const body, float * fx, float * fy, float * fz) {
  146. dVector v;
  147. NewtonBodyGetForce(body, &v[0]);
  148. *fx = v.m_x;
  149. *fy = v.m_y;
  150. *fz = v.m_z;
  151. }
  152. void bmx_newtondynamics_NewtonBodyGetTorque(const NewtonBody* const body, float * tx, float * ty, float * tz) {
  153. dVector v;
  154. NewtonBodyGetTorque(body, &v[0]);
  155. *tx = v.m_x;
  156. *ty = v.m_y;
  157. *tz = v.m_z;
  158. }
  159. void bmx_newtondynamics_NewtonBodyGetForceAcc(const NewtonBody* const body, float * fx, float * fy, float * fz) {
  160. dVector v;
  161. NewtonBodyGetForceAcc(body, &v[0]);
  162. *fx = v.m_x;
  163. *fy = v.m_y;
  164. *fz = v.m_z;
  165. }
  166. void bmx_newtondynamics_NewtonBodyGetTorqueAcc(const NewtonBody* const body, float * tx, float * ty, float * tz) {
  167. dVector v;
  168. NewtonBodyGetTorqueAcc(body, &v[0]);
  169. *tx = v.m_x;
  170. *ty = v.m_y;
  171. *tz = v.m_z;
  172. }
  173. void bmx_newtondynamics_NewtonBodyGetCentreOfMass(const NewtonBody* const body, float * cx, float * cy, float * cz) {
  174. dVector v;
  175. NewtonBodyGetCentreOfMass(body, &v[0]);
  176. *cx = v.m_x;
  177. *cy = v.m_y;
  178. *cz = v.m_z;
  179. }
  180. void bmx_newtondynamics_NewtonBodyGetAABB(const NewtonBody* const body, float * p0x, float * p0y, float * p0z, float * p1x, float * p1y, float * p1z) {
  181. dVector v0, v1;
  182. NewtonBodyGetAABB(body, &v0[0], &v1[0]);
  183. *p0x = v0.m_x;
  184. *p0y = v0.m_y;
  185. *p0z = v0.m_z;
  186. *p1x = v1.m_x;
  187. *p1y = v1.m_y;
  188. *p1z = v1.m_z;
  189. }
  190. void bmx_newtondynamics_NewtonBodyAddForce(const NewtonBody* const body, float x, float y, float z) {
  191. dVector v(x, y, z, 0.0f);
  192. NewtonBodyAddForce(body, &v[0]);
  193. }
  194. void bmx_newtondynamics_NewtonBodyAddTorque(const NewtonBody* const body, float x, float y, float z) {
  195. dVector v(x, y, z, 0.0f);
  196. NewtonBodyAddTorque(body, &v[0]);
  197. }
  198. void bmx_newtondynamics_NewtonBodyCalculateInverseDynamicsForce(const NewtonBody* const body, float timestep, float x, float y, float z, float * fx, float * fy, float * fz) {
  199. dVector v(x, y, z, 0.0f);
  200. dVector f;
  201. NewtonBodyCalculateInverseDynamicsForce(body, timestep, &v[0], &f[0]);
  202. *fx = f.m_x;
  203. *fy = f.m_y;
  204. *fz = f.m_z;
  205. }
  206. // ********************************************************
  207. NewtonCollision * bmx_newtondynamics_NewtonCreateSphere(BBObject * obj, const NewtonWorld* const world, float radius, int shapeID, float * offsetMatrix) {
  208. NewtonCollision * coll = NewtonCreateSphere(world, radius, shapeID, offsetMatrix);
  209. bmx_newtondynamics_config_collision(obj, coll);
  210. return coll;
  211. }
  212. NewtonCollision * bmx_newtondynamics_NewtonCreateBox(BBObject * obj, const NewtonWorld* const world, float dx, float dy, float dz, int shapeID, float * offsetMatrix) {
  213. NewtonCollision * coll = NewtonCreateBox(world, dx, dy, dz, shapeID, offsetMatrix);
  214. bmx_newtondynamics_config_collision(obj, coll);
  215. return coll;
  216. }
  217. NewtonCollision * bmx_newtondynamics_NewtonCreateTreeCollision(BBObject * obj, const NewtonWorld* const world, int shapeID) {
  218. NewtonCollision * coll = NewtonCreateTreeCollision(world, shapeID);
  219. bmx_newtondynamics_config_collision(obj, coll);
  220. return coll;
  221. }
  222. NewtonCollision * bmx_newtondynamics_NewtonCreateCylinder(BBObject * obj, const NewtonWorld* const world, float radius, float height, int shapeID, float * offsetMatrix) {
  223. NewtonCollision * cyl = NewtonCreateCylinder(world, radius, height, shapeID, offsetMatrix);
  224. bmx_newtondynamics_config_collision(obj, cyl);
  225. return cyl;
  226. }
  227. void bmx_newtondynamics_NewtonWorldRayCast(const NewtonWorld* const world, float p0x, float p0y, float p0z, float p1x, float p1y, float p1z,
  228. NewtonWorldRayFilterCallback fcb, BBObject * delegate, NewtonWorldRayPrefilterCallback pfcb, int threadIndex) {
  229. dVector p0(p0x, p0y, p0z, 0.0f);
  230. dVector p1(p1x, p1y, p1z, 0.0f);
  231. NewtonWorldRayCast(world, &p0[0], &p1[0], fcb, delegate, pfcb, threadIndex);
  232. }
  233. // ********************************************************
  234. void bmx_newtondynamics_matrix_GetEulerAngles(float * frontX, float * pitch0, float * yaw0, float * roll0, float * pitch1, float * yaw1, float * roll1) {
  235. dMatrix m(frontX);
  236. dVector v0, v1;
  237. m.GetEulerAngles(v0, v1, m_pitchYawRoll);
  238. *pitch0 = v0[0];
  239. *yaw0 = v0[1];
  240. *roll0 = v0[2];
  241. *pitch1 = v1[0];
  242. *yaw1 = v1[1];
  243. *roll1 = v1[2];
  244. }
  245. BBObject * bmx_newtondynamics_RayCastDelegateFromPtr(void * data) {
  246. return (BBObject*)data;
  247. }