physics_server_2d_extension.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /**************************************************************************/
  2. /* physics_server_2d_extension.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_server_2d_extension.h"
  31. bool PhysicsDirectSpaceState2DExtension::is_body_excluded_from_query(const RID &p_body) const {
  32. return exclude && exclude->has(p_body);
  33. }
  34. thread_local const HashSet<RID> *PhysicsDirectSpaceState2DExtension::exclude = nullptr;
  35. void PhysicsDirectSpaceState2DExtension::_bind_methods() {
  36. ClassDB::bind_method(D_METHOD("is_body_excluded_from_query", "body"), &PhysicsDirectSpaceState2DExtension::is_body_excluded_from_query);
  37. GDVIRTUAL_BIND(_intersect_ray, "from", "to", "collision_mask", "collide_with_bodies", "collide_with_areas", "hit_from_inside", "result");
  38. GDVIRTUAL_BIND(_intersect_point, "position", "canvas_instance_id", "collision_mask", "collide_with_bodies", "collide_with_areas", "results", "max_results");
  39. GDVIRTUAL_BIND(_intersect_shape, "shape_rid", "transform", "motion", "margin", "collision_mask", "collide_with_bodies", "collide_with_areas", "result", "max_results");
  40. GDVIRTUAL_BIND(_cast_motion, "shape_rid", "transform", "motion", "margin", "collision_mask", "collide_with_bodies", "collide_with_areas", "closest_safe", "closest_unsafe");
  41. GDVIRTUAL_BIND(_collide_shape, "shape_rid", "transform", "motion", "margin", "collision_mask", "collide_with_bodies", "collide_with_areas", "results", "max_results", "result_count");
  42. GDVIRTUAL_BIND(_rest_info, "shape_rid", "transform", "motion", "margin", "collision_mask", "collide_with_bodies", "collide_with_areas", "rest_info");
  43. }
  44. PhysicsDirectSpaceState2DExtension::PhysicsDirectSpaceState2DExtension() {
  45. }
  46. void PhysicsDirectBodyState2DExtension::_bind_methods() {
  47. GDVIRTUAL_BIND(_get_total_gravity);
  48. GDVIRTUAL_BIND(_get_total_linear_damp);
  49. GDVIRTUAL_BIND(_get_total_angular_damp);
  50. GDVIRTUAL_BIND(_get_center_of_mass);
  51. GDVIRTUAL_BIND(_get_center_of_mass_local);
  52. GDVIRTUAL_BIND(_get_inverse_mass);
  53. GDVIRTUAL_BIND(_get_inverse_inertia);
  54. GDVIRTUAL_BIND(_set_linear_velocity, "velocity");
  55. GDVIRTUAL_BIND(_get_linear_velocity);
  56. GDVIRTUAL_BIND(_set_angular_velocity, "velocity");
  57. GDVIRTUAL_BIND(_get_angular_velocity);
  58. GDVIRTUAL_BIND(_set_transform, "transform");
  59. GDVIRTUAL_BIND(_get_transform);
  60. GDVIRTUAL_BIND(_get_velocity_at_local_position, "local_position");
  61. GDVIRTUAL_BIND(_apply_central_impulse, "impulse");
  62. GDVIRTUAL_BIND(_apply_impulse, "impulse", "position");
  63. GDVIRTUAL_BIND(_apply_torque_impulse, "impulse");
  64. GDVIRTUAL_BIND(_apply_central_force, "force");
  65. GDVIRTUAL_BIND(_apply_force, "force", "position");
  66. GDVIRTUAL_BIND(_apply_torque, "torque");
  67. GDVIRTUAL_BIND(_add_constant_central_force, "force");
  68. GDVIRTUAL_BIND(_add_constant_force, "force", "position");
  69. GDVIRTUAL_BIND(_add_constant_torque, "torque");
  70. GDVIRTUAL_BIND(_set_constant_force, "force");
  71. GDVIRTUAL_BIND(_get_constant_force);
  72. GDVIRTUAL_BIND(_set_constant_torque, "torque");
  73. GDVIRTUAL_BIND(_get_constant_torque);
  74. GDVIRTUAL_BIND(_set_sleep_state, "enabled");
  75. GDVIRTUAL_BIND(_is_sleeping);
  76. GDVIRTUAL_BIND(_get_contact_count);
  77. GDVIRTUAL_BIND(_get_contact_local_position, "contact_idx");
  78. GDVIRTUAL_BIND(_get_contact_local_normal, "contact_idx");
  79. GDVIRTUAL_BIND(_get_contact_local_shape, "contact_idx");
  80. GDVIRTUAL_BIND(_get_contact_local_velocity_at_position, "contact_idx");
  81. GDVIRTUAL_BIND(_get_contact_collider, "contact_idx");
  82. GDVIRTUAL_BIND(_get_contact_collider_position, "contact_idx");
  83. GDVIRTUAL_BIND(_get_contact_collider_id, "contact_idx");
  84. GDVIRTUAL_BIND(_get_contact_collider_object, "contact_idx");
  85. GDVIRTUAL_BIND(_get_contact_collider_shape, "contact_idx");
  86. GDVIRTUAL_BIND(_get_contact_collider_velocity_at_position, "contact_idx");
  87. GDVIRTUAL_BIND(_get_contact_impulse, "contact_idx");
  88. GDVIRTUAL_BIND(_get_step);
  89. GDVIRTUAL_BIND(_integrate_forces);
  90. GDVIRTUAL_BIND(_get_space_state);
  91. }
  92. PhysicsDirectBodyState2DExtension::PhysicsDirectBodyState2DExtension() {
  93. }
  94. thread_local const HashSet<RID> *PhysicsServer2DExtension::exclude_bodies = nullptr;
  95. thread_local const HashSet<ObjectID> *PhysicsServer2DExtension::exclude_objects = nullptr;
  96. bool PhysicsServer2DExtension::body_test_motion_is_excluding_body(RID p_body) const {
  97. return exclude_bodies && exclude_bodies->has(p_body);
  98. }
  99. bool PhysicsServer2DExtension::body_test_motion_is_excluding_object(ObjectID p_object) const {
  100. return exclude_objects && exclude_objects->has(p_object);
  101. }
  102. void PhysicsServer2DExtension::_bind_methods() {
  103. /* SHAPE API */
  104. GDVIRTUAL_BIND(_world_boundary_shape_create);
  105. GDVIRTUAL_BIND(_separation_ray_shape_create);
  106. GDVIRTUAL_BIND(_segment_shape_create);
  107. GDVIRTUAL_BIND(_circle_shape_create);
  108. GDVIRTUAL_BIND(_rectangle_shape_create);
  109. GDVIRTUAL_BIND(_capsule_shape_create);
  110. GDVIRTUAL_BIND(_convex_polygon_shape_create);
  111. GDVIRTUAL_BIND(_concave_polygon_shape_create);
  112. GDVIRTUAL_BIND(_shape_set_data, "shape", "data");
  113. GDVIRTUAL_BIND(_shape_set_custom_solver_bias, "shape", "bias");
  114. GDVIRTUAL_BIND(_shape_get_type, "shape");
  115. GDVIRTUAL_BIND(_shape_get_data, "shape");
  116. GDVIRTUAL_BIND(_shape_get_custom_solver_bias, "shape");
  117. GDVIRTUAL_BIND(_shape_collide, "shape_A", "xform_A", "motion_A", "shape_B", "xform_B", "motion_B", "results", "result_max", "result_count");
  118. /* SPACE API */
  119. GDVIRTUAL_BIND(_space_create);
  120. GDVIRTUAL_BIND(_space_set_active, "space", "active");
  121. GDVIRTUAL_BIND(_space_is_active, "space");
  122. GDVIRTUAL_BIND(_space_set_param, "space", "param", "value");
  123. GDVIRTUAL_BIND(_space_get_param, "space", "param");
  124. GDVIRTUAL_BIND(_space_get_direct_state, "space");
  125. GDVIRTUAL_BIND(_space_set_debug_contacts, "space", "max_contacts");
  126. GDVIRTUAL_BIND(_space_get_contacts, "space");
  127. GDVIRTUAL_BIND(_space_get_contact_count, "space");
  128. /* AREA API */
  129. GDVIRTUAL_BIND(_area_create);
  130. GDVIRTUAL_BIND(_area_set_space, "area", "space");
  131. GDVIRTUAL_BIND(_area_get_space, "area");
  132. GDVIRTUAL_BIND(_area_add_shape, "area", "shape", "transform", "disabled");
  133. GDVIRTUAL_BIND(_area_set_shape, "area", "shape_idx", "shape");
  134. GDVIRTUAL_BIND(_area_set_shape_transform, "area", "shape_idx", "transform");
  135. GDVIRTUAL_BIND(_area_set_shape_disabled, "area", "shape_idx", "disabled");
  136. GDVIRTUAL_BIND(_area_get_shape_count, "area");
  137. GDVIRTUAL_BIND(_area_get_shape, "area", "shape_idx");
  138. GDVIRTUAL_BIND(_area_get_shape_transform, "area", "shape_idx");
  139. GDVIRTUAL_BIND(_area_remove_shape, "area", "shape_idx");
  140. GDVIRTUAL_BIND(_area_clear_shapes, "area");
  141. GDVIRTUAL_BIND(_area_attach_object_instance_id, "area", "id");
  142. GDVIRTUAL_BIND(_area_get_object_instance_id, "area");
  143. GDVIRTUAL_BIND(_area_attach_canvas_instance_id, "area", "id");
  144. GDVIRTUAL_BIND(_area_get_canvas_instance_id, "area");
  145. GDVIRTUAL_BIND(_area_set_param, "area", "param", "value");
  146. GDVIRTUAL_BIND(_area_set_transform, "area", "transform");
  147. GDVIRTUAL_BIND(_area_get_param, "area", "param");
  148. GDVIRTUAL_BIND(_area_get_transform, "area");
  149. GDVIRTUAL_BIND(_area_set_collision_layer, "area", "layer");
  150. GDVIRTUAL_BIND(_area_get_collision_layer, "area");
  151. GDVIRTUAL_BIND(_area_set_collision_mask, "area", "mask");
  152. GDVIRTUAL_BIND(_area_get_collision_mask, "area");
  153. GDVIRTUAL_BIND(_area_set_monitorable, "area", "monitorable");
  154. GDVIRTUAL_BIND(_area_set_pickable, "area", "pickable");
  155. GDVIRTUAL_BIND(_area_set_monitor_callback, "area", "callback");
  156. GDVIRTUAL_BIND(_area_set_area_monitor_callback, "area", "callback");
  157. /* BODY API */
  158. ClassDB::bind_method(D_METHOD("body_test_motion_is_excluding_body", "body"), &PhysicsServer2DExtension::body_test_motion_is_excluding_body);
  159. ClassDB::bind_method(D_METHOD("body_test_motion_is_excluding_object", "object"), &PhysicsServer2DExtension::body_test_motion_is_excluding_object);
  160. GDVIRTUAL_BIND(_body_create);
  161. GDVIRTUAL_BIND(_body_set_space, "body", "space");
  162. GDVIRTUAL_BIND(_body_get_space, "body");
  163. GDVIRTUAL_BIND(_body_set_mode, "body", "mode");
  164. GDVIRTUAL_BIND(_body_get_mode, "body");
  165. GDVIRTUAL_BIND(_body_add_shape, "body", "shape", "transform", "disabled");
  166. GDVIRTUAL_BIND(_body_set_shape, "body", "shape_idx", "shape");
  167. GDVIRTUAL_BIND(_body_set_shape_transform, "body", "shape_idx", "transform");
  168. GDVIRTUAL_BIND(_body_get_shape_count, "body");
  169. GDVIRTUAL_BIND(_body_get_shape, "body", "shape_idx");
  170. GDVIRTUAL_BIND(_body_get_shape_transform, "body", "shape_idx");
  171. GDVIRTUAL_BIND(_body_set_shape_disabled, "body", "shape_idx", "disabled");
  172. GDVIRTUAL_BIND(_body_set_shape_as_one_way_collision, "body", "shape_idx", "enable", "margin");
  173. GDVIRTUAL_BIND(_body_remove_shape, "body", "shape_idx");
  174. GDVIRTUAL_BIND(_body_clear_shapes, "body");
  175. GDVIRTUAL_BIND(_body_attach_object_instance_id, "body", "id");
  176. GDVIRTUAL_BIND(_body_get_object_instance_id, "body");
  177. GDVIRTUAL_BIND(_body_attach_canvas_instance_id, "body", "id");
  178. GDVIRTUAL_BIND(_body_get_canvas_instance_id, "body");
  179. GDVIRTUAL_BIND(_body_set_continuous_collision_detection_mode, "body", "mode");
  180. GDVIRTUAL_BIND(_body_get_continuous_collision_detection_mode, "body");
  181. GDVIRTUAL_BIND(_body_set_collision_layer, "body", "layer");
  182. GDVIRTUAL_BIND(_body_get_collision_layer, "body");
  183. GDVIRTUAL_BIND(_body_set_collision_mask, "body", "mask");
  184. GDVIRTUAL_BIND(_body_get_collision_mask, "body");
  185. GDVIRTUAL_BIND(_body_set_collision_priority, "body", "priority");
  186. GDVIRTUAL_BIND(_body_get_collision_priority, "body");
  187. GDVIRTUAL_BIND(_body_set_param, "body", "param", "value");
  188. GDVIRTUAL_BIND(_body_get_param, "body", "param");
  189. GDVIRTUAL_BIND(_body_reset_mass_properties, "body");
  190. GDVIRTUAL_BIND(_body_set_state, "body", "state", "value");
  191. GDVIRTUAL_BIND(_body_get_state, "body", "state");
  192. GDVIRTUAL_BIND(_body_apply_central_impulse, "body", "impulse");
  193. GDVIRTUAL_BIND(_body_apply_torque_impulse, "body", "impulse");
  194. GDVIRTUAL_BIND(_body_apply_impulse, "body", "impulse", "position");
  195. GDVIRTUAL_BIND(_body_apply_central_force, "body", "force");
  196. GDVIRTUAL_BIND(_body_apply_force, "body", "force", "position");
  197. GDVIRTUAL_BIND(_body_apply_torque, "body", "torque");
  198. GDVIRTUAL_BIND(_body_add_constant_central_force, "body", "force");
  199. GDVIRTUAL_BIND(_body_add_constant_force, "body", "force", "position");
  200. GDVIRTUAL_BIND(_body_add_constant_torque, "body", "torque");
  201. GDVIRTUAL_BIND(_body_set_constant_force, "body", "force");
  202. GDVIRTUAL_BIND(_body_get_constant_force, "body");
  203. GDVIRTUAL_BIND(_body_set_constant_torque, "body", "torque");
  204. GDVIRTUAL_BIND(_body_get_constant_torque, "body");
  205. GDVIRTUAL_BIND(_body_set_axis_velocity, "body", "axis_velocity");
  206. GDVIRTUAL_BIND(_body_add_collision_exception, "body", "excepted_body");
  207. GDVIRTUAL_BIND(_body_remove_collision_exception, "body", "excepted_body");
  208. GDVIRTUAL_BIND(_body_get_collision_exceptions, "body");
  209. GDVIRTUAL_BIND(_body_set_max_contacts_reported, "body", "amount");
  210. GDVIRTUAL_BIND(_body_get_max_contacts_reported, "body");
  211. GDVIRTUAL_BIND(_body_set_contacts_reported_depth_threshold, "body", "threshold");
  212. GDVIRTUAL_BIND(_body_get_contacts_reported_depth_threshold, "body");
  213. GDVIRTUAL_BIND(_body_set_omit_force_integration, "body", "enable");
  214. GDVIRTUAL_BIND(_body_is_omitting_force_integration, "body");
  215. GDVIRTUAL_BIND(_body_set_state_sync_callback, "body", "callable");
  216. GDVIRTUAL_BIND(_body_set_force_integration_callback, "body", "callable", "userdata");
  217. GDVIRTUAL_BIND(_body_collide_shape, "body", "body_shape", "shape", "shape_xform", "motion", "results", "result_max", "result_count");
  218. GDVIRTUAL_BIND(_body_set_pickable, "body", "pickable");
  219. GDVIRTUAL_BIND(_body_get_direct_state, "body");
  220. GDVIRTUAL_BIND(_body_test_motion, "body", "from", "motion", "margin", "collide_separation_ray", "recovery_as_collision", "result");
  221. /* JOINT API */
  222. GDVIRTUAL_BIND(_joint_create);
  223. GDVIRTUAL_BIND(_joint_clear, "joint");
  224. GDVIRTUAL_BIND(_joint_set_param, "joint", "param", "value");
  225. GDVIRTUAL_BIND(_joint_get_param, "joint", "param");
  226. GDVIRTUAL_BIND(_joint_disable_collisions_between_bodies, "joint", "disable");
  227. GDVIRTUAL_BIND(_joint_is_disabled_collisions_between_bodies, "joint");
  228. GDVIRTUAL_BIND(_joint_make_pin, "joint", "anchor", "body_a", "body_b");
  229. GDVIRTUAL_BIND(_joint_make_groove, "joint", "a_groove1", "a_groove2", "b_anchor", "body_a", "body_b");
  230. GDVIRTUAL_BIND(_joint_make_damped_spring, "joint", "anchor_a", "anchor_b", "body_a", "body_b");
  231. GDVIRTUAL_BIND(_pin_joint_set_flag, "joint", "flag", "enabled");
  232. GDVIRTUAL_BIND(_pin_joint_get_flag, "joint", "flag");
  233. GDVIRTUAL_BIND(_pin_joint_set_param, "joint", "param", "value");
  234. GDVIRTUAL_BIND(_pin_joint_get_param, "joint", "param");
  235. GDVIRTUAL_BIND(_damped_spring_joint_set_param, "joint", "param", "value");
  236. GDVIRTUAL_BIND(_damped_spring_joint_get_param, "joint", "param");
  237. GDVIRTUAL_BIND(_joint_get_type, "joint");
  238. /* MISC */
  239. GDVIRTUAL_BIND(_free_rid, "rid");
  240. GDVIRTUAL_BIND(_set_active, "active");
  241. GDVIRTUAL_BIND(_init);
  242. GDVIRTUAL_BIND(_step, "step");
  243. GDVIRTUAL_BIND(_sync);
  244. GDVIRTUAL_BIND(_flush_queries);
  245. GDVIRTUAL_BIND(_end_sync);
  246. GDVIRTUAL_BIND(_finish);
  247. GDVIRTUAL_BIND(_is_flushing_queries);
  248. GDVIRTUAL_BIND(_get_process_info, "process_info");
  249. }
  250. PhysicsServer2DExtension::PhysicsServer2DExtension() {
  251. }
  252. PhysicsServer2DExtension::~PhysicsServer2DExtension() {
  253. }