physics_body_2d.cpp 42 KB

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