physics_body.cpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  1. /*************************************************************************/
  2. /* physics_body.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "physics_body.h"
  31. #include "engine.h"
  32. #include "method_bind_ext.gen.inc"
  33. #include "scene/scene_string_names.h"
  34. void PhysicsBody::_notification(int p_what) {
  35. /*
  36. switch(p_what) {
  37. case NOTIFICATION_TRANSFORM_CHANGED: {
  38. PhysicsServer::get_singleton()->body_set_state(get_rid(),PhysicsServer::BODY_STATE_TRANSFORM,get_global_transform());
  39. } break;
  40. }
  41. */
  42. }
  43. Vector3 PhysicsBody::get_linear_velocity() const {
  44. return Vector3();
  45. }
  46. Vector3 PhysicsBody::get_angular_velocity() const {
  47. return Vector3();
  48. }
  49. float PhysicsBody::get_inverse_mass() const {
  50. return 0;
  51. }
  52. void PhysicsBody::set_collision_layer(uint32_t p_layer) {
  53. collision_layer = p_layer;
  54. PhysicsServer::get_singleton()->body_set_collision_layer(get_rid(), p_layer);
  55. }
  56. uint32_t PhysicsBody::get_collision_layer() const {
  57. return collision_layer;
  58. }
  59. void PhysicsBody::set_collision_mask(uint32_t p_mask) {
  60. collision_mask = p_mask;
  61. PhysicsServer::get_singleton()->body_set_collision_mask(get_rid(), p_mask);
  62. }
  63. uint32_t PhysicsBody::get_collision_mask() const {
  64. return collision_mask;
  65. }
  66. void PhysicsBody::set_collision_mask_bit(int p_bit, bool p_value) {
  67. uint32_t mask = get_collision_mask();
  68. if (p_value)
  69. mask |= 1 << p_bit;
  70. else
  71. mask &= ~(1 << p_bit);
  72. set_collision_mask(mask);
  73. }
  74. bool PhysicsBody::get_collision_mask_bit(int p_bit) const {
  75. return get_collision_mask() & (1 << p_bit);
  76. }
  77. void PhysicsBody::set_collision_layer_bit(int p_bit, bool p_value) {
  78. uint32_t mask = get_collision_layer();
  79. if (p_value)
  80. mask |= 1 << p_bit;
  81. else
  82. mask &= ~(1 << p_bit);
  83. set_collision_layer(mask);
  84. }
  85. bool PhysicsBody::get_collision_layer_bit(int p_bit) const {
  86. return get_collision_layer() & (1 << p_bit);
  87. }
  88. void PhysicsBody::add_collision_exception_with(Node *p_node) {
  89. ERR_FAIL_NULL(p_node);
  90. PhysicsBody *physics_body = Object::cast_to<PhysicsBody>(p_node);
  91. if (!physics_body) {
  92. ERR_EXPLAIN("Collision exception only works between two objects of PhysicsBody type");
  93. }
  94. ERR_FAIL_COND(!physics_body);
  95. PhysicsServer::get_singleton()->body_add_collision_exception(get_rid(), physics_body->get_rid());
  96. }
  97. void PhysicsBody::remove_collision_exception_with(Node *p_node) {
  98. ERR_FAIL_NULL(p_node);
  99. PhysicsBody *physics_body = Object::cast_to<PhysicsBody>(p_node);
  100. if (!physics_body) {
  101. ERR_EXPLAIN("Collision exception only works between two objects of PhysicsBody type");
  102. }
  103. ERR_FAIL_COND(!physics_body);
  104. PhysicsServer::get_singleton()->body_remove_collision_exception(get_rid(), physics_body->get_rid());
  105. }
  106. void PhysicsBody::_set_layers(uint32_t p_mask) {
  107. set_collision_layer(p_mask);
  108. set_collision_mask(p_mask);
  109. }
  110. uint32_t PhysicsBody::_get_layers() const {
  111. return get_collision_layer();
  112. }
  113. void PhysicsBody::_bind_methods() {
  114. ClassDB::bind_method(D_METHOD("set_collision_layer", "layer"), &PhysicsBody::set_collision_layer);
  115. ClassDB::bind_method(D_METHOD("get_collision_layer"), &PhysicsBody::get_collision_layer);
  116. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &PhysicsBody::set_collision_mask);
  117. ClassDB::bind_method(D_METHOD("get_collision_mask"), &PhysicsBody::get_collision_mask);
  118. ClassDB::bind_method(D_METHOD("set_collision_mask_bit", "bit", "value"), &PhysicsBody::set_collision_mask_bit);
  119. ClassDB::bind_method(D_METHOD("get_collision_mask_bit", "bit"), &PhysicsBody::get_collision_mask_bit);
  120. ClassDB::bind_method(D_METHOD("set_collision_layer_bit", "bit", "value"), &PhysicsBody::set_collision_layer_bit);
  121. ClassDB::bind_method(D_METHOD("get_collision_layer_bit", "bit"), &PhysicsBody::get_collision_layer_bit);
  122. ClassDB::bind_method(D_METHOD("_set_layers", "mask"), &PhysicsBody::_set_layers);
  123. ClassDB::bind_method(D_METHOD("_get_layers"), &PhysicsBody::_get_layers);
  124. ADD_GROUP("Collision", "collision_");
  125. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_layer", "get_collision_layer");
  126. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  127. }
  128. PhysicsBody::PhysicsBody(PhysicsServer::BodyMode p_mode)
  129. : CollisionObject(PhysicsServer::get_singleton()->body_create(p_mode), false) {
  130. collision_layer = 1;
  131. collision_mask = 1;
  132. }
  133. void StaticBody::set_friction(real_t p_friction) {
  134. ERR_FAIL_COND(p_friction < 0 || p_friction > 1);
  135. friction = p_friction;
  136. PhysicsServer::get_singleton()->body_set_param(get_rid(), PhysicsServer::BODY_PARAM_FRICTION, friction);
  137. }
  138. real_t StaticBody::get_friction() const {
  139. return friction;
  140. }
  141. void StaticBody::set_bounce(real_t p_bounce) {
  142. ERR_FAIL_COND(p_bounce < 0 || p_bounce > 1);
  143. bounce = p_bounce;
  144. PhysicsServer::get_singleton()->body_set_param(get_rid(), PhysicsServer::BODY_PARAM_BOUNCE, bounce);
  145. }
  146. real_t StaticBody::get_bounce() const {
  147. return bounce;
  148. }
  149. void StaticBody::set_constant_linear_velocity(const Vector3 &p_vel) {
  150. constant_linear_velocity = p_vel;
  151. PhysicsServer::get_singleton()->body_set_state(get_rid(), PhysicsServer::BODY_STATE_LINEAR_VELOCITY, constant_linear_velocity);
  152. }
  153. void StaticBody::set_constant_angular_velocity(const Vector3 &p_vel) {
  154. constant_angular_velocity = p_vel;
  155. PhysicsServer::get_singleton()->body_set_state(get_rid(), PhysicsServer::BODY_STATE_ANGULAR_VELOCITY, constant_angular_velocity);
  156. }
  157. Vector3 StaticBody::get_constant_linear_velocity() const {
  158. return constant_linear_velocity;
  159. }
  160. Vector3 StaticBody::get_constant_angular_velocity() const {
  161. return constant_angular_velocity;
  162. }
  163. void StaticBody::_bind_methods() {
  164. ClassDB::bind_method(D_METHOD("set_constant_linear_velocity", "vel"), &StaticBody::set_constant_linear_velocity);
  165. ClassDB::bind_method(D_METHOD("set_constant_angular_velocity", "vel"), &StaticBody::set_constant_angular_velocity);
  166. ClassDB::bind_method(D_METHOD("get_constant_linear_velocity"), &StaticBody::get_constant_linear_velocity);
  167. ClassDB::bind_method(D_METHOD("get_constant_angular_velocity"), &StaticBody::get_constant_angular_velocity);
  168. ClassDB::bind_method(D_METHOD("set_friction", "friction"), &StaticBody::set_friction);
  169. ClassDB::bind_method(D_METHOD("get_friction"), &StaticBody::get_friction);
  170. ClassDB::bind_method(D_METHOD("set_bounce", "bounce"), &StaticBody::set_bounce);
  171. ClassDB::bind_method(D_METHOD("get_bounce"), &StaticBody::get_bounce);
  172. ClassDB::bind_method(D_METHOD("add_collision_exception_with", "body"), &PhysicsBody::add_collision_exception_with);
  173. ClassDB::bind_method(D_METHOD("remove_collision_exception_with", "body"), &PhysicsBody::remove_collision_exception_with);
  174. ADD_PROPERTY(PropertyInfo(Variant::REAL, "friction", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_friction", "get_friction");
  175. ADD_PROPERTY(PropertyInfo(Variant::REAL, "bounce", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_bounce", "get_bounce");
  176. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "constant_linear_velocity"), "set_constant_linear_velocity", "get_constant_linear_velocity");
  177. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "constant_angular_velocity"), "set_constant_angular_velocity", "get_constant_angular_velocity");
  178. }
  179. StaticBody::StaticBody()
  180. : PhysicsBody(PhysicsServer::BODY_MODE_STATIC) {
  181. bounce = 0;
  182. friction = 1;
  183. }
  184. StaticBody::~StaticBody() {
  185. }
  186. void RigidBody::_body_enter_tree(ObjectID p_id) {
  187. Object *obj = ObjectDB::get_instance(p_id);
  188. Node *node = Object::cast_to<Node>(obj);
  189. ERR_FAIL_COND(!node);
  190. Map<ObjectID, BodyState>::Element *E = contact_monitor->body_map.find(p_id);
  191. ERR_FAIL_COND(!E);
  192. ERR_FAIL_COND(E->get().in_tree);
  193. E->get().in_tree = true;
  194. contact_monitor->locked = true;
  195. emit_signal(SceneStringNames::get_singleton()->body_entered, node);
  196. for (int i = 0; i < E->get().shapes.size(); i++) {
  197. emit_signal(SceneStringNames::get_singleton()->body_shape_entered, p_id, node, E->get().shapes[i].body_shape, E->get().shapes[i].local_shape);
  198. }
  199. contact_monitor->locked = false;
  200. }
  201. void RigidBody::_body_exit_tree(ObjectID p_id) {
  202. Object *obj = ObjectDB::get_instance(p_id);
  203. Node *node = Object::cast_to<Node>(obj);
  204. ERR_FAIL_COND(!node);
  205. Map<ObjectID, BodyState>::Element *E = contact_monitor->body_map.find(p_id);
  206. ERR_FAIL_COND(!E);
  207. ERR_FAIL_COND(!E->get().in_tree);
  208. E->get().in_tree = false;
  209. contact_monitor->locked = true;
  210. emit_signal(SceneStringNames::get_singleton()->body_exited, node);
  211. for (int i = 0; i < E->get().shapes.size(); i++) {
  212. emit_signal(SceneStringNames::get_singleton()->body_shape_exited, p_id, node, E->get().shapes[i].body_shape, E->get().shapes[i].local_shape);
  213. }
  214. contact_monitor->locked = false;
  215. }
  216. void RigidBody::_body_inout(int p_status, ObjectID p_instance, int p_body_shape, int p_local_shape) {
  217. bool body_in = p_status == 1;
  218. ObjectID objid = p_instance;
  219. Object *obj = ObjectDB::get_instance(objid);
  220. Node *node = Object::cast_to<Node>(obj);
  221. Map<ObjectID, BodyState>::Element *E = contact_monitor->body_map.find(objid);
  222. ERR_FAIL_COND(!body_in && !E);
  223. if (body_in) {
  224. if (!E) {
  225. E = contact_monitor->body_map.insert(objid, BodyState());
  226. //E->get().rc=0;
  227. E->get().in_tree = node && node->is_inside_tree();
  228. if (node) {
  229. node->connect(SceneStringNames::get_singleton()->tree_entered, this, SceneStringNames::get_singleton()->_body_enter_tree, make_binds(objid));
  230. node->connect(SceneStringNames::get_singleton()->tree_exited, this, SceneStringNames::get_singleton()->_body_exit_tree, make_binds(objid));
  231. if (E->get().in_tree) {
  232. emit_signal(SceneStringNames::get_singleton()->body_entered, node);
  233. }
  234. }
  235. }
  236. //E->get().rc++;
  237. if (node)
  238. E->get().shapes.insert(ShapePair(p_body_shape, p_local_shape));
  239. if (E->get().in_tree) {
  240. emit_signal(SceneStringNames::get_singleton()->body_shape_entered, objid, node, p_body_shape, p_local_shape);
  241. }
  242. } else {
  243. //E->get().rc--;
  244. if (node)
  245. E->get().shapes.erase(ShapePair(p_body_shape, p_local_shape));
  246. bool in_tree = E->get().in_tree;
  247. if (E->get().shapes.empty()) {
  248. if (node) {
  249. node->disconnect(SceneStringNames::get_singleton()->tree_entered, this, SceneStringNames::get_singleton()->_body_enter_tree);
  250. node->disconnect(SceneStringNames::get_singleton()->tree_exited, this, SceneStringNames::get_singleton()->_body_exit_tree);
  251. if (in_tree)
  252. emit_signal(SceneStringNames::get_singleton()->body_exited, obj);
  253. }
  254. contact_monitor->body_map.erase(E);
  255. }
  256. if (node && in_tree) {
  257. emit_signal(SceneStringNames::get_singleton()->body_shape_exited, objid, obj, p_body_shape, p_local_shape);
  258. }
  259. }
  260. }
  261. struct _RigidBodyInOut {
  262. ObjectID id;
  263. int shape;
  264. int local_shape;
  265. };
  266. void RigidBody::_direct_state_changed(Object *p_state) {
  267. //eh.. fuck
  268. #ifdef DEBUG_ENABLED
  269. state = Object::cast_to<PhysicsDirectBodyState>(p_state);
  270. #else
  271. state = (PhysicsDirectBodyState *)p_state; //trust it
  272. #endif
  273. set_ignore_transform_notification(true);
  274. set_global_transform(state->get_transform());
  275. linear_velocity = state->get_linear_velocity();
  276. angular_velocity = state->get_angular_velocity();
  277. if (sleeping != state->is_sleeping()) {
  278. sleeping = state->is_sleeping();
  279. emit_signal(SceneStringNames::get_singleton()->sleeping_state_changed);
  280. }
  281. if (get_script_instance())
  282. get_script_instance()->call("_integrate_forces", state);
  283. set_ignore_transform_notification(false);
  284. if (contact_monitor) {
  285. contact_monitor->locked = true;
  286. //untag all
  287. int rc = 0;
  288. for (Map<ObjectID, BodyState>::Element *E = contact_monitor->body_map.front(); E; E = E->next()) {
  289. for (int i = 0; i < E->get().shapes.size(); i++) {
  290. E->get().shapes[i].tagged = false;
  291. rc++;
  292. }
  293. }
  294. _RigidBodyInOut *toadd = (_RigidBodyInOut *)alloca(state->get_contact_count() * sizeof(_RigidBodyInOut));
  295. int toadd_count = 0; //state->get_contact_count();
  296. RigidBody_RemoveAction *toremove = (RigidBody_RemoveAction *)alloca(rc * sizeof(RigidBody_RemoveAction));
  297. int toremove_count = 0;
  298. //put the ones to add
  299. for (int i = 0; i < state->get_contact_count(); i++) {
  300. ObjectID obj = state->get_contact_collider_id(i);
  301. int local_shape = state->get_contact_local_shape(i);
  302. int shape = state->get_contact_collider_shape(i);
  303. //bool found=false;
  304. Map<ObjectID, BodyState>::Element *E = contact_monitor->body_map.find(obj);
  305. if (!E) {
  306. toadd[toadd_count].local_shape = local_shape;
  307. toadd[toadd_count].id = obj;
  308. toadd[toadd_count].shape = shape;
  309. toadd_count++;
  310. continue;
  311. }
  312. ShapePair sp(shape, local_shape);
  313. int idx = E->get().shapes.find(sp);
  314. if (idx == -1) {
  315. toadd[toadd_count].local_shape = local_shape;
  316. toadd[toadd_count].id = obj;
  317. toadd[toadd_count].shape = shape;
  318. toadd_count++;
  319. continue;
  320. }
  321. E->get().shapes[idx].tagged = true;
  322. }
  323. //put the ones to remove
  324. for (Map<ObjectID, BodyState>::Element *E = contact_monitor->body_map.front(); E; E = E->next()) {
  325. for (int i = 0; i < E->get().shapes.size(); i++) {
  326. if (!E->get().shapes[i].tagged) {
  327. toremove[toremove_count].body_id = E->key();
  328. toremove[toremove_count].pair = E->get().shapes[i];
  329. toremove_count++;
  330. }
  331. }
  332. }
  333. //process remotions
  334. for (int i = 0; i < toremove_count; i++) {
  335. _body_inout(0, toremove[i].body_id, toremove[i].pair.body_shape, toremove[i].pair.local_shape);
  336. }
  337. //process aditions
  338. for (int i = 0; i < toadd_count; i++) {
  339. _body_inout(1, toadd[i].id, toadd[i].shape, toadd[i].local_shape);
  340. }
  341. contact_monitor->locked = false;
  342. }
  343. state = NULL;
  344. }
  345. void RigidBody::_notification(int p_what) {
  346. #ifdef TOOLS_ENABLED
  347. if (p_what == NOTIFICATION_ENTER_TREE) {
  348. if (Engine::get_singleton()->is_editor_hint()) {
  349. set_notify_local_transform(true); //used for warnings and only in editor
  350. }
  351. }
  352. if (p_what == NOTIFICATION_LOCAL_TRANSFORM_CHANGED) {
  353. if (Engine::get_singleton()->is_editor_hint()) {
  354. update_configuration_warning();
  355. }
  356. }
  357. #endif
  358. }
  359. void RigidBody::set_mode(Mode p_mode) {
  360. mode = p_mode;
  361. switch (p_mode) {
  362. case MODE_RIGID: {
  363. PhysicsServer::get_singleton()->body_set_mode(get_rid(), PhysicsServer::BODY_MODE_RIGID);
  364. } break;
  365. case MODE_STATIC: {
  366. PhysicsServer::get_singleton()->body_set_mode(get_rid(), PhysicsServer::BODY_MODE_STATIC);
  367. } break;
  368. case MODE_CHARACTER: {
  369. PhysicsServer::get_singleton()->body_set_mode(get_rid(), PhysicsServer::BODY_MODE_CHARACTER);
  370. } break;
  371. case MODE_KINEMATIC: {
  372. PhysicsServer::get_singleton()->body_set_mode(get_rid(), PhysicsServer::BODY_MODE_KINEMATIC);
  373. } break;
  374. }
  375. }
  376. RigidBody::Mode RigidBody::get_mode() const {
  377. return mode;
  378. }
  379. void RigidBody::set_mass(real_t p_mass) {
  380. ERR_FAIL_COND(p_mass <= 0);
  381. mass = p_mass;
  382. _change_notify("mass");
  383. _change_notify("weight");
  384. PhysicsServer::get_singleton()->body_set_param(get_rid(), PhysicsServer::BODY_PARAM_MASS, mass);
  385. }
  386. real_t RigidBody::get_mass() const {
  387. return mass;
  388. }
  389. void RigidBody::set_weight(real_t p_weight) {
  390. set_mass(p_weight / real_t(GLOBAL_DEF("physics/3d/default_gravity", 9.8)));
  391. }
  392. real_t RigidBody::get_weight() const {
  393. return mass * real_t(GLOBAL_DEF("physics/3d/default_gravity", 9.8));
  394. }
  395. void RigidBody::set_friction(real_t p_friction) {
  396. ERR_FAIL_COND(p_friction < 0 || p_friction > 1);
  397. friction = p_friction;
  398. PhysicsServer::get_singleton()->body_set_param(get_rid(), PhysicsServer::BODY_PARAM_FRICTION, friction);
  399. }
  400. real_t RigidBody::get_friction() const {
  401. return friction;
  402. }
  403. void RigidBody::set_bounce(real_t p_bounce) {
  404. ERR_FAIL_COND(p_bounce < 0 || p_bounce > 1);
  405. bounce = p_bounce;
  406. PhysicsServer::get_singleton()->body_set_param(get_rid(), PhysicsServer::BODY_PARAM_BOUNCE, bounce);
  407. }
  408. real_t RigidBody::get_bounce() const {
  409. return bounce;
  410. }
  411. void RigidBody::set_gravity_scale(real_t p_gravity_scale) {
  412. gravity_scale = p_gravity_scale;
  413. PhysicsServer::get_singleton()->body_set_param(get_rid(), PhysicsServer::BODY_PARAM_GRAVITY_SCALE, gravity_scale);
  414. }
  415. real_t RigidBody::get_gravity_scale() const {
  416. return gravity_scale;
  417. }
  418. void RigidBody::set_linear_damp(real_t p_linear_damp) {
  419. ERR_FAIL_COND(p_linear_damp < -1);
  420. linear_damp = p_linear_damp;
  421. PhysicsServer::get_singleton()->body_set_param(get_rid(), PhysicsServer::BODY_PARAM_LINEAR_DAMP, linear_damp);
  422. }
  423. real_t RigidBody::get_linear_damp() const {
  424. return linear_damp;
  425. }
  426. void RigidBody::set_angular_damp(real_t p_angular_damp) {
  427. ERR_FAIL_COND(p_angular_damp < -1);
  428. angular_damp = p_angular_damp;
  429. PhysicsServer::get_singleton()->body_set_param(get_rid(), PhysicsServer::BODY_PARAM_ANGULAR_DAMP, angular_damp);
  430. }
  431. real_t RigidBody::get_angular_damp() const {
  432. return angular_damp;
  433. }
  434. void RigidBody::set_axis_velocity(const Vector3 &p_axis) {
  435. Vector3 v = state ? state->get_linear_velocity() : linear_velocity;
  436. Vector3 axis = p_axis.normalized();
  437. v -= axis * axis.dot(v);
  438. v += p_axis;
  439. if (state) {
  440. set_linear_velocity(v);
  441. } else {
  442. PhysicsServer::get_singleton()->body_set_axis_velocity(get_rid(), p_axis);
  443. linear_velocity = v;
  444. }
  445. }
  446. void RigidBody::set_linear_velocity(const Vector3 &p_velocity) {
  447. linear_velocity = p_velocity;
  448. if (state)
  449. state->set_linear_velocity(linear_velocity);
  450. else
  451. PhysicsServer::get_singleton()->body_set_state(get_rid(), PhysicsServer::BODY_STATE_LINEAR_VELOCITY, linear_velocity);
  452. }
  453. Vector3 RigidBody::get_linear_velocity() const {
  454. return linear_velocity;
  455. }
  456. void RigidBody::set_angular_velocity(const Vector3 &p_velocity) {
  457. angular_velocity = p_velocity;
  458. if (state)
  459. state->set_angular_velocity(angular_velocity);
  460. else
  461. PhysicsServer::get_singleton()->body_set_state(get_rid(), PhysicsServer::BODY_STATE_ANGULAR_VELOCITY, angular_velocity);
  462. }
  463. Vector3 RigidBody::get_angular_velocity() const {
  464. return angular_velocity;
  465. }
  466. void RigidBody::set_use_custom_integrator(bool p_enable) {
  467. if (custom_integrator == p_enable)
  468. return;
  469. custom_integrator = p_enable;
  470. PhysicsServer::get_singleton()->body_set_omit_force_integration(get_rid(), p_enable);
  471. }
  472. bool RigidBody::is_using_custom_integrator() {
  473. return custom_integrator;
  474. }
  475. void RigidBody::set_sleeping(bool p_sleeping) {
  476. sleeping = p_sleeping;
  477. PhysicsServer::get_singleton()->body_set_state(get_rid(), PhysicsServer::BODY_STATE_SLEEPING, sleeping);
  478. }
  479. void RigidBody::set_can_sleep(bool p_active) {
  480. can_sleep = p_active;
  481. PhysicsServer::get_singleton()->body_set_state(get_rid(), PhysicsServer::BODY_STATE_CAN_SLEEP, p_active);
  482. }
  483. bool RigidBody::is_able_to_sleep() const {
  484. return can_sleep;
  485. }
  486. bool RigidBody::is_sleeping() const {
  487. return sleeping;
  488. }
  489. void RigidBody::set_max_contacts_reported(int p_amount) {
  490. max_contacts_reported = p_amount;
  491. PhysicsServer::get_singleton()->body_set_max_contacts_reported(get_rid(), p_amount);
  492. }
  493. int RigidBody::get_max_contacts_reported() const {
  494. return max_contacts_reported;
  495. }
  496. void RigidBody::apply_impulse(const Vector3 &p_pos, const Vector3 &p_impulse) {
  497. PhysicsServer::get_singleton()->body_apply_impulse(get_rid(), p_pos, p_impulse);
  498. }
  499. void RigidBody::set_use_continuous_collision_detection(bool p_enable) {
  500. ccd = p_enable;
  501. PhysicsServer::get_singleton()->body_set_enable_continuous_collision_detection(get_rid(), p_enable);
  502. }
  503. bool RigidBody::is_using_continuous_collision_detection() const {
  504. return ccd;
  505. }
  506. void RigidBody::set_contact_monitor(bool p_enabled) {
  507. if (p_enabled == is_contact_monitor_enabled())
  508. return;
  509. if (!p_enabled) {
  510. if (contact_monitor->locked) {
  511. ERR_EXPLAIN("Can't disable contact monitoring during in/out callback. Use call_deferred(\"set_contact_monitor\",false) instead");
  512. }
  513. ERR_FAIL_COND(contact_monitor->locked);
  514. for (Map<ObjectID, BodyState>::Element *E = contact_monitor->body_map.front(); E; E = E->next()) {
  515. //clean up mess
  516. }
  517. memdelete(contact_monitor);
  518. contact_monitor = NULL;
  519. } else {
  520. contact_monitor = memnew(ContactMonitor);
  521. contact_monitor->locked = false;
  522. }
  523. }
  524. bool RigidBody::is_contact_monitor_enabled() const {
  525. return contact_monitor != NULL;
  526. }
  527. void RigidBody::set_axis_lock(AxisLock p_lock) {
  528. axis_lock = p_lock;
  529. PhysicsServer::get_singleton()->body_set_axis_lock(get_rid(), PhysicsServer::BodyAxisLock(axis_lock));
  530. }
  531. RigidBody::AxisLock RigidBody::get_axis_lock() const {
  532. return axis_lock;
  533. }
  534. Array RigidBody::get_colliding_bodies() const {
  535. ERR_FAIL_COND_V(!contact_monitor, Array());
  536. Array ret;
  537. ret.resize(contact_monitor->body_map.size());
  538. int idx = 0;
  539. for (const Map<ObjectID, BodyState>::Element *E = contact_monitor->body_map.front(); E; E = E->next()) {
  540. Object *obj = ObjectDB::get_instance(E->key());
  541. if (!obj) {
  542. ret.resize(ret.size() - 1); //ops
  543. } else {
  544. ret[idx++] = obj;
  545. }
  546. }
  547. return ret;
  548. }
  549. String RigidBody::get_configuration_warning() const {
  550. Transform t = get_transform();
  551. String warning = CollisionObject::get_configuration_warning();
  552. if ((get_mode() == MODE_RIGID || get_mode() == MODE_CHARACTER) && (ABS(t.basis.get_axis(0).length() - 1.0) > 0.05 || ABS(t.basis.get_axis(1).length() - 1.0) > 0.05 || ABS(t.basis.get_axis(0).length() - 1.0) > 0.05)) {
  553. if (warning != String()) {
  554. warning += "\n";
  555. }
  556. warning += TTR("Size changes to RigidBody (in character or rigid modes) will be overriden by the physics engine when running.\nChange the size in children collision shapes instead.");
  557. }
  558. return warning;
  559. }
  560. void RigidBody::_bind_methods() {
  561. ClassDB::bind_method(D_METHOD("set_mode", "mode"), &RigidBody::set_mode);
  562. ClassDB::bind_method(D_METHOD("get_mode"), &RigidBody::get_mode);
  563. ClassDB::bind_method(D_METHOD("set_mass", "mass"), &RigidBody::set_mass);
  564. ClassDB::bind_method(D_METHOD("get_mass"), &RigidBody::get_mass);
  565. ClassDB::bind_method(D_METHOD("set_weight", "weight"), &RigidBody::set_weight);
  566. ClassDB::bind_method(D_METHOD("get_weight"), &RigidBody::get_weight);
  567. ClassDB::bind_method(D_METHOD("set_friction", "friction"), &RigidBody::set_friction);
  568. ClassDB::bind_method(D_METHOD("get_friction"), &RigidBody::get_friction);
  569. ClassDB::bind_method(D_METHOD("set_bounce", "bounce"), &RigidBody::set_bounce);
  570. ClassDB::bind_method(D_METHOD("get_bounce"), &RigidBody::get_bounce);
  571. ClassDB::bind_method(D_METHOD("set_linear_velocity", "linear_velocity"), &RigidBody::set_linear_velocity);
  572. ClassDB::bind_method(D_METHOD("get_linear_velocity"), &RigidBody::get_linear_velocity);
  573. ClassDB::bind_method(D_METHOD("set_angular_velocity", "angular_velocity"), &RigidBody::set_angular_velocity);
  574. ClassDB::bind_method(D_METHOD("get_angular_velocity"), &RigidBody::get_angular_velocity);
  575. ClassDB::bind_method(D_METHOD("set_gravity_scale", "gravity_scale"), &RigidBody::set_gravity_scale);
  576. ClassDB::bind_method(D_METHOD("get_gravity_scale"), &RigidBody::get_gravity_scale);
  577. ClassDB::bind_method(D_METHOD("set_linear_damp", "linear_damp"), &RigidBody::set_linear_damp);
  578. ClassDB::bind_method(D_METHOD("get_linear_damp"), &RigidBody::get_linear_damp);
  579. ClassDB::bind_method(D_METHOD("set_angular_damp", "angular_damp"), &RigidBody::set_angular_damp);
  580. ClassDB::bind_method(D_METHOD("get_angular_damp"), &RigidBody::get_angular_damp);
  581. ClassDB::bind_method(D_METHOD("set_max_contacts_reported", "amount"), &RigidBody::set_max_contacts_reported);
  582. ClassDB::bind_method(D_METHOD("get_max_contacts_reported"), &RigidBody::get_max_contacts_reported);
  583. ClassDB::bind_method(D_METHOD("set_use_custom_integrator", "enable"), &RigidBody::set_use_custom_integrator);
  584. ClassDB::bind_method(D_METHOD("is_using_custom_integrator"), &RigidBody::is_using_custom_integrator);
  585. ClassDB::bind_method(D_METHOD("set_contact_monitor", "enabled"), &RigidBody::set_contact_monitor);
  586. ClassDB::bind_method(D_METHOD("is_contact_monitor_enabled"), &RigidBody::is_contact_monitor_enabled);
  587. ClassDB::bind_method(D_METHOD("set_use_continuous_collision_detection", "enable"), &RigidBody::set_use_continuous_collision_detection);
  588. ClassDB::bind_method(D_METHOD("is_using_continuous_collision_detection"), &RigidBody::is_using_continuous_collision_detection);
  589. ClassDB::bind_method(D_METHOD("set_axis_velocity", "axis_velocity"), &RigidBody::set_axis_velocity);
  590. ClassDB::bind_method(D_METHOD("apply_impulse", "position", "impulse"), &RigidBody::apply_impulse);
  591. ClassDB::bind_method(D_METHOD("set_sleeping", "sleeping"), &RigidBody::set_sleeping);
  592. ClassDB::bind_method(D_METHOD("is_sleeping"), &RigidBody::is_sleeping);
  593. ClassDB::bind_method(D_METHOD("set_can_sleep", "able_to_sleep"), &RigidBody::set_can_sleep);
  594. ClassDB::bind_method(D_METHOD("is_able_to_sleep"), &RigidBody::is_able_to_sleep);
  595. ClassDB::bind_method(D_METHOD("_direct_state_changed"), &RigidBody::_direct_state_changed);
  596. ClassDB::bind_method(D_METHOD("_body_enter_tree"), &RigidBody::_body_enter_tree);
  597. ClassDB::bind_method(D_METHOD("_body_exit_tree"), &RigidBody::_body_exit_tree);
  598. ClassDB::bind_method(D_METHOD("set_axis_lock", "axis_lock"), &RigidBody::set_axis_lock);
  599. ClassDB::bind_method(D_METHOD("get_axis_lock"), &RigidBody::get_axis_lock);
  600. ClassDB::bind_method(D_METHOD("get_colliding_bodies"), &RigidBody::get_colliding_bodies);
  601. BIND_VMETHOD(MethodInfo("_integrate_forces", PropertyInfo(Variant::OBJECT, "state", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsDirectBodyState")));
  602. ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Rigid,Static,Character,Kinematic"), "set_mode", "get_mode");
  603. ADD_PROPERTY(PropertyInfo(Variant::REAL, "mass", PROPERTY_HINT_EXP_RANGE, "0.01,65535,0.01"), "set_mass", "get_mass");
  604. ADD_PROPERTY(PropertyInfo(Variant::REAL, "weight", PROPERTY_HINT_EXP_RANGE, "0.01,65535,0.01", PROPERTY_USAGE_EDITOR), "set_weight", "get_weight");
  605. ADD_PROPERTY(PropertyInfo(Variant::REAL, "friction", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_friction", "get_friction");
  606. ADD_PROPERTY(PropertyInfo(Variant::REAL, "bounce", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_bounce", "get_bounce");
  607. ADD_PROPERTY(PropertyInfo(Variant::REAL, "gravity_scale", PROPERTY_HINT_RANGE, "-128,128,0.01"), "set_gravity_scale", "get_gravity_scale");
  608. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "custom_integrator"), "set_use_custom_integrator", "is_using_custom_integrator");
  609. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "continuous_cd"), "set_use_continuous_collision_detection", "is_using_continuous_collision_detection");
  610. ADD_PROPERTY(PropertyInfo(Variant::INT, "contacts_reported"), "set_max_contacts_reported", "get_max_contacts_reported");
  611. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "contact_monitor"), "set_contact_monitor", "is_contact_monitor_enabled");
  612. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sleeping"), "set_sleeping", "is_sleeping");
  613. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "can_sleep"), "set_can_sleep", "is_able_to_sleep");
  614. ADD_PROPERTY(PropertyInfo(Variant::INT, "axis_lock", PROPERTY_HINT_ENUM, "Disabled,Lock X,Lock Y,Lock Z"), "set_axis_lock", "get_axis_lock");
  615. ADD_GROUP("Linear", "linear_");
  616. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "linear_velocity"), "set_linear_velocity", "get_linear_velocity");
  617. ADD_PROPERTY(PropertyInfo(Variant::REAL, "linear_damp", PROPERTY_HINT_RANGE, "-1,128,0.01"), "set_linear_damp", "get_linear_damp");
  618. ADD_GROUP("Angular", "angular_");
  619. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "angular_velocity"), "set_angular_velocity", "get_angular_velocity");
  620. ADD_PROPERTY(PropertyInfo(Variant::REAL, "angular_damp", PROPERTY_HINT_RANGE, "-1,128,0.01"), "set_angular_damp", "get_angular_damp");
  621. 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")));
  622. 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")));
  623. ADD_SIGNAL(MethodInfo("body_entered", PropertyInfo(Variant::OBJECT, "body")));
  624. ADD_SIGNAL(MethodInfo("body_exited", PropertyInfo(Variant::OBJECT, "body")));
  625. ADD_SIGNAL(MethodInfo("sleeping_state_changed"));
  626. BIND_ENUM_CONSTANT(MODE_RIGID);
  627. BIND_ENUM_CONSTANT(MODE_STATIC);
  628. BIND_ENUM_CONSTANT(MODE_CHARACTER);
  629. BIND_ENUM_CONSTANT(MODE_KINEMATIC);
  630. BIND_ENUM_CONSTANT(AXIS_LOCK_DISABLED);
  631. BIND_ENUM_CONSTANT(AXIS_LOCK_X);
  632. BIND_ENUM_CONSTANT(AXIS_LOCK_Y);
  633. BIND_ENUM_CONSTANT(AXIS_LOCK_Z);
  634. }
  635. RigidBody::RigidBody()
  636. : PhysicsBody(PhysicsServer::BODY_MODE_RIGID) {
  637. mode = MODE_RIGID;
  638. bounce = 0;
  639. mass = 1;
  640. friction = 1;
  641. max_contacts_reported = 0;
  642. state = NULL;
  643. gravity_scale = 1;
  644. linear_damp = -1;
  645. angular_damp = -1;
  646. //angular_velocity=0;
  647. sleeping = false;
  648. ccd = false;
  649. custom_integrator = false;
  650. contact_monitor = NULL;
  651. can_sleep = true;
  652. axis_lock = AXIS_LOCK_DISABLED;
  653. PhysicsServer::get_singleton()->body_set_force_integration_callback(get_rid(), this, "_direct_state_changed");
  654. }
  655. RigidBody::~RigidBody() {
  656. if (contact_monitor)
  657. memdelete(contact_monitor);
  658. }
  659. //////////////////////////////////////////////////////
  660. //////////////////////////
  661. Ref<KinematicCollision> KinematicBody::_move(const Vector3 &p_motion) {
  662. Collision col;
  663. if (move_and_collide(p_motion, col)) {
  664. if (motion_cache.is_null()) {
  665. motion_cache.instance();
  666. motion_cache->owner = this;
  667. }
  668. motion_cache->collision = col;
  669. return motion_cache;
  670. }
  671. return Ref<KinematicCollision>();
  672. }
  673. bool KinematicBody::move_and_collide(const Vector3 &p_motion, Collision &r_collision) {
  674. Transform gt = get_global_transform();
  675. PhysicsServer::MotionResult result;
  676. bool colliding = PhysicsServer::get_singleton()->body_test_motion(get_rid(), gt, p_motion, margin, &result);
  677. if (colliding) {
  678. r_collision.collider_metadata = result.collider_metadata;
  679. r_collision.collider_shape = result.collider_shape;
  680. r_collision.collider_vel = result.collider_velocity;
  681. r_collision.collision = result.collision_point;
  682. r_collision.normal = result.collision_normal;
  683. r_collision.collider = result.collider_id;
  684. r_collision.travel = result.motion;
  685. r_collision.remainder = result.remainder;
  686. r_collision.local_shape = result.collision_local_shape;
  687. }
  688. gt.origin += result.motion;
  689. set_global_transform(gt);
  690. return colliding;
  691. }
  692. Vector3 KinematicBody::move_and_slide(const Vector3 &p_linear_velocity, const Vector3 &p_floor_direction, float p_slope_stop_min_velocity, int p_max_slides, float p_floor_max_angle) {
  693. Vector3 motion = (floor_velocity + p_linear_velocity) * get_physics_process_delta_time();
  694. Vector3 lv = p_linear_velocity;
  695. on_floor = false;
  696. on_ceiling = false;
  697. on_wall = false;
  698. colliders.clear();
  699. floor_velocity = Vector3();
  700. while (p_max_slides) {
  701. Collision collision;
  702. bool collided = move_and_collide(motion, collision);
  703. if (collided) {
  704. motion = collision.remainder;
  705. if (p_floor_direction == Vector3()) {
  706. //all is a wall
  707. on_wall = true;
  708. } else {
  709. if (collision.normal.dot(p_floor_direction) >= Math::cos(p_floor_max_angle)) { //floor
  710. on_floor = true;
  711. floor_velocity = collision.collider_vel;
  712. /*if (collision.travel.length() < 0.01 && ABS((lv.x - floor_velocity.x)) < p_slope_stop_min_velocity) {
  713. Transform gt = get_global_transform();
  714. gt.elements[2] -= collision.travel;
  715. set_global_transform(gt);
  716. return Vector3();
  717. }*/
  718. } else if (collision.normal.dot(-p_floor_direction) >= Math::cos(p_floor_max_angle)) { //ceiling
  719. on_ceiling = true;
  720. } else {
  721. on_wall = true;
  722. }
  723. }
  724. Vector3 n = collision.normal;
  725. motion = motion.slide(n);
  726. lv = lv.slide(n);
  727. colliders.push_back(collision);
  728. } else {
  729. break;
  730. }
  731. p_max_slides--;
  732. if (motion == Vector3())
  733. break;
  734. }
  735. return lv;
  736. }
  737. bool KinematicBody::is_on_floor() const {
  738. return on_floor;
  739. }
  740. bool KinematicBody::is_on_wall() const {
  741. return on_wall;
  742. }
  743. bool KinematicBody::is_on_ceiling() const {
  744. return on_ceiling;
  745. }
  746. Vector3 KinematicBody::get_floor_velocity() const {
  747. return floor_velocity;
  748. }
  749. bool KinematicBody::test_move(const Transform &p_from, const Vector3 &p_motion) {
  750. ERR_FAIL_COND_V(!is_inside_tree(), false);
  751. return PhysicsServer::get_singleton()->body_test_motion(get_rid(), p_from, p_motion, margin);
  752. }
  753. void KinematicBody::set_safe_margin(float p_margin) {
  754. margin = p_margin;
  755. }
  756. float KinematicBody::get_safe_margin() const {
  757. return margin;
  758. }
  759. int KinematicBody::get_slide_count() const {
  760. return colliders.size();
  761. }
  762. KinematicBody::Collision KinematicBody::get_slide_collision(int p_bounce) const {
  763. ERR_FAIL_INDEX_V(p_bounce, colliders.size(), Collision());
  764. return colliders[p_bounce];
  765. }
  766. Ref<KinematicCollision> KinematicBody::_get_slide_collision(int p_bounce) {
  767. ERR_FAIL_INDEX_V(p_bounce, colliders.size(), Ref<KinematicCollision>());
  768. if (p_bounce >= slide_colliders.size()) {
  769. slide_colliders.resize(p_bounce + 1);
  770. }
  771. if (slide_colliders[p_bounce].is_null()) {
  772. slide_colliders[p_bounce].instance();
  773. slide_colliders[p_bounce]->owner = this;
  774. }
  775. slide_colliders[p_bounce]->collision = colliders[p_bounce];
  776. return slide_colliders[p_bounce];
  777. }
  778. void KinematicBody::_bind_methods() {
  779. ClassDB::bind_method(D_METHOD("move_and_collide", "rel_vec"), &KinematicBody::_move);
  780. ClassDB::bind_method(D_METHOD("move_and_slide", "linear_velocity", "floor_normal", "slope_stop_min_velocity", "max_slides", "floor_max_angle"), &KinematicBody::move_and_slide, DEFVAL(Vector3(0, 0, 0)), DEFVAL(0.05), DEFVAL(4), DEFVAL(Math::deg2rad((float)45)));
  781. ClassDB::bind_method(D_METHOD("test_move", "from", "rel_vec"), &KinematicBody::test_move);
  782. ClassDB::bind_method(D_METHOD("is_on_floor"), &KinematicBody::is_on_floor);
  783. ClassDB::bind_method(D_METHOD("is_on_ceiling"), &KinematicBody::is_on_ceiling);
  784. ClassDB::bind_method(D_METHOD("is_on_wall"), &KinematicBody::is_on_wall);
  785. ClassDB::bind_method(D_METHOD("get_floor_velocity"), &KinematicBody::get_floor_velocity);
  786. ClassDB::bind_method(D_METHOD("set_safe_margin", "pixels"), &KinematicBody::set_safe_margin);
  787. ClassDB::bind_method(D_METHOD("get_safe_margin"), &KinematicBody::get_safe_margin);
  788. ClassDB::bind_method(D_METHOD("get_slide_count"), &KinematicBody::get_slide_count);
  789. ClassDB::bind_method(D_METHOD("get_slide_collision", "slide_idx"), &KinematicBody::_get_slide_collision);
  790. ADD_PROPERTY(PropertyInfo(Variant::REAL, "collision/safe_margin", PROPERTY_HINT_RANGE, "0.001,256,0.001"), "set_safe_margin", "get_safe_margin");
  791. }
  792. KinematicBody::KinematicBody()
  793. : PhysicsBody(PhysicsServer::BODY_MODE_KINEMATIC) {
  794. margin = 0.001;
  795. on_floor = false;
  796. on_ceiling = false;
  797. on_wall = false;
  798. }
  799. KinematicBody::~KinematicBody() {
  800. if (motion_cache.is_valid()) {
  801. motion_cache->owner = NULL;
  802. }
  803. for (int i = 0; i < slide_colliders.size(); i++) {
  804. if (slide_colliders[i].is_valid()) {
  805. slide_colliders[i]->owner = NULL;
  806. }
  807. }
  808. }
  809. ///////////////////////////////////////
  810. Vector3 KinematicCollision::get_position() const {
  811. return collision.collision;
  812. }
  813. Vector3 KinematicCollision::get_normal() const {
  814. return collision.normal;
  815. }
  816. Vector3 KinematicCollision::get_travel() const {
  817. return collision.travel;
  818. }
  819. Vector3 KinematicCollision::get_remainder() const {
  820. return collision.remainder;
  821. }
  822. Object *KinematicCollision::get_local_shape() const {
  823. ERR_FAIL_COND_V(!owner, NULL);
  824. uint32_t ownerid = owner->shape_find_owner(collision.local_shape);
  825. return owner->shape_owner_get_owner(ownerid);
  826. }
  827. Object *KinematicCollision::get_collider() const {
  828. if (collision.collider) {
  829. return ObjectDB::get_instance(collision.collider);
  830. }
  831. return NULL;
  832. }
  833. ObjectID KinematicCollision::get_collider_id() const {
  834. return collision.collider;
  835. }
  836. Object *KinematicCollision::get_collider_shape() const {
  837. Object *collider = get_collider();
  838. if (collider) {
  839. CollisionObject *obj2d = Object::cast_to<CollisionObject>(collider);
  840. if (obj2d) {
  841. uint32_t ownerid = obj2d->shape_find_owner(collision.collider_shape);
  842. return obj2d->shape_owner_get_owner(ownerid);
  843. }
  844. }
  845. return NULL;
  846. }
  847. int KinematicCollision::get_collider_shape_index() const {
  848. return collision.collider_shape;
  849. }
  850. Vector3 KinematicCollision::get_collider_velocity() const {
  851. return collision.collider_vel;
  852. }
  853. Variant KinematicCollision::get_collider_metadata() const {
  854. return Variant();
  855. }
  856. void KinematicCollision::_bind_methods() {
  857. ClassDB::bind_method(D_METHOD("get_position"), &KinematicCollision::get_position);
  858. ClassDB::bind_method(D_METHOD("get_normal"), &KinematicCollision::get_normal);
  859. ClassDB::bind_method(D_METHOD("get_travel"), &KinematicCollision::get_travel);
  860. ClassDB::bind_method(D_METHOD("get_remainder"), &KinematicCollision::get_remainder);
  861. ClassDB::bind_method(D_METHOD("get_local_shape"), &KinematicCollision::get_local_shape);
  862. ClassDB::bind_method(D_METHOD("get_collider"), &KinematicCollision::get_collider);
  863. ClassDB::bind_method(D_METHOD("get_collider_id"), &KinematicCollision::get_collider_id);
  864. ClassDB::bind_method(D_METHOD("get_collider_shape"), &KinematicCollision::get_collider_shape);
  865. ClassDB::bind_method(D_METHOD("get_collider_shape_index"), &KinematicCollision::get_collider_shape_index);
  866. ClassDB::bind_method(D_METHOD("get_collider_velocity"), &KinematicCollision::get_collider_velocity);
  867. ClassDB::bind_method(D_METHOD("get_collider_metadata"), &KinematicCollision::get_collider_metadata);
  868. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "position"), "", "get_position");
  869. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "normal"), "", "get_normal");
  870. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "travel"), "", "get_travel");
  871. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "remainder"), "", "get_remainder");
  872. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "local_shape"), "", "get_local_shape");
  873. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "collider"), "", "get_collider");
  874. ADD_PROPERTY(PropertyInfo(Variant::INT, "collider_id"), "", "get_collider_id");
  875. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "collider_shape"), "", "get_collider_shape");
  876. ADD_PROPERTY(PropertyInfo(Variant::INT, "collider_shape_index"), "", "get_collider_shape_index");
  877. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "collider_velocity"), "", "get_collider_velocity");
  878. ADD_PROPERTY(PropertyInfo(Variant::NIL, "collider_metadata", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT), "", "get_collider_metadata");
  879. }
  880. KinematicCollision::KinematicCollision() {
  881. collision.collider = 0;
  882. collision.collider_shape = 0;
  883. collision.local_shape = 0;
  884. owner = NULL;
  885. }