physics_body.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  1. /*************************************************************************/
  2. /* physics_body.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 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 "physics_body.h"
  30. #include "scene/scene_string_names.h"
  31. void PhysicsBody::_notification(int p_what) {
  32. /*
  33. switch(p_what) {
  34. case NOTIFICATION_TRANSFORM_CHANGED: {
  35. PhysicsServer::get_singleton()->body_set_state(get_rid(),PhysicsServer::BODY_STATE_TRANSFORM,get_global_transform());
  36. } break;
  37. }
  38. */
  39. }
  40. Vector3 PhysicsBody::get_linear_velocity() const {
  41. return Vector3();
  42. }
  43. Vector3 PhysicsBody::get_angular_velocity() const {
  44. return Vector3();
  45. }
  46. float PhysicsBody::get_inverse_mass() const {
  47. return 0;
  48. }
  49. void PhysicsBody::set_collision_layer(uint32_t p_mask) {
  50. layer_mask=p_mask;
  51. PhysicsServer::get_singleton()->body_set_layer_mask(get_rid(),p_mask);
  52. }
  53. uint32_t PhysicsBody::get_collision_layer() const {
  54. return layer_mask;
  55. }
  56. void PhysicsBody::set_collision_mask(uint32_t p_mask) {
  57. collision_mask=p_mask;
  58. PhysicsServer::get_singleton()->body_set_collision_mask(get_rid(),p_mask);
  59. }
  60. uint32_t PhysicsBody::get_collision_mask() const {
  61. return collision_mask;
  62. }
  63. void PhysicsBody::set_collision_mask_bit(int p_bit, bool p_value) {
  64. uint32_t mask = get_collision_mask();
  65. if (p_value)
  66. mask|=1<<p_bit;
  67. else
  68. mask&=~(1<<p_bit);
  69. set_collision_mask(mask);
  70. }
  71. bool PhysicsBody::get_collision_mask_bit(int p_bit) const{
  72. return get_collision_mask()&(1<<p_bit);
  73. }
  74. void PhysicsBody::set_collision_layer_bit(int p_bit, bool p_value) {
  75. uint32_t mask = get_collision_layer();
  76. if (p_value)
  77. mask|=1<<p_bit;
  78. else
  79. mask&=~(1<<p_bit);
  80. set_collision_layer(mask);
  81. }
  82. bool PhysicsBody::get_collision_layer_bit(int p_bit) const{
  83. return get_collision_layer()&(1<<p_bit);
  84. }
  85. void PhysicsBody::add_collision_exception_with(Node* p_node) {
  86. ERR_FAIL_NULL(p_node);
  87. PhysicsBody *physics_body = p_node->cast_to<PhysicsBody>();
  88. if (!physics_body) {
  89. ERR_EXPLAIN("Collision exception only works between two objects of PhysicsBody type");
  90. }
  91. ERR_FAIL_COND(!physics_body);
  92. PhysicsServer::get_singleton()->body_add_collision_exception(get_rid(),physics_body->get_rid());
  93. }
  94. void PhysicsBody::remove_collision_exception_with(Node* p_node) {
  95. ERR_FAIL_NULL(p_node);
  96. PhysicsBody *physics_body = p_node->cast_to<PhysicsBody>();
  97. if (!physics_body) {
  98. ERR_EXPLAIN("Collision exception only works between two objects of PhysicsBody type");
  99. }
  100. ERR_FAIL_COND(!physics_body);
  101. PhysicsServer::get_singleton()->body_remove_collision_exception(get_rid(),physics_body->get_rid());
  102. }
  103. void PhysicsBody::_set_layers(uint32_t p_mask) {
  104. set_collision_layer(p_mask);
  105. set_collision_mask(p_mask);
  106. }
  107. uint32_t PhysicsBody::_get_layers() const{
  108. return get_collision_layer();
  109. }
  110. void PhysicsBody::_bind_methods() {
  111. ClassDB::bind_method(_MD("set_collision_layer","layer"),&PhysicsBody::set_collision_layer);
  112. ClassDB::bind_method(_MD("get_collision_layer"),&PhysicsBody::get_collision_layer);
  113. ClassDB::bind_method(_MD("set_collision_mask","mask"),&PhysicsBody::set_collision_mask);
  114. ClassDB::bind_method(_MD("get_collision_mask"),&PhysicsBody::get_collision_mask);
  115. ClassDB::bind_method(_MD("set_collision_mask_bit","bit","value"),&PhysicsBody::set_collision_mask_bit);
  116. ClassDB::bind_method(_MD("get_collision_mask_bit","bit"),&PhysicsBody::get_collision_mask_bit);
  117. ClassDB::bind_method(_MD("set_collision_layer_bit","bit","value"),&PhysicsBody::set_collision_layer_bit);
  118. ClassDB::bind_method(_MD("get_collision_layer_bit","bit"),&PhysicsBody::get_collision_layer_bit);
  119. ClassDB::bind_method(_MD("_set_layers","mask"),&PhysicsBody::_set_layers);
  120. ClassDB::bind_method(_MD("_get_layers"),&PhysicsBody::_get_layers);
  121. ADD_GROUP("Collision","collision_");
  122. ADD_PROPERTY(PropertyInfo(Variant::INT,"collision_layer",PROPERTY_HINT_LAYERS_3D_PHYSICS),"set_collision_layer","get_collision_layer");
  123. ADD_PROPERTY(PropertyInfo(Variant::INT,"collision_mask",PROPERTY_HINT_LAYERS_3D_PHYSICS),"set_collision_mask","get_collision_mask");
  124. }
  125. PhysicsBody::PhysicsBody(PhysicsServer::BodyMode p_mode) : CollisionObject( PhysicsServer::get_singleton()->body_create(p_mode), false) {
  126. layer_mask=1;
  127. collision_mask=1;
  128. }
  129. void StaticBody::set_friction(real_t p_friction){
  130. ERR_FAIL_COND(p_friction<0 || p_friction>1);
  131. friction=p_friction;
  132. PhysicsServer::get_singleton()->body_set_param(get_rid(),PhysicsServer::BODY_PARAM_FRICTION,friction);
  133. }
  134. real_t StaticBody::get_friction() const{
  135. return friction;
  136. }
  137. void StaticBody::set_bounce(real_t p_bounce){
  138. ERR_FAIL_COND(p_bounce<0 || p_bounce>1);
  139. bounce=p_bounce;
  140. PhysicsServer::get_singleton()->body_set_param(get_rid(),PhysicsServer::BODY_PARAM_BOUNCE,bounce);
  141. }
  142. real_t StaticBody::get_bounce() const{
  143. return bounce;
  144. }
  145. void StaticBody::set_constant_linear_velocity(const Vector3& p_vel) {
  146. constant_linear_velocity=p_vel;
  147. PhysicsServer::get_singleton()->body_set_state(get_rid(),PhysicsServer::BODY_STATE_LINEAR_VELOCITY,constant_linear_velocity);
  148. }
  149. void StaticBody::set_constant_angular_velocity(const Vector3& p_vel) {
  150. constant_angular_velocity=p_vel;
  151. PhysicsServer::get_singleton()->body_set_state(get_rid(),PhysicsServer::BODY_STATE_ANGULAR_VELOCITY,constant_angular_velocity);
  152. }
  153. Vector3 StaticBody::get_constant_linear_velocity() const {
  154. return constant_linear_velocity;
  155. }
  156. Vector3 StaticBody::get_constant_angular_velocity() const {
  157. return constant_angular_velocity;
  158. }
  159. void StaticBody::_bind_methods() {
  160. ClassDB::bind_method(_MD("set_constant_linear_velocity","vel"),&StaticBody::set_constant_linear_velocity);
  161. ClassDB::bind_method(_MD("set_constant_angular_velocity","vel"),&StaticBody::set_constant_angular_velocity);
  162. ClassDB::bind_method(_MD("get_constant_linear_velocity"),&StaticBody::get_constant_linear_velocity);
  163. ClassDB::bind_method(_MD("get_constant_angular_velocity"),&StaticBody::get_constant_angular_velocity);
  164. ClassDB::bind_method(_MD("set_friction","friction"),&StaticBody::set_friction);
  165. ClassDB::bind_method(_MD("get_friction"),&StaticBody::get_friction);
  166. ClassDB::bind_method(_MD("set_bounce","bounce"),&StaticBody::set_bounce);
  167. ClassDB::bind_method(_MD("get_bounce"),&StaticBody::get_bounce);
  168. ClassDB::bind_method(_MD("add_collision_exception_with","body:PhysicsBody"),&PhysicsBody::add_collision_exception_with);
  169. ClassDB::bind_method(_MD("remove_collision_exception_with","body:PhysicsBody"),&PhysicsBody::remove_collision_exception_with);
  170. ADD_PROPERTY( PropertyInfo(Variant::REAL,"friction",PROPERTY_HINT_RANGE,"0,1,0.01"),"set_friction","get_friction");
  171. ADD_PROPERTY( PropertyInfo(Variant::REAL,"bounce",PROPERTY_HINT_RANGE,"0,1,0.01"),"set_bounce","get_bounce");
  172. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3,"constant_linear_velocity"),"set_constant_linear_velocity","get_constant_linear_velocity");
  173. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3,"constant_angular_velocity"),"set_constant_angular_velocity","get_constant_angular_velocity");
  174. }
  175. StaticBody::StaticBody() : PhysicsBody(PhysicsServer::BODY_MODE_STATIC) {
  176. bounce=0;
  177. friction=1;
  178. }
  179. StaticBody::~StaticBody() {
  180. }
  181. void RigidBody::_body_enter_tree(ObjectID p_id) {
  182. Object *obj = ObjectDB::get_instance(p_id);
  183. Node *node = obj ? obj->cast_to<Node>() : NULL;
  184. ERR_FAIL_COND(!node);
  185. Map<ObjectID,BodyState>::Element *E=contact_monitor->body_map.find(p_id);
  186. ERR_FAIL_COND(!E);
  187. ERR_FAIL_COND(E->get().in_tree);
  188. E->get().in_tree=true;
  189. contact_monitor->locked=true;
  190. emit_signal(SceneStringNames::get_singleton()->body_entered,node);
  191. for(int i=0;i<E->get().shapes.size();i++) {
  192. emit_signal(SceneStringNames::get_singleton()->body_shape_entered,p_id,node,E->get().shapes[i].body_shape,E->get().shapes[i].local_shape);
  193. }
  194. contact_monitor->locked=false;
  195. }
  196. void RigidBody::_body_exit_tree(ObjectID p_id) {
  197. Object *obj = ObjectDB::get_instance(p_id);
  198. Node *node = obj ? obj->cast_to<Node>() : NULL;
  199. ERR_FAIL_COND(!node);
  200. Map<ObjectID,BodyState>::Element *E=contact_monitor->body_map.find(p_id);
  201. ERR_FAIL_COND(!E);
  202. ERR_FAIL_COND(!E->get().in_tree);
  203. E->get().in_tree=false;
  204. contact_monitor->locked=true;
  205. emit_signal(SceneStringNames::get_singleton()->body_exited,node);
  206. for(int i=0;i<E->get().shapes.size();i++) {
  207. emit_signal(SceneStringNames::get_singleton()->body_shape_exited,p_id,node,E->get().shapes[i].body_shape,E->get().shapes[i].local_shape);
  208. }
  209. contact_monitor->locked=false;
  210. }
  211. void RigidBody::_body_inout(int p_status, ObjectID p_instance, int p_body_shape,int p_local_shape) {
  212. bool body_in = p_status==1;
  213. ObjectID objid=p_instance;
  214. Object *obj = ObjectDB::get_instance(objid);
  215. Node *node = obj ? obj->cast_to<Node>() : NULL;
  216. Map<ObjectID,BodyState>::Element *E=contact_monitor->body_map.find(objid);
  217. ERR_FAIL_COND(!body_in && !E);
  218. if (body_in) {
  219. if (!E) {
  220. E = contact_monitor->body_map.insert(objid,BodyState());
  221. //E->get().rc=0;
  222. E->get().in_tree=node && node->is_inside_tree();
  223. if (node) {
  224. node->connect(SceneStringNames::get_singleton()->tree_entered,this,SceneStringNames::get_singleton()->_body_enter_tree,make_binds(objid));
  225. node->connect(SceneStringNames::get_singleton()->tree_exited,this,SceneStringNames::get_singleton()->_body_exit_tree,make_binds(objid));
  226. if (E->get().in_tree) {
  227. emit_signal(SceneStringNames::get_singleton()->body_entered,node);
  228. }
  229. }
  230. }
  231. //E->get().rc++;
  232. if (node)
  233. E->get().shapes.insert(ShapePair(p_body_shape,p_local_shape));
  234. if (E->get().in_tree) {
  235. emit_signal(SceneStringNames::get_singleton()->body_shape_entered,objid,node,p_body_shape,p_local_shape);
  236. }
  237. } else {
  238. //E->get().rc--;
  239. if (node)
  240. E->get().shapes.erase(ShapePair(p_body_shape,p_local_shape));
  241. bool in_tree = E->get().in_tree;
  242. if (E->get().shapes.empty()) {
  243. if (node) {
  244. node->disconnect(SceneStringNames::get_singleton()->tree_entered,this,SceneStringNames::get_singleton()->_body_enter_tree);
  245. node->disconnect(SceneStringNames::get_singleton()->tree_exited,this,SceneStringNames::get_singleton()->_body_exit_tree);
  246. if (in_tree)
  247. emit_signal(SceneStringNames::get_singleton()->body_exited,obj);
  248. }
  249. contact_monitor->body_map.erase(E);
  250. }
  251. if (node && in_tree) {
  252. emit_signal(SceneStringNames::get_singleton()->body_shape_exited,objid,obj,p_body_shape,p_local_shape);
  253. }
  254. }
  255. }
  256. struct _RigidBodyInOut {
  257. ObjectID id;
  258. int shape;
  259. int local_shape;
  260. };
  261. void RigidBody::_direct_state_changed(Object *p_state) {
  262. //eh.. fuck
  263. #ifdef DEBUG_ENABLED
  264. state=p_state->cast_to<PhysicsDirectBodyState>();
  265. #else
  266. state=(PhysicsDirectBodyState*)p_state; //trust it
  267. #endif
  268. set_ignore_transform_notification(true);
  269. set_global_transform(state->get_transform());
  270. linear_velocity=state->get_linear_velocity();
  271. angular_velocity=state->get_angular_velocity();
  272. if(sleeping!=state->is_sleeping()) {
  273. sleeping=state->is_sleeping();
  274. emit_signal(SceneStringNames::get_singleton()->sleeping_state_changed);
  275. }
  276. if (get_script_instance())
  277. get_script_instance()->call("_integrate_forces",state);
  278. set_ignore_transform_notification(false);
  279. if (contact_monitor) {
  280. contact_monitor->locked=true;
  281. //untag all
  282. int rc=0;
  283. for( Map<ObjectID,BodyState>::Element *E=contact_monitor->body_map.front();E;E=E->next()) {
  284. for(int i=0;i<E->get().shapes.size();i++) {
  285. E->get().shapes[i].tagged=false;
  286. rc++;
  287. }
  288. }
  289. _RigidBodyInOut *toadd=(_RigidBodyInOut*)alloca(state->get_contact_count()*sizeof(_RigidBodyInOut));
  290. int toadd_count=0;//state->get_contact_count();
  291. RigidBody_RemoveAction *toremove=(RigidBody_RemoveAction*)alloca(rc*sizeof(RigidBody_RemoveAction));
  292. int toremove_count=0;
  293. //put the ones to add
  294. for(int i=0;i<state->get_contact_count();i++) {
  295. ObjectID obj = state->get_contact_collider_id(i);
  296. int local_shape = state->get_contact_local_shape(i);
  297. int shape = state->get_contact_collider_shape(i);
  298. //bool found=false;
  299. Map<ObjectID,BodyState>::Element *E=contact_monitor->body_map.find(obj);
  300. if (!E) {
  301. toadd[toadd_count].local_shape=local_shape;
  302. toadd[toadd_count].id=obj;
  303. toadd[toadd_count].shape=shape;
  304. toadd_count++;
  305. continue;
  306. }
  307. ShapePair sp( shape,local_shape );
  308. int idx = E->get().shapes.find(sp);
  309. if (idx==-1) {
  310. toadd[toadd_count].local_shape=local_shape;
  311. toadd[toadd_count].id=obj;
  312. toadd[toadd_count].shape=shape;
  313. toadd_count++;
  314. continue;
  315. }
  316. E->get().shapes[idx].tagged=true;
  317. }
  318. //put the ones to remove
  319. for( Map<ObjectID,BodyState>::Element *E=contact_monitor->body_map.front();E;E=E->next()) {
  320. for(int i=0;i<E->get().shapes.size();i++) {
  321. if (!E->get().shapes[i].tagged) {
  322. toremove[toremove_count].body_id=E->key();
  323. toremove[toremove_count].pair=E->get().shapes[i];
  324. toremove_count++;
  325. }
  326. }
  327. }
  328. //process remotions
  329. for(int i=0;i<toremove_count;i++) {
  330. _body_inout(0,toremove[i].body_id,toremove[i].pair.body_shape,toremove[i].pair.local_shape);
  331. }
  332. //process aditions
  333. for(int i=0;i<toadd_count;i++) {
  334. _body_inout(1,toadd[i].id,toadd[i].shape,toadd[i].local_shape);
  335. }
  336. contact_monitor->locked=false;
  337. }
  338. state=NULL;
  339. }
  340. void RigidBody::_notification(int p_what) {
  341. }
  342. void RigidBody::set_mode(Mode p_mode) {
  343. mode=p_mode;
  344. switch(p_mode) {
  345. case MODE_RIGID: {
  346. PhysicsServer::get_singleton()->body_set_mode(get_rid(),PhysicsServer::BODY_MODE_RIGID);
  347. } break;
  348. case MODE_STATIC: {
  349. PhysicsServer::get_singleton()->body_set_mode(get_rid(),PhysicsServer::BODY_MODE_STATIC);
  350. } break;
  351. case MODE_CHARACTER: {
  352. PhysicsServer::get_singleton()->body_set_mode(get_rid(),PhysicsServer::BODY_MODE_CHARACTER);
  353. } break;
  354. case MODE_KINEMATIC: {
  355. PhysicsServer::get_singleton()->body_set_mode(get_rid(),PhysicsServer::BODY_MODE_KINEMATIC);
  356. } break;
  357. }
  358. }
  359. RigidBody::Mode RigidBody::get_mode() const{
  360. return mode;
  361. }
  362. void RigidBody::set_mass(real_t p_mass){
  363. ERR_FAIL_COND(p_mass<=0);
  364. mass=p_mass;
  365. _change_notify("mass");
  366. _change_notify("weight");
  367. PhysicsServer::get_singleton()->body_set_param(get_rid(),PhysicsServer::BODY_PARAM_MASS,mass);
  368. }
  369. real_t RigidBody::get_mass() const{
  370. return mass;
  371. }
  372. void RigidBody::set_weight(real_t p_weight){
  373. set_mass(p_weight/9.8);
  374. }
  375. real_t RigidBody::get_weight() const{
  376. return mass*9.8;
  377. }
  378. void RigidBody::set_friction(real_t p_friction){
  379. ERR_FAIL_COND(p_friction<0 || p_friction>1);
  380. friction=p_friction;
  381. PhysicsServer::get_singleton()->body_set_param(get_rid(),PhysicsServer::BODY_PARAM_FRICTION,friction);
  382. }
  383. real_t RigidBody::get_friction() const{
  384. return friction;
  385. }
  386. void RigidBody::set_bounce(real_t p_bounce){
  387. ERR_FAIL_COND(p_bounce<0 || p_bounce>1);
  388. bounce=p_bounce;
  389. PhysicsServer::get_singleton()->body_set_param(get_rid(),PhysicsServer::BODY_PARAM_BOUNCE,bounce);
  390. }
  391. real_t RigidBody::get_bounce() const{
  392. return bounce;
  393. }
  394. void RigidBody::set_gravity_scale(real_t p_gravity_scale){
  395. gravity_scale=p_gravity_scale;
  396. PhysicsServer::get_singleton()->body_set_param(get_rid(),PhysicsServer::BODY_PARAM_GRAVITY_SCALE,gravity_scale);
  397. }
  398. real_t RigidBody::get_gravity_scale() const{
  399. return gravity_scale;
  400. }
  401. void RigidBody::set_linear_damp(real_t p_linear_damp){
  402. ERR_FAIL_COND(p_linear_damp<-1);
  403. linear_damp=p_linear_damp;
  404. PhysicsServer::get_singleton()->body_set_param(get_rid(),PhysicsServer::BODY_PARAM_LINEAR_DAMP,linear_damp);
  405. }
  406. real_t RigidBody::get_linear_damp() const{
  407. return linear_damp;
  408. }
  409. void RigidBody::set_angular_damp(real_t p_angular_damp){
  410. ERR_FAIL_COND(p_angular_damp<-1);
  411. angular_damp=p_angular_damp;
  412. PhysicsServer::get_singleton()->body_set_param(get_rid(),PhysicsServer::BODY_PARAM_ANGULAR_DAMP,angular_damp);
  413. }
  414. real_t RigidBody::get_angular_damp() const{
  415. return angular_damp;
  416. }
  417. void RigidBody::set_axis_velocity(const Vector3& p_axis) {
  418. Vector3 v = state? state->get_linear_velocity() : linear_velocity;
  419. Vector3 axis = p_axis.normalized();
  420. v-=axis*axis.dot(v);
  421. v+=p_axis;
  422. if (state) {
  423. set_linear_velocity(v);
  424. } else {
  425. PhysicsServer::get_singleton()->body_set_axis_velocity(get_rid(),p_axis);
  426. linear_velocity=v;
  427. }
  428. }
  429. void RigidBody::set_linear_velocity(const Vector3& p_velocity){
  430. linear_velocity=p_velocity;
  431. if (state)
  432. state->set_linear_velocity(linear_velocity);
  433. else
  434. PhysicsServer::get_singleton()->body_set_state(get_rid(),PhysicsServer::BODY_STATE_LINEAR_VELOCITY,linear_velocity);
  435. }
  436. Vector3 RigidBody::get_linear_velocity() const{
  437. return linear_velocity;
  438. }
  439. void RigidBody::set_angular_velocity(const Vector3& p_velocity){
  440. angular_velocity=p_velocity;
  441. if (state)
  442. state->set_angular_velocity(angular_velocity);
  443. else
  444. PhysicsServer::get_singleton()->body_set_state(get_rid(),PhysicsServer::BODY_STATE_ANGULAR_VELOCITY,angular_velocity);
  445. }
  446. Vector3 RigidBody::get_angular_velocity() const{
  447. return angular_velocity;
  448. }
  449. void RigidBody::set_use_custom_integrator(bool p_enable){
  450. if (custom_integrator==p_enable)
  451. return;
  452. custom_integrator=p_enable;
  453. PhysicsServer::get_singleton()->body_set_omit_force_integration(get_rid(),p_enable);
  454. }
  455. bool RigidBody::is_using_custom_integrator(){
  456. return custom_integrator;
  457. }
  458. void RigidBody::set_sleeping(bool p_sleeping) {
  459. sleeping=p_sleeping;
  460. PhysicsServer::get_singleton()->body_set_state(get_rid(),PhysicsServer::BODY_STATE_SLEEPING,sleeping);
  461. }
  462. void RigidBody::set_can_sleep(bool p_active) {
  463. can_sleep=p_active;
  464. PhysicsServer::get_singleton()->body_set_state(get_rid(),PhysicsServer::BODY_STATE_CAN_SLEEP,p_active);
  465. }
  466. bool RigidBody::is_able_to_sleep() const {
  467. return can_sleep;
  468. }
  469. bool RigidBody::is_sleeping() const {
  470. return sleeping;
  471. }
  472. void RigidBody::set_max_contacts_reported(int p_amount) {
  473. max_contacts_reported=p_amount;
  474. PhysicsServer::get_singleton()->body_set_max_contacts_reported(get_rid(),p_amount);
  475. }
  476. int RigidBody::get_max_contacts_reported() const{
  477. return max_contacts_reported;
  478. }
  479. void RigidBody::apply_impulse(const Vector3& p_pos, const Vector3& p_impulse) {
  480. PhysicsServer::get_singleton()->body_apply_impulse(get_rid(),p_pos,p_impulse);
  481. }
  482. void RigidBody::set_use_continuous_collision_detection(bool p_enable) {
  483. ccd=p_enable;
  484. PhysicsServer::get_singleton()->body_set_enable_continuous_collision_detection(get_rid(),p_enable);
  485. }
  486. bool RigidBody::is_using_continuous_collision_detection() const {
  487. return ccd;
  488. }
  489. void RigidBody::set_contact_monitor(bool p_enabled) {
  490. if (p_enabled==is_contact_monitor_enabled())
  491. return;
  492. if (!p_enabled) {
  493. if (contact_monitor->locked) {
  494. ERR_EXPLAIN("Can't disable contact monitoring during in/out callback. Use call_deferred(\"set_contact_monitor\",false) instead");
  495. }
  496. ERR_FAIL_COND(contact_monitor->locked);
  497. for(Map<ObjectID,BodyState>::Element *E=contact_monitor->body_map.front();E;E=E->next()) {
  498. //clean up mess
  499. }
  500. memdelete( contact_monitor );
  501. contact_monitor=NULL;
  502. } else {
  503. contact_monitor = memnew( ContactMonitor );
  504. contact_monitor->locked=false;
  505. }
  506. }
  507. bool RigidBody::is_contact_monitor_enabled() const {
  508. return contact_monitor!=NULL;
  509. }
  510. void RigidBody::set_axis_lock(AxisLock p_lock) {
  511. axis_lock=p_lock;
  512. PhysicsServer::get_singleton()->body_set_axis_lock(get_rid(),PhysicsServer::BodyAxisLock(axis_lock));
  513. }
  514. RigidBody::AxisLock RigidBody::get_axis_lock() const {
  515. return axis_lock;
  516. }
  517. Array RigidBody::get_colliding_bodies() const {
  518. ERR_FAIL_COND_V(!contact_monitor,Array());
  519. Array ret;
  520. ret.resize(contact_monitor->body_map.size());
  521. int idx=0;
  522. for (const Map<ObjectID,BodyState>::Element *E=contact_monitor->body_map.front();E;E=E->next()) {
  523. Object *obj = ObjectDB::get_instance(E->key());
  524. if (!obj) {
  525. ret.resize( ret.size() -1 ); //ops
  526. } else {
  527. ret[idx++]=obj;
  528. }
  529. }
  530. return ret;
  531. }
  532. void RigidBody::_bind_methods() {
  533. ClassDB::bind_method(_MD("set_mode","mode"),&RigidBody::set_mode);
  534. ClassDB::bind_method(_MD("get_mode"),&RigidBody::get_mode);
  535. ClassDB::bind_method(_MD("set_mass","mass"),&RigidBody::set_mass);
  536. ClassDB::bind_method(_MD("get_mass"),&RigidBody::get_mass);
  537. ClassDB::bind_method(_MD("set_weight","weight"),&RigidBody::set_weight);
  538. ClassDB::bind_method(_MD("get_weight"),&RigidBody::get_weight);
  539. ClassDB::bind_method(_MD("set_friction","friction"),&RigidBody::set_friction);
  540. ClassDB::bind_method(_MD("get_friction"),&RigidBody::get_friction);
  541. ClassDB::bind_method(_MD("set_bounce","bounce"),&RigidBody::set_bounce);
  542. ClassDB::bind_method(_MD("get_bounce"),&RigidBody::get_bounce);
  543. ClassDB::bind_method(_MD("set_linear_velocity","linear_velocity"),&RigidBody::set_linear_velocity);
  544. ClassDB::bind_method(_MD("get_linear_velocity"),&RigidBody::get_linear_velocity);
  545. ClassDB::bind_method(_MD("set_angular_velocity","angular_velocity"),&RigidBody::set_angular_velocity);
  546. ClassDB::bind_method(_MD("get_angular_velocity"),&RigidBody::get_angular_velocity);
  547. ClassDB::bind_method(_MD("set_gravity_scale","gravity_scale"),&RigidBody::set_gravity_scale);
  548. ClassDB::bind_method(_MD("get_gravity_scale"),&RigidBody::get_gravity_scale);
  549. ClassDB::bind_method(_MD("set_linear_damp","linear_damp"),&RigidBody::set_linear_damp);
  550. ClassDB::bind_method(_MD("get_linear_damp"),&RigidBody::get_linear_damp);
  551. ClassDB::bind_method(_MD("set_angular_damp","angular_damp"),&RigidBody::set_angular_damp);
  552. ClassDB::bind_method(_MD("get_angular_damp"),&RigidBody::get_angular_damp);
  553. ClassDB::bind_method(_MD("set_max_contacts_reported","amount"),&RigidBody::set_max_contacts_reported);
  554. ClassDB::bind_method(_MD("get_max_contacts_reported"),&RigidBody::get_max_contacts_reported);
  555. ClassDB::bind_method(_MD("set_use_custom_integrator","enable"),&RigidBody::set_use_custom_integrator);
  556. ClassDB::bind_method(_MD("is_using_custom_integrator"),&RigidBody::is_using_custom_integrator);
  557. ClassDB::bind_method(_MD("set_contact_monitor","enabled"),&RigidBody::set_contact_monitor);
  558. ClassDB::bind_method(_MD("is_contact_monitor_enabled"),&RigidBody::is_contact_monitor_enabled);
  559. ClassDB::bind_method(_MD("set_use_continuous_collision_detection","enable"),&RigidBody::set_use_continuous_collision_detection);
  560. ClassDB::bind_method(_MD("is_using_continuous_collision_detection"),&RigidBody::is_using_continuous_collision_detection);
  561. ClassDB::bind_method(_MD("set_axis_velocity","axis_velocity"),&RigidBody::set_axis_velocity);
  562. ClassDB::bind_method(_MD("apply_impulse","pos","impulse"),&RigidBody::apply_impulse);
  563. ClassDB::bind_method(_MD("set_sleeping","sleeping"),&RigidBody::set_sleeping);
  564. ClassDB::bind_method(_MD("is_sleeping"),&RigidBody::is_sleeping);
  565. ClassDB::bind_method(_MD("set_can_sleep","able_to_sleep"),&RigidBody::set_can_sleep);
  566. ClassDB::bind_method(_MD("is_able_to_sleep"),&RigidBody::is_able_to_sleep);
  567. ClassDB::bind_method(_MD("_direct_state_changed"),&RigidBody::_direct_state_changed);
  568. ClassDB::bind_method(_MD("_body_enter_tree"),&RigidBody::_body_enter_tree);
  569. ClassDB::bind_method(_MD("_body_exit_tree"),&RigidBody::_body_exit_tree);
  570. ClassDB::bind_method(_MD("set_axis_lock","axis_lock"),&RigidBody::set_axis_lock);
  571. ClassDB::bind_method(_MD("get_axis_lock"),&RigidBody::get_axis_lock);
  572. ClassDB::bind_method(_MD("get_colliding_bodies"),&RigidBody::get_colliding_bodies);
  573. BIND_VMETHOD(MethodInfo("_integrate_forces",PropertyInfo(Variant::OBJECT,"state:PhysicsDirectBodyState")));
  574. ADD_PROPERTY( PropertyInfo(Variant::INT,"mode",PROPERTY_HINT_ENUM,"Rigid,Static,Character,Kinematic"),"set_mode","get_mode");
  575. ADD_PROPERTY( PropertyInfo(Variant::REAL,"mass",PROPERTY_HINT_EXP_RANGE,"0.01,65535,0.01"),"set_mass","get_mass");
  576. ADD_PROPERTY( PropertyInfo(Variant::REAL,"weight",PROPERTY_HINT_EXP_RANGE,"0.01,65535,0.01",PROPERTY_USAGE_EDITOR),"set_weight","get_weight");
  577. ADD_PROPERTY( PropertyInfo(Variant::REAL,"friction",PROPERTY_HINT_RANGE,"0,1,0.01"),"set_friction","get_friction");
  578. ADD_PROPERTY( PropertyInfo(Variant::REAL,"bounce",PROPERTY_HINT_RANGE,"0,1,0.01"),"set_bounce","get_bounce");
  579. ADD_PROPERTY( PropertyInfo(Variant::REAL,"gravity_scale",PROPERTY_HINT_RANGE,"-128,128,0.01"),"set_gravity_scale","get_gravity_scale");
  580. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"custom_integrator"),"set_use_custom_integrator","is_using_custom_integrator");
  581. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"continuous_cd"),"set_use_continuous_collision_detection","is_using_continuous_collision_detection");
  582. ADD_PROPERTY( PropertyInfo(Variant::INT,"contacts_reported"),"set_max_contacts_reported","get_max_contacts_reported");
  583. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"contact_monitor"),"set_contact_monitor","is_contact_monitor_enabled");
  584. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"sleeping"),"set_sleeping","is_sleeping");
  585. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"can_sleep"),"set_can_sleep","is_able_to_sleep");
  586. ADD_PROPERTY( PropertyInfo(Variant::INT,"axis_lock",PROPERTY_HINT_ENUM,"Disabled,Lock X,Lock Y,Lock Z"),"set_axis_lock","get_axis_lock");
  587. ADD_GROUP("Linear","linear_");
  588. ADD_PROPERTY( PropertyInfo(Variant::VECTOR3,"linear_velocity"),"set_linear_velocity","get_linear_velocity");
  589. ADD_PROPERTY( PropertyInfo(Variant::REAL,"linear_damp",PROPERTY_HINT_RANGE,"-1,128,0.01"),"set_linear_damp","get_linear_damp");
  590. ADD_GROUP("Angular","angular_");
  591. ADD_PROPERTY( PropertyInfo(Variant::VECTOR3,"angular_velocity"),"set_angular_velocity","get_angular_velocity");
  592. ADD_PROPERTY( PropertyInfo(Variant::REAL,"angular_damp",PROPERTY_HINT_RANGE,"-1,128,0.01"),"set_angular_damp","get_angular_damp");
  593. ADD_SIGNAL( MethodInfo("body_shape_entered",PropertyInfo(Variant::INT,"body_id"),PropertyInfo(Variant::OBJECT,"body"),PropertyInfo(Variant::INT,"body_shape"),PropertyInfo(Variant::INT,"local_shape")));
  594. ADD_SIGNAL( MethodInfo("body_shape_exited",PropertyInfo(Variant::INT,"body_id"),PropertyInfo(Variant::OBJECT,"body"),PropertyInfo(Variant::INT,"body_shape"),PropertyInfo(Variant::INT,"local_shape")));
  595. ADD_SIGNAL( MethodInfo("body_entered",PropertyInfo(Variant::OBJECT,"body")));
  596. ADD_SIGNAL( MethodInfo("body_exited",PropertyInfo(Variant::OBJECT,"body")));
  597. ADD_SIGNAL( MethodInfo("sleeping_state_changed"));
  598. BIND_CONSTANT( MODE_STATIC );
  599. BIND_CONSTANT( MODE_KINEMATIC );
  600. BIND_CONSTANT( MODE_RIGID );
  601. BIND_CONSTANT( MODE_CHARACTER );
  602. }
  603. RigidBody::RigidBody() : PhysicsBody(PhysicsServer::BODY_MODE_RIGID) {
  604. mode=MODE_RIGID;
  605. bounce=0;
  606. mass=1;
  607. friction=1;
  608. max_contacts_reported=0;
  609. state=NULL;
  610. gravity_scale=1;
  611. linear_damp=-1;
  612. angular_damp=-1;
  613. //angular_velocity=0;
  614. sleeping=false;
  615. ccd=false;
  616. custom_integrator=false;
  617. contact_monitor=NULL;
  618. can_sleep=true;
  619. axis_lock = AXIS_LOCK_DISABLED;
  620. PhysicsServer::get_singleton()->body_set_force_integration_callback(get_rid(),this,"_direct_state_changed");
  621. }
  622. RigidBody::~RigidBody() {
  623. if (contact_monitor)
  624. memdelete( contact_monitor );
  625. }
  626. //////////////////////////////////////////////////////
  627. //////////////////////////
  628. Variant KinematicBody::_get_collider() const {
  629. ObjectID oid=get_collider();
  630. if (oid==0)
  631. return Variant();
  632. Object *obj = ObjectDB::get_instance(oid);
  633. if (!obj)
  634. return Variant();
  635. Reference *ref = obj->cast_to<Reference>();
  636. if (ref) {
  637. return Ref<Reference>(ref);
  638. }
  639. return obj;
  640. }
  641. bool KinematicBody::_ignores_mode(PhysicsServer::BodyMode p_mode) const {
  642. switch(p_mode) {
  643. case PhysicsServer::BODY_MODE_STATIC: return !collide_static;
  644. case PhysicsServer::BODY_MODE_KINEMATIC: return !collide_kinematic;
  645. case PhysicsServer::BODY_MODE_RIGID: return !collide_rigid;
  646. case PhysicsServer::BODY_MODE_CHARACTER: return !collide_character;
  647. }
  648. return true;
  649. }
  650. Vector3 KinematicBody::move(const Vector3& p_motion) {
  651. //give me back regular physics engine logic
  652. //this is madness
  653. //and most people using this function will think
  654. //what it does is simpler than using physics
  655. //this took about a week to get right..
  656. //but is it right? who knows at this point..
  657. colliding=false;
  658. ERR_FAIL_COND_V(!is_inside_tree(),Vector3());
  659. PhysicsDirectSpaceState *dss = PhysicsServer::get_singleton()->space_get_direct_state(get_world()->get_space());
  660. ERR_FAIL_COND_V(!dss,Vector3());
  661. const int max_shapes=32;
  662. Vector3 sr[max_shapes*2];
  663. int res_shapes;
  664. Set<RID> exclude;
  665. exclude.insert(get_rid());
  666. //recover first
  667. int recover_attempts=4;
  668. bool collided=false;
  669. uint32_t mask=0;
  670. if (collide_static)
  671. mask|=PhysicsDirectSpaceState::TYPE_MASK_STATIC_BODY;
  672. if (collide_kinematic)
  673. mask|=PhysicsDirectSpaceState::TYPE_MASK_KINEMATIC_BODY;
  674. if (collide_rigid)
  675. mask|=PhysicsDirectSpaceState::TYPE_MASK_RIGID_BODY;
  676. if (collide_character)
  677. mask|=PhysicsDirectSpaceState::TYPE_MASK_CHARACTER_BODY;
  678. //print_line("motion: "+p_motion+" margin: "+rtos(margin));
  679. //print_line("margin: "+rtos(margin));
  680. float m = margin;
  681. //m=0.001;
  682. do {
  683. //motion recover
  684. for(int i=0;i<get_shape_count();i++) {
  685. if (is_shape_set_as_trigger(i))
  686. continue;
  687. if (dss->collide_shape(get_shape(i)->get_rid(), get_global_transform() * get_shape_transform(i),m,sr,max_shapes,res_shapes,exclude,get_collision_layer(),mask)) {
  688. collided=true;
  689. }
  690. }
  691. if (!collided)
  692. break;
  693. //print_line("have to recover");
  694. Vector3 recover_motion;
  695. bool all_outside=true;
  696. for(int j=0;j<8;j++) {
  697. for(int i=0;i<res_shapes;i++) {
  698. Vector3 a = sr[i*2+0];
  699. Vector3 b = sr[i*2+1];
  700. //print_line(String()+a+" -> "+b);
  701. #if 0
  702. float d = a.distance_to(b);
  703. /*
  704. if (d<margin)
  705. continue;
  706. */
  707. recover_motion+=(b-a)*0.2;
  708. #else
  709. float dist = a.distance_to(b);
  710. if (dist>CMP_EPSILON) {
  711. Vector3 norm = (b-a).normalized();
  712. if (dist>margin*0.5)
  713. all_outside=false;
  714. float adv = norm.dot(recover_motion);
  715. //print_line(itos(i)+" dist: "+rtos(dist)+" adv: "+rtos(adv));
  716. recover_motion+=norm*MAX(dist-adv,0)*0.4;
  717. }
  718. #endif
  719. }
  720. }
  721. if (recover_motion==Vector3()) {
  722. collided=false;
  723. break;
  724. }
  725. //print_line("**** RECOVER: "+recover_motion);
  726. Transform gt = get_global_transform();
  727. gt.origin+=recover_motion;
  728. set_global_transform(gt);
  729. recover_attempts--;
  730. if (all_outside)
  731. break;
  732. } while (recover_attempts);
  733. //move second
  734. float safe = 1.0;
  735. float unsafe = 1.0;
  736. int best_shape=-1;
  737. PhysicsDirectSpaceState::ShapeRestInfo rest;
  738. //print_line("pos: "+get_global_transform().origin);
  739. //print_line("motion: "+p_motion);
  740. for(int i=0;i<get_shape_count();i++) {
  741. if (is_shape_set_as_trigger(i))
  742. continue;
  743. float lsafe,lunsafe;
  744. PhysicsDirectSpaceState::ShapeRestInfo lrest;
  745. bool valid = dss->cast_motion(get_shape(i)->get_rid(), get_global_transform() * get_shape_transform(i), p_motion,0, lsafe,lunsafe,exclude,get_collision_layer(),mask,&lrest);
  746. //print_line("shape: "+itos(i)+" travel:"+rtos(ltravel));
  747. if (!valid) {
  748. safe=0;
  749. unsafe=0;
  750. best_shape=i; //sadly it's the best
  751. //print_line("initial stuck");
  752. break;
  753. }
  754. if (lsafe==1.0) {
  755. //print_line("initial free");
  756. continue;
  757. }
  758. if (lsafe < safe) {
  759. //print_line("initial at "+rtos(lsafe));
  760. safe=lsafe;
  761. safe=MAX(0,lsafe-0.01);
  762. unsafe=lunsafe;
  763. best_shape=i;
  764. rest=lrest;
  765. }
  766. }
  767. //print_line("best shape: "+itos(best_shape)+" motion "+p_motion);
  768. if (safe>=1) {
  769. //not collided
  770. colliding=false;
  771. } else {
  772. colliding=true;
  773. if (true || (safe==0 && unsafe==0)) { //use it always because it's more precise than GJK
  774. //no advance, use rest info from collision
  775. Transform ugt = get_global_transform();
  776. ugt.origin+=p_motion*unsafe;
  777. PhysicsDirectSpaceState::ShapeRestInfo rest_info;
  778. bool c2 = dss->rest_info(get_shape(best_shape)->get_rid(), ugt*get_shape_transform(best_shape), m,&rest,exclude,get_collision_layer(),mask);
  779. if (!c2) {
  780. //should not happen, but floating point precision is so weird..
  781. colliding=false;
  782. }
  783. //print_line("Rest Travel: "+rest.normal);
  784. }
  785. if (colliding) {
  786. collision=rest.point;
  787. normal=rest.normal;
  788. collider=rest.collider_id;
  789. collider_vel=rest.linear_velocity;
  790. collider_shape=rest.shape;
  791. }
  792. }
  793. Vector3 motion=p_motion*safe;
  794. /*
  795. if (colliding)
  796. motion+=normal*0.001;
  797. */
  798. Transform gt = get_global_transform();
  799. gt.origin+=motion;
  800. set_global_transform(gt);
  801. return p_motion-motion;
  802. }
  803. Vector3 KinematicBody::move_to(const Vector3& p_position) {
  804. return move(p_position-get_global_transform().origin);
  805. }
  806. bool KinematicBody::can_teleport_to(const Vector3& p_position) {
  807. ERR_FAIL_COND_V(!is_inside_tree(),false);
  808. PhysicsDirectSpaceState *dss = PhysicsServer::get_singleton()->space_get_direct_state(get_world()->get_space());
  809. ERR_FAIL_COND_V(!dss,false);
  810. uint32_t mask=0;
  811. if (collide_static)
  812. mask|=PhysicsDirectSpaceState::TYPE_MASK_STATIC_BODY;
  813. if (collide_kinematic)
  814. mask|=PhysicsDirectSpaceState::TYPE_MASK_KINEMATIC_BODY;
  815. if (collide_rigid)
  816. mask|=PhysicsDirectSpaceState::TYPE_MASK_RIGID_BODY;
  817. if (collide_character)
  818. mask|=PhysicsDirectSpaceState::TYPE_MASK_CHARACTER_BODY;
  819. Transform xform=get_global_transform();
  820. xform.origin=p_position;
  821. Set<RID> exclude;
  822. exclude.insert(get_rid());
  823. for(int i=0;i<get_shape_count();i++) {
  824. if (is_shape_set_as_trigger(i))
  825. continue;
  826. bool col = dss->intersect_shape(get_shape(i)->get_rid(), xform * get_shape_transform(i),0,NULL,1,exclude,get_collision_layer(),mask);
  827. if (col)
  828. return false;
  829. }
  830. return true;
  831. }
  832. bool KinematicBody::is_colliding() const {
  833. ERR_FAIL_COND_V(!is_inside_tree(),false);
  834. return colliding;
  835. }
  836. Vector3 KinematicBody::get_collision_pos() const {
  837. ERR_FAIL_COND_V(!colliding,Vector3());
  838. return collision;
  839. }
  840. Vector3 KinematicBody::get_collision_normal() const {
  841. ERR_FAIL_COND_V(!colliding,Vector3());
  842. return normal;
  843. }
  844. Vector3 KinematicBody::get_collider_velocity() const {
  845. return collider_vel;
  846. }
  847. ObjectID KinematicBody::get_collider() const {
  848. ERR_FAIL_COND_V(!colliding,0);
  849. return collider;
  850. }
  851. int KinematicBody::get_collider_shape() const {
  852. ERR_FAIL_COND_V(!colliding,-1);
  853. return collider_shape;
  854. }
  855. void KinematicBody::set_collide_with_static_bodies(bool p_enable) {
  856. collide_static=p_enable;
  857. }
  858. bool KinematicBody::can_collide_with_static_bodies() const {
  859. return collide_static;
  860. }
  861. void KinematicBody::set_collide_with_rigid_bodies(bool p_enable) {
  862. collide_rigid=p_enable;
  863. }
  864. bool KinematicBody::can_collide_with_rigid_bodies() const {
  865. return collide_rigid;
  866. }
  867. void KinematicBody::set_collide_with_kinematic_bodies(bool p_enable) {
  868. collide_kinematic=p_enable;
  869. }
  870. bool KinematicBody::can_collide_with_kinematic_bodies() const {
  871. return collide_kinematic;
  872. }
  873. void KinematicBody::set_collide_with_character_bodies(bool p_enable) {
  874. collide_character=p_enable;
  875. }
  876. bool KinematicBody::can_collide_with_character_bodies() const {
  877. return collide_character;
  878. }
  879. void KinematicBody::set_collision_margin(float p_margin) {
  880. margin=p_margin;
  881. }
  882. float KinematicBody::get_collision_margin() const{
  883. return margin;
  884. }
  885. void KinematicBody::_bind_methods() {
  886. ClassDB::bind_method(_MD("move","rel_vec"),&KinematicBody::move);
  887. ClassDB::bind_method(_MD("move_to","position"),&KinematicBody::move_to);
  888. ClassDB::bind_method(_MD("can_teleport_to","position"),&KinematicBody::can_teleport_to);
  889. ClassDB::bind_method(_MD("is_colliding"),&KinematicBody::is_colliding);
  890. ClassDB::bind_method(_MD("get_collision_pos"),&KinematicBody::get_collision_pos);
  891. ClassDB::bind_method(_MD("get_collision_normal"),&KinematicBody::get_collision_normal);
  892. ClassDB::bind_method(_MD("get_collider_velocity"),&KinematicBody::get_collider_velocity);
  893. ClassDB::bind_method(_MD("get_collider:Variant"),&KinematicBody::_get_collider);
  894. ClassDB::bind_method(_MD("get_collider_shape"),&KinematicBody::get_collider_shape);
  895. ClassDB::bind_method(_MD("set_collide_with_static_bodies","enable"),&KinematicBody::set_collide_with_static_bodies);
  896. ClassDB::bind_method(_MD("can_collide_with_static_bodies"),&KinematicBody::can_collide_with_static_bodies);
  897. ClassDB::bind_method(_MD("set_collide_with_kinematic_bodies","enable"),&KinematicBody::set_collide_with_kinematic_bodies);
  898. ClassDB::bind_method(_MD("can_collide_with_kinematic_bodies"),&KinematicBody::can_collide_with_kinematic_bodies);
  899. ClassDB::bind_method(_MD("set_collide_with_rigid_bodies","enable"),&KinematicBody::set_collide_with_rigid_bodies);
  900. ClassDB::bind_method(_MD("can_collide_with_rigid_bodies"),&KinematicBody::can_collide_with_rigid_bodies);
  901. ClassDB::bind_method(_MD("set_collide_with_character_bodies","enable"),&KinematicBody::set_collide_with_character_bodies);
  902. ClassDB::bind_method(_MD("can_collide_with_character_bodies"),&KinematicBody::can_collide_with_character_bodies);
  903. ClassDB::bind_method(_MD("set_collision_margin","pixels"),&KinematicBody::set_collision_margin);
  904. ClassDB::bind_method(_MD("get_collision_margin","pixels"),&KinematicBody::get_collision_margin);
  905. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"collide_with/static"),"set_collide_with_static_bodies","can_collide_with_static_bodies");
  906. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"collide_with/kinematic"),"set_collide_with_kinematic_bodies","can_collide_with_kinematic_bodies");
  907. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"collide_with/rigid"),"set_collide_with_rigid_bodies","can_collide_with_rigid_bodies");
  908. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"collide_with/character"),"set_collide_with_character_bodies","can_collide_with_character_bodies");
  909. ADD_PROPERTY( PropertyInfo(Variant::REAL,"collision/margin",PROPERTY_HINT_RANGE,"0.001,256,0.001"),"set_collision_margin","get_collision_margin");
  910. }
  911. KinematicBody::KinematicBody() : PhysicsBody(PhysicsServer::BODY_MODE_KINEMATIC){
  912. collide_static=true;
  913. collide_rigid=true;
  914. collide_kinematic=true;
  915. collide_character=true;
  916. colliding=false;
  917. collider=0;
  918. margin=0.001;
  919. collider_shape=0;
  920. }
  921. KinematicBody::~KinematicBody() {
  922. }