joints_sw.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*************************************************************************/
  2. /* joints_sw.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "joints_sw.h"
  30. #include "space_sw.h"
  31. #if 0
  32. //based on chipmunk joint constraints
  33. /* Copyright (c) 2007 Scott Lembcke
  34. *
  35. * Permission is hereby granted, free of charge, to any person obtaining a copy
  36. * of this software and associated documentation files (the "Software"), to deal
  37. * in the Software without restriction, including without limitation the rights
  38. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  39. * copies of the Software, and to permit persons to whom the Software is
  40. * furnished to do so, subject to the following conditions:
  41. *
  42. * The above copyright notice and this permission notice shall be included in
  43. * all copies or substantial portions of the Software.
  44. *
  45. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  46. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  47. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  48. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  49. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  50. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  51. * SOFTWARE.
  52. */
  53. static inline real_t k_scalar(Body2DSW *a,Body2DSW *b,const Vector2& rA, const Vector2& rB, const Vector2& n) {
  54. real_t value=0;
  55. {
  56. value+=a->get_inv_mass();
  57. real_t rcn = rA.cross(n);
  58. value+=a->get_inv_inertia() * rcn * rcn;
  59. }
  60. if (b) {
  61. value+=b->get_inv_mass();
  62. real_t rcn = rB.cross(n);
  63. value+=b->get_inv_inertia() * rcn * rcn;
  64. }
  65. return value;
  66. }
  67. bool PinJoint2DSW::setup(float p_step) {
  68. Space2DSW *space = A->get_space();
  69. ERR_FAIL_COND_V(!space,false;)
  70. rA = A->get_transform().xform(anchor_A);
  71. rB = B?B->get_transform().xform(anchor_B):anchor_B;
  72. Vector2 delta = rB - rA;
  73. rA-= A->get_transform().get_origin();
  74. if (B)
  75. rB-=B->get_transform().get_origin();
  76. real_t jdist = delta.length();
  77. correct=false;
  78. if (jdist==0)
  79. return false; // do not correct
  80. correct=true;
  81. n = delta / jdist;
  82. // calculate mass normal
  83. mass_normal = 1.0f/k_scalar(A, B, rA, rB, n);
  84. // calculate bias velocity
  85. //real_t maxBias = joint->constraint.maxBias;
  86. bias = -(get_bias()==0?space->get_constraint_bias():get_bias())*(1.0/p_step)*(jdist-dist);
  87. bias = CLAMP(bias, -get_max_bias(), +get_max_bias());
  88. // compute max impulse
  89. jn_max = get_max_force() * p_step;
  90. // apply accumulated impulse
  91. Vector2 j = n * jn_acc;
  92. A->apply_impulse(rA,-j);
  93. if (B)
  94. B->apply_impulse(rB,j);
  95. return true;
  96. }
  97. static inline Vector2
  98. relative_velocity(Body2DSW *a, Body2DSW *b, Vector2 rA, Vector2 rB){
  99. Vector2 sum = a->get_linear_velocity() -rA.tangent() * a->get_angular_velocity();
  100. if (b)
  101. return (b->get_linear_velocity() -rB.tangent() * b->get_angular_velocity()) - sum;
  102. else
  103. return -sum;
  104. }
  105. static inline real_t
  106. normal_relative_velocity(Body2DSW *a, Body2DSW *b, Vector2 rA, Vector2 rB, Vector2 n){
  107. return relative_velocity(a, b, rA, rB).dot(n);
  108. }
  109. void PinJoint2DSW::solve(float p_step){
  110. if (!correct)
  111. return;
  112. Vector2 ln = n;
  113. // compute relative velocity
  114. real_t vrn = normal_relative_velocity(A,B, rA, rB, ln);
  115. // compute normal impulse
  116. real_t jn = (bias - vrn)*mass_normal;
  117. real_t jnOld = jn_acc;
  118. jn_acc = CLAMP(jnOld + jn,-jn_max,jn_max); //cpfclamp(jnOld + jn, -joint->jnMax, joint->jnMax);
  119. jn = jn_acc - jnOld;
  120. Vector2 j = jn*ln;
  121. A->apply_impulse(rA,-j);
  122. if (B)
  123. B->apply_impulse(rB,j);
  124. }
  125. PinJoint2DSW::PinJoint2DSW(const Vector2& p_pos,Body2DSW* p_body_a,Body2DSW* p_body_b) : Joint2DSW(_arr,p_body_b?2:1) {
  126. A=p_body_a;
  127. B=p_body_b;
  128. anchor_A = p_body_a->get_inv_transform().xform(p_pos);
  129. anchor_B = p_body_b?p_body_b->get_inv_transform().xform(p_pos):p_pos;
  130. jn_acc=0;
  131. dist=0;
  132. p_body_a->add_constraint(this,0);
  133. if (p_body_b)
  134. p_body_b->add_constraint(this,1);
  135. }
  136. PinJoint2DSW::~PinJoint2DSW() {
  137. if (A)
  138. A->remove_constraint(this);
  139. if (B)
  140. B->remove_constraint(this);
  141. }
  142. //////////////////////////////////////////////
  143. //////////////////////////////////////////////
  144. //////////////////////////////////////////////
  145. static inline void
  146. k_tensor(Body2DSW *a, Body2DSW *b, Vector2 r1, Vector2 r2, Vector2 *k1, Vector2 *k2)
  147. {
  148. // calculate mass matrix
  149. // If I wasn't lazy and wrote a proper matrix class, this wouldn't be so gross...
  150. real_t k11, k12, k21, k22;
  151. real_t m_sum = a->get_inv_mass() + b->get_inv_mass();
  152. // start with I*m_sum
  153. k11 = m_sum; k12 = 0.0f;
  154. k21 = 0.0f; k22 = m_sum;
  155. // add the influence from r1
  156. real_t a_i_inv = a->get_inv_inertia();
  157. real_t r1xsq = r1.x * r1.x * a_i_inv;
  158. real_t r1ysq = r1.y * r1.y * a_i_inv;
  159. real_t r1nxy = -r1.x * r1.y * a_i_inv;
  160. k11 += r1ysq; k12 += r1nxy;
  161. k21 += r1nxy; k22 += r1xsq;
  162. // add the influnce from r2
  163. real_t b_i_inv = b->get_inv_inertia();
  164. real_t r2xsq = r2.x * r2.x * b_i_inv;
  165. real_t r2ysq = r2.y * r2.y * b_i_inv;
  166. real_t r2nxy = -r2.x * r2.y * b_i_inv;
  167. k11 += r2ysq; k12 += r2nxy;
  168. k21 += r2nxy; k22 += r2xsq;
  169. // invert
  170. real_t determinant = k11*k22 - k12*k21;
  171. ERR_FAIL_COND(determinant== 0.0);
  172. real_t det_inv = 1.0f/determinant;
  173. *k1 = Vector2( k22*det_inv, -k12*det_inv);
  174. *k2 = Vector2(-k21*det_inv, k11*det_inv);
  175. }
  176. static _FORCE_INLINE_ Vector2
  177. mult_k(const Vector2& vr, const Vector2 &k1, const Vector2 &k2)
  178. {
  179. return Vector2(vr.dot(k1), vr.dot(k2));
  180. }
  181. bool GrooveJoint2DSW::setup(float p_step) {
  182. // calculate endpoints in worldspace
  183. Vector2 ta = A->get_transform().xform(A_groove_1);
  184. Vector2 tb = A->get_transform().xform(A_groove_2);
  185. Space2DSW *space=A->get_space();
  186. // calculate axis
  187. Vector2 n = -(tb - ta).tangent().normalized();
  188. real_t d = ta.dot(n);
  189. xf_normal = n;
  190. rB = B->get_transform().basis_xform(B_anchor);
  191. // calculate tangential distance along the axis of rB
  192. real_t td = (B->get_transform().get_origin() + rB).cross(n);
  193. // calculate clamping factor and rB
  194. if(td <= ta.cross(n)){
  195. clamp = 1.0f;
  196. rA = ta - A->get_transform().get_origin();
  197. } else if(td >= tb.cross(n)){
  198. clamp = -1.0f;
  199. rA = tb - A->get_transform().get_origin();
  200. } else {
  201. clamp = 0.0f;
  202. //joint->r1 = cpvsub(cpvadd(cpvmult(cpvperp(n), -td), cpvmult(n, d)), a->p);
  203. rA = ((-n.tangent() * -td) + n*d) - A->get_transform().get_origin();
  204. }
  205. // Calculate mass tensor
  206. k_tensor(A, B, rA, rB, &k1, &k2);
  207. // compute max impulse
  208. jn_max = get_max_force() * p_step;
  209. // calculate bias velocity
  210. // cpVect delta = cpvsub(cpvadd(b->p, joint->r2), cpvadd(a->p, joint->r1));
  211. // joint->bias = cpvclamp(cpvmult(delta, -joint->constraint.biasCoef*dt_inv), joint->constraint.maxBias);
  212. Vector2 delta = (B->get_transform().get_origin() +rB) - (A->get_transform().get_origin() + rA);
  213. gbias=(delta*-(get_bias()==0?space->get_constraint_bias():get_bias())*(1.0/p_step)).clamped(get_max_bias());
  214. // apply accumulated impulse
  215. A->apply_impulse(rA,-jn_acc);
  216. B->apply_impulse(rB,jn_acc);
  217. correct=true;
  218. return true;
  219. }
  220. void GrooveJoint2DSW::solve(float p_step){
  221. // compute impulse
  222. Vector2 vr = relative_velocity(A, B, rA,rB);
  223. Vector2 j = mult_k(gbias-vr, k1, k2);
  224. Vector2 jOld = jn_acc;
  225. j+=jOld;
  226. jn_acc = (((clamp * j.cross(xf_normal)) > 0) ? j : xf_normal.project(j)).clamped(jn_max);
  227. j = jn_acc - jOld;
  228. A->apply_impulse(rA,-j);
  229. B->apply_impulse(rB,j);
  230. }
  231. GrooveJoint2DSW::GrooveJoint2DSW(const Vector2& p_a_groove1,const Vector2& p_a_groove2, const Vector2& p_b_anchor, Body2DSW* p_body_a,Body2DSW* p_body_b) : Joint2DSW(_arr,2) {
  232. A=p_body_a;
  233. B=p_body_b;
  234. A_groove_1 = A->get_inv_transform().xform(p_a_groove1);
  235. A_groove_2 = A->get_inv_transform().xform(p_a_groove2);
  236. B_anchor=B->get_inv_transform().xform(p_b_anchor);
  237. A_groove_normal = -(A_groove_2 - A_groove_1).normalized().tangent();
  238. A->add_constraint(this,0);
  239. B->add_constraint(this,1);
  240. }
  241. GrooveJoint2DSW::~GrooveJoint2DSW() {
  242. A->remove_constraint(this);
  243. B->remove_constraint(this);
  244. }
  245. //////////////////////////////////////////////
  246. //////////////////////////////////////////////
  247. //////////////////////////////////////////////
  248. bool DampedSpringJoint2DSW::setup(float p_step) {
  249. rA = A->get_transform().basis_xform(anchor_A);
  250. rB = B->get_transform().basis_xform(anchor_B);
  251. Vector2 delta = (B->get_transform().get_origin() + rB) - (A->get_transform().get_origin() + rA) ;
  252. real_t dist = delta.length();
  253. if (dist)
  254. n=delta/dist;
  255. else
  256. n=Vector2();
  257. real_t k = k_scalar(A, B, rA, rB, n);
  258. n_mass = 1.0f/k;
  259. target_vrn = 0.0f;
  260. v_coef = 1.0f - Math::exp(-damping*(p_step)*k);
  261. // apply spring force
  262. real_t f_spring = (rest_length - dist) * stiffness;
  263. Vector2 j = n * f_spring*(p_step);
  264. A->apply_impulse(rA,-j);
  265. B->apply_impulse(rB,j);
  266. return true;
  267. }
  268. void DampedSpringJoint2DSW::solve(float p_step) {
  269. // compute relative velocity
  270. real_t vrn = normal_relative_velocity(A, B, rA, rB, n) - target_vrn;
  271. // compute velocity loss from drag
  272. // not 100% certain this is derived correctly, though it makes sense
  273. real_t v_damp = -vrn*v_coef;
  274. target_vrn = vrn + v_damp;
  275. Vector2 j=n*v_damp*n_mass;
  276. A->apply_impulse(rA,-j);
  277. B->apply_impulse(rB,j);
  278. }
  279. void DampedSpringJoint2DSW::set_param(Physics2DServer::DampedStringParam p_param, real_t p_value) {
  280. switch(p_param) {
  281. case Physics2DServer::DAMPED_STRING_REST_LENGTH: {
  282. rest_length=p_value;
  283. } break;
  284. case Physics2DServer::DAMPED_STRING_DAMPING: {
  285. damping=p_value;
  286. } break;
  287. case Physics2DServer::DAMPED_STRING_STIFFNESS: {
  288. stiffness=p_value;
  289. } break;
  290. }
  291. }
  292. real_t DampedSpringJoint2DSW::get_param(Physics2DServer::DampedStringParam p_param) const{
  293. switch(p_param) {
  294. case Physics2DServer::DAMPED_STRING_REST_LENGTH: {
  295. return rest_length;
  296. } break;
  297. case Physics2DServer::DAMPED_STRING_DAMPING: {
  298. return damping;
  299. } break;
  300. case Physics2DServer::DAMPED_STRING_STIFFNESS: {
  301. return stiffness;
  302. } break;
  303. }
  304. ERR_FAIL_V(0);
  305. }
  306. DampedSpringJoint2DSW::DampedSpringJoint2DSW(const Vector2& p_anchor_a,const Vector2& p_anchor_b, Body2DSW* p_body_a,Body2DSW* p_body_b) : Joint2DSW(_arr,2) {
  307. A=p_body_a;
  308. B=p_body_b;
  309. anchor_A = A->get_inv_transform().xform(p_anchor_a);
  310. anchor_B = B->get_inv_transform().xform(p_anchor_b);
  311. rest_length=p_anchor_a.distance_to(p_anchor_b);
  312. stiffness=20;
  313. damping=1.5;
  314. A->add_constraint(this,0);
  315. B->add_constraint(this,1);
  316. }
  317. DampedSpringJoint2DSW::~DampedSpringJoint2DSW() {
  318. A->remove_constraint(this);
  319. B->remove_constraint(this);
  320. }
  321. #endif