body_pair_sw.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*************************************************************************/
  2. /* body_pair_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 "body_pair_sw.h"
  30. #include "collision_solver_sw.h"
  31. #include "space_sw.h"
  32. /*
  33. #define NO_ACCUMULATE_IMPULSES
  34. #define NO_SPLIT_IMPULSES
  35. #define NO_FRICTION
  36. */
  37. #define NO_TANGENTIALS
  38. /* BODY PAIR */
  39. //#define ALLOWED_PENETRATION 0.01
  40. #define RELAXATION_TIMESTEPS 3
  41. #define MIN_VELOCITY 0.0001
  42. void BodyPairSW::_contact_added_callback(const Vector3& p_point_A,const Vector3& p_point_B,void *p_userdata) {
  43. BodyPairSW* pair = (BodyPairSW*)p_userdata;
  44. pair->contact_added_callback(p_point_A,p_point_B);
  45. }
  46. void BodyPairSW::contact_added_callback(const Vector3& p_point_A,const Vector3& p_point_B) {
  47. // check if we already have the contact
  48. //Vector3 local_A = A->get_inv_transform().xform(p_point_A);
  49. //Vector3 local_B = B->get_inv_transform().xform(p_point_B);
  50. Vector3 local_A = A->get_inv_transform().basis.xform(p_point_A);
  51. Vector3 local_B = B->get_inv_transform().basis.xform(p_point_B-offset_B);
  52. int new_index = contact_count;
  53. ERR_FAIL_COND( new_index >= (MAX_CONTACTS+1) );
  54. Contact contact;
  55. contact.acc_normal_impulse=0;
  56. contact.acc_bias_impulse=0;
  57. contact.acc_tangent_impulse=Vector3();
  58. contact.local_A=local_A;
  59. contact.local_B=local_B;
  60. contact.normal=(p_point_A-p_point_B).normalized();
  61. // attempt to determine if the contact will be reused
  62. real_t contact_recycle_radius=space->get_contact_recycle_radius();
  63. for (int i=0;i<contact_count;i++) {
  64. Contact& c = contacts[i];
  65. if (
  66. c.local_A.distance_squared_to( local_A ) < (contact_recycle_radius*contact_recycle_radius) &&
  67. c.local_B.distance_squared_to( local_B ) < (contact_recycle_radius*contact_recycle_radius) ) {
  68. contact.acc_normal_impulse=c.acc_normal_impulse;
  69. contact.acc_bias_impulse=c.acc_bias_impulse;
  70. contact.acc_tangent_impulse=c.acc_tangent_impulse;
  71. new_index=i;
  72. break;
  73. }
  74. }
  75. // figure out if the contact amount must be reduced to fit the new contact
  76. if (new_index == MAX_CONTACTS) {
  77. // remove the contact with the minimum depth
  78. int least_deep=-1;
  79. float min_depth=1e10;
  80. for (int i=0;i<=contact_count;i++) {
  81. Contact& c = (i==contact_count)?contact:contacts[i];
  82. Vector3 global_A = A->get_transform().basis.xform(c.local_A);
  83. Vector3 global_B = B->get_transform().basis.xform(c.local_B)+offset_B;
  84. Vector3 axis = global_A - global_B;
  85. float depth = axis.dot( c.normal );
  86. if (depth<min_depth) {
  87. min_depth=depth;
  88. least_deep=i;
  89. }
  90. }
  91. ERR_FAIL_COND(least_deep==-1);
  92. if (least_deep < contact_count) { //replace the last deep contact by the new one
  93. contacts[least_deep]=contact;
  94. }
  95. return;
  96. }
  97. contacts[new_index]=contact;
  98. if (new_index==contact_count) {
  99. contact_count++;
  100. }
  101. }
  102. void BodyPairSW::validate_contacts() {
  103. //make sure to erase contacts that are no longer valid
  104. real_t contact_max_separation=space->get_contact_max_separation();
  105. for (int i=0;i<contact_count;i++) {
  106. Contact& c = contacts[i];
  107. Vector3 global_A = A->get_transform().basis.xform(c.local_A);
  108. Vector3 global_B = B->get_transform().basis.xform(c.local_B)+offset_B;
  109. Vector3 axis = global_A - global_B;
  110. float depth = axis.dot( c.normal );
  111. if (depth < -contact_max_separation || (global_B + c.normal * depth - global_A).length() > contact_max_separation) {
  112. // contact no longer needed, remove
  113. if ((i+1) < contact_count) {
  114. // swap with the last one
  115. SWAP( contacts[i], contacts[ contact_count-1 ] );
  116. }
  117. i--;
  118. contact_count--;
  119. }
  120. }
  121. }
  122. bool BodyPairSW::setup(float p_step) {
  123. offset_B = B->get_transform().get_origin() - A->get_transform().get_origin();
  124. validate_contacts();
  125. Vector3 offset_A = A->get_transform().get_origin();
  126. Transform xform_Au = Transform(A->get_transform().basis,Vector3());
  127. Transform xform_A = xform_Au * A->get_shape_transform(shape_A);
  128. Transform xform_Bu = B->get_transform();
  129. xform_Bu.origin-=offset_A;
  130. Transform xform_B = xform_Bu * B->get_shape_transform(shape_B);
  131. ShapeSW *shape_A_ptr=A->get_shape(shape_A);
  132. ShapeSW *shape_B_ptr=B->get_shape(shape_B);
  133. bool collided = CollisionSolverSW::solve_static(shape_A_ptr,xform_A,shape_B_ptr,xform_B,_contact_added_callback,this,&sep_axis);
  134. this->collided=collided;
  135. if (!collided)
  136. return false;
  137. //cannot collide
  138. if (A->has_exception(B->get_self()) || B->has_exception(A->get_self()) || (A->get_mode()<=PhysicsServer::BODY_MODE_KINEMATIC && B->get_mode()<=PhysicsServer::BODY_MODE_KINEMATIC)) {
  139. return false;
  140. }
  141. real_t max_penetration = space->get_contact_max_allowed_penetration();
  142. float bias = 0.3f;
  143. if (shape_A_ptr->get_custom_bias() || shape_B_ptr->get_custom_bias()) {
  144. if (shape_A_ptr->get_custom_bias()==0)
  145. bias=shape_B_ptr->get_custom_bias();
  146. else if (shape_B_ptr->get_custom_bias()==0)
  147. bias=shape_A_ptr->get_custom_bias();
  148. else
  149. bias=(shape_B_ptr->get_custom_bias()+shape_A_ptr->get_custom_bias())*0.5;
  150. }
  151. real_t inv_dt = 1.0/p_step;
  152. for(int i=0;i<contact_count;i++) {
  153. Contact &c = contacts[i];
  154. c.active=false;
  155. Vector3 global_A = xform_Au.xform(c.local_A);
  156. Vector3 global_B = xform_Bu.xform(c.local_B);
  157. real_t depth = c.normal.dot(global_A - global_B);
  158. if (depth<=0) {
  159. c.active=false;
  160. continue;
  161. }
  162. c.active=true;
  163. int gather_A = A->can_report_contacts();
  164. int gather_B = B->can_report_contacts();
  165. c.rA = global_A;
  166. c.rB = global_B-offset_B;
  167. // contact query reporting...
  168. #if 0
  169. if (A->get_body_type() == PhysicsServer::BODY_CHARACTER)
  170. static_cast<CharacterBodySW*>(A)->report_character_contact( global_A, global_B, B );
  171. if (B->get_body_type() == PhysicsServer::BODY_CHARACTER)
  172. static_cast<CharacterBodySW*>(B)->report_character_contact( global_B, global_A, A );
  173. if (A->has_contact_query())
  174. A->report_contact( global_A, global_B, B );
  175. if (B->has_contact_query())
  176. B->report_contact( global_B, global_A, A );
  177. #endif
  178. if (A->can_report_contacts()) {
  179. Vector3 crB = A->get_angular_velocity().cross( c.rA ) + A->get_linear_velocity();
  180. A->add_contact(global_A,-c.normal,depth,shape_A,global_B,shape_B,B->get_instance_id(),B->get_self(),crB);
  181. }
  182. if (B->can_report_contacts()) {
  183. Vector3 crA = A->get_angular_velocity().cross( c.rB ) + A->get_linear_velocity();
  184. B->add_contact(global_B,c.normal,depth,shape_B,global_A,shape_A,A->get_instance_id(),A->get_self(),crA);
  185. }
  186. c.active=true;
  187. // Precompute normal mass, tangent mass, and bias.
  188. Vector3 inertia_A = A->get_inv_inertia_tensor().xform( c.rA.cross( c.normal ) );
  189. Vector3 inertia_B = B->get_inv_inertia_tensor().xform( c.rB.cross( c.normal ) );
  190. real_t kNormal = A->get_inv_mass() + B->get_inv_mass();
  191. kNormal += c.normal.dot( inertia_A.cross(c.rA ) ) + c.normal.dot( inertia_B.cross( c.rB ));
  192. c.mass_normal = 1.0f / kNormal;
  193. #if 1
  194. c.bias = -bias * inv_dt * MIN(0.0f, -depth + max_penetration);
  195. #else
  196. if (depth > max_penetration) {
  197. c.bias = (depth - max_penetration) * (1.0/(p_step*(1.0/RELAXATION_TIMESTEPS)));
  198. } else {
  199. float approach = -0.1f * (depth - max_penetration) / (CMP_EPSILON + max_penetration);
  200. approach = CLAMP( approach, CMP_EPSILON, 1.0 );
  201. c.bias = approach * (depth - max_penetration) * (1.0/p_step);
  202. }
  203. #endif
  204. c.depth=depth;
  205. Vector3 j_vec = c.normal * c.acc_normal_impulse + c.acc_tangent_impulse;
  206. A->apply_impulse( c.rA, -j_vec );
  207. B->apply_impulse( c.rB, j_vec );
  208. c.acc_bias_impulse=0;
  209. Vector3 jb_vec = c.normal * c.acc_bias_impulse;
  210. A->apply_bias_impulse( c.rA, -jb_vec );
  211. B->apply_bias_impulse( c.rB, jb_vec );
  212. }
  213. return true;
  214. }
  215. void BodyPairSW::solve(float p_step) {
  216. if (!collided)
  217. return;
  218. for(int i=0;i<contact_count;i++) {
  219. Contact &c = contacts[i];
  220. if (!c.active)
  221. continue;
  222. c.active=false; //try to deactivate, will activate itself if still needed
  223. //bias impule
  224. Vector3 crbA = A->get_biased_angular_velocity().cross( c.rA );
  225. Vector3 crbB = B->get_biased_angular_velocity().cross( c.rB );
  226. Vector3 dbv = B->get_biased_linear_velocity() + crbB - A->get_biased_linear_velocity() - crbA;
  227. real_t vbn = dbv.dot(c.normal);
  228. if (Math::abs(-vbn+c.bias)>MIN_VELOCITY) {
  229. real_t jbn = (-vbn + c.bias)*c.mass_normal;
  230. real_t jbnOld = c.acc_bias_impulse;
  231. c.acc_bias_impulse = MAX(jbnOld + jbn, 0.0f);
  232. Vector3 jb = c.normal * (c.acc_bias_impulse - jbnOld);
  233. A->apply_bias_impulse(c.rA,-jb);
  234. B->apply_bias_impulse(c.rB, jb);
  235. c.active=true;
  236. }
  237. Vector3 crA = A->get_angular_velocity().cross( c.rA );
  238. Vector3 crB = B->get_angular_velocity().cross( c.rB );
  239. Vector3 dv = B->get_linear_velocity() + crB - A->get_linear_velocity() - crA;
  240. //normal impule
  241. real_t vn = dv.dot(c.normal);
  242. if (Math::abs(vn)>MIN_VELOCITY) {
  243. real_t bounce=0;
  244. real_t jn = (-bounce -vn)*c.mass_normal;
  245. real_t jnOld = c.acc_normal_impulse;
  246. c.acc_normal_impulse = MAX(jnOld + jn, 0.0f);
  247. Vector3 j =c.normal * (c.acc_normal_impulse - jnOld);
  248. A->apply_impulse(c.rA,-j);
  249. B->apply_impulse(c.rB, j);
  250. c.active=true;
  251. }
  252. //friction impule
  253. real_t friction = A->get_friction() * B->get_friction();
  254. Vector3 lvA = A->get_linear_velocity() + A->get_angular_velocity().cross( c.rA );
  255. Vector3 lvB = B->get_linear_velocity() + B->get_angular_velocity().cross( c.rB );
  256. Vector3 dtv = lvB - lvA;
  257. real_t tn = c.normal.dot(dtv);
  258. // tangential velocity
  259. Vector3 tv = dtv - c.normal * tn;
  260. real_t tvl = tv.length();
  261. if (tvl > MIN_VELOCITY) {
  262. tv /= tvl;
  263. Vector3 temp1 = A->get_inv_inertia_tensor().xform( c.rA.cross( tv ) );
  264. Vector3 temp2 = B->get_inv_inertia_tensor().xform( c.rB.cross( tv ) );
  265. real_t t = -tvl /
  266. (A->get_inv_mass() + B->get_inv_mass() + tv.dot(temp1.cross(c.rA) + temp2.cross(c.rB)));
  267. Vector3 jt = t * tv;
  268. Vector3 jtOld = c.acc_tangent_impulse;
  269. c.acc_tangent_impulse += jt;
  270. real_t fi_len = c.acc_tangent_impulse.length();
  271. real_t jtMax = c.acc_normal_impulse * friction;
  272. if (fi_len > CMP_EPSILON && fi_len > jtMax) {
  273. c.acc_tangent_impulse*=jtMax / fi_len;
  274. }
  275. jt = c.acc_tangent_impulse - jtOld;
  276. A->apply_impulse( c.rA, -jt );
  277. B->apply_impulse( c.rB, jt );
  278. c.active=true;
  279. }
  280. }
  281. }
  282. BodyPairSW::BodyPairSW(BodySW *p_A, int p_shape_A,BodySW *p_B, int p_shape_B) : ConstraintSW(_arr,2) {
  283. A=p_A;
  284. B=p_B;
  285. shape_A=p_shape_A;
  286. shape_B=p_shape_B;
  287. space=A->get_space();
  288. A->add_constraint(this,0);
  289. B->add_constraint(this,1);
  290. contact_count=0;
  291. collided=false;
  292. }
  293. BodyPairSW::~BodyPairSW() {
  294. A->remove_constraint(this);
  295. B->remove_constraint(this);
  296. }