physics_2d_server.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. /*************************************************************************/
  2. /* physics_2d_server.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_2d_server.h"
  31. #include "print_string.h"
  32. Physics2DServer *Physics2DServer::singleton = NULL;
  33. void Physics2DDirectBodyState::integrate_forces() {
  34. real_t step = get_step();
  35. Vector2 lv = get_linear_velocity();
  36. lv += get_total_gravity() * step;
  37. real_t av = get_angular_velocity();
  38. float damp = 1.0 - step * get_total_linear_damp();
  39. if (damp < 0) // reached zero in the given time
  40. damp = 0;
  41. lv *= damp;
  42. damp = 1.0 - step * get_total_angular_damp();
  43. if (damp < 0) // reached zero in the given time
  44. damp = 0;
  45. av *= damp;
  46. set_linear_velocity(lv);
  47. set_angular_velocity(av);
  48. }
  49. Object *Physics2DDirectBodyState::get_contact_collider_object(int p_contact_idx) const {
  50. ObjectID objid = get_contact_collider_id(p_contact_idx);
  51. Object *obj = ObjectDB::get_instance(objid);
  52. return obj;
  53. }
  54. Physics2DServer *Physics2DServer::get_singleton() {
  55. return singleton;
  56. }
  57. void Physics2DDirectBodyState::_bind_methods() {
  58. ClassDB::bind_method(D_METHOD("get_total_gravity"), &Physics2DDirectBodyState::get_total_gravity);
  59. ClassDB::bind_method(D_METHOD("get_total_linear_damp"), &Physics2DDirectBodyState::get_total_linear_damp);
  60. ClassDB::bind_method(D_METHOD("get_total_angular_damp"), &Physics2DDirectBodyState::get_total_angular_damp);
  61. ClassDB::bind_method(D_METHOD("get_inverse_mass"), &Physics2DDirectBodyState::get_inverse_mass);
  62. ClassDB::bind_method(D_METHOD("get_inverse_inertia"), &Physics2DDirectBodyState::get_inverse_inertia);
  63. ClassDB::bind_method(D_METHOD("set_linear_velocity", "velocity"), &Physics2DDirectBodyState::set_linear_velocity);
  64. ClassDB::bind_method(D_METHOD("get_linear_velocity"), &Physics2DDirectBodyState::get_linear_velocity);
  65. ClassDB::bind_method(D_METHOD("set_angular_velocity", "velocity"), &Physics2DDirectBodyState::set_angular_velocity);
  66. ClassDB::bind_method(D_METHOD("get_angular_velocity"), &Physics2DDirectBodyState::get_angular_velocity);
  67. ClassDB::bind_method(D_METHOD("set_transform", "transform"), &Physics2DDirectBodyState::set_transform);
  68. ClassDB::bind_method(D_METHOD("get_transform"), &Physics2DDirectBodyState::get_transform);
  69. ClassDB::bind_method(D_METHOD("set_sleep_state", "enabled"), &Physics2DDirectBodyState::set_sleep_state);
  70. ClassDB::bind_method(D_METHOD("is_sleeping"), &Physics2DDirectBodyState::is_sleeping);
  71. ClassDB::bind_method(D_METHOD("get_contact_count"), &Physics2DDirectBodyState::get_contact_count);
  72. ClassDB::bind_method(D_METHOD("get_contact_local_position", "contact_idx"), &Physics2DDirectBodyState::get_contact_local_position);
  73. ClassDB::bind_method(D_METHOD("get_contact_local_normal", "contact_idx"), &Physics2DDirectBodyState::get_contact_local_normal);
  74. ClassDB::bind_method(D_METHOD("get_contact_local_shape", "contact_idx"), &Physics2DDirectBodyState::get_contact_local_shape);
  75. ClassDB::bind_method(D_METHOD("get_contact_collider", "contact_idx"), &Physics2DDirectBodyState::get_contact_collider);
  76. ClassDB::bind_method(D_METHOD("get_contact_collider_position", "contact_idx"), &Physics2DDirectBodyState::get_contact_collider_position);
  77. ClassDB::bind_method(D_METHOD("get_contact_collider_id", "contact_idx"), &Physics2DDirectBodyState::get_contact_collider_id);
  78. ClassDB::bind_method(D_METHOD("get_contact_collider_object", "contact_idx"), &Physics2DDirectBodyState::get_contact_collider_object);
  79. ClassDB::bind_method(D_METHOD("get_contact_collider_shape", "contact_idx"), &Physics2DDirectBodyState::get_contact_collider_shape);
  80. ClassDB::bind_method(D_METHOD("get_contact_collider_shape_metadata", "contact_idx"), &Physics2DDirectBodyState::get_contact_collider_shape_metadata);
  81. ClassDB::bind_method(D_METHOD("get_contact_collider_velocity_at_position", "contact_idx"), &Physics2DDirectBodyState::get_contact_collider_velocity_at_position);
  82. ClassDB::bind_method(D_METHOD("get_step"), &Physics2DDirectBodyState::get_step);
  83. ClassDB::bind_method(D_METHOD("integrate_forces"), &Physics2DDirectBodyState::integrate_forces);
  84. ClassDB::bind_method(D_METHOD("get_space_state"), &Physics2DDirectBodyState::get_space_state);
  85. }
  86. Physics2DDirectBodyState::Physics2DDirectBodyState() {}
  87. ///////////////////////////////////////////////////////
  88. void Physics2DShapeQueryParameters::set_shape(const RES &p_shape) {
  89. ERR_FAIL_COND(p_shape.is_null());
  90. shape = p_shape->get_rid();
  91. }
  92. void Physics2DShapeQueryParameters::set_shape_rid(const RID &p_shape) {
  93. shape = p_shape;
  94. }
  95. RID Physics2DShapeQueryParameters::get_shape_rid() const {
  96. return shape;
  97. }
  98. void Physics2DShapeQueryParameters::set_transform(const Transform2D &p_transform) {
  99. transform = p_transform;
  100. }
  101. Transform2D Physics2DShapeQueryParameters::get_transform() const {
  102. return transform;
  103. }
  104. void Physics2DShapeQueryParameters::set_motion(const Vector2 &p_motion) {
  105. motion = p_motion;
  106. }
  107. Vector2 Physics2DShapeQueryParameters::get_motion() const {
  108. return motion;
  109. }
  110. void Physics2DShapeQueryParameters::set_margin(float p_margin) {
  111. margin = p_margin;
  112. }
  113. float Physics2DShapeQueryParameters::get_margin() const {
  114. return margin;
  115. }
  116. void Physics2DShapeQueryParameters::set_collision_layer(int p_collision_layer) {
  117. collision_layer = p_collision_layer;
  118. }
  119. int Physics2DShapeQueryParameters::get_collision_layer() const {
  120. return collision_layer;
  121. }
  122. void Physics2DShapeQueryParameters::set_object_type_mask(int p_object_type_mask) {
  123. object_type_mask = p_object_type_mask;
  124. }
  125. int Physics2DShapeQueryParameters::get_object_type_mask() const {
  126. return object_type_mask;
  127. }
  128. void Physics2DShapeQueryParameters::set_exclude(const Vector<RID> &p_exclude) {
  129. exclude.clear();
  130. for (int i = 0; i < p_exclude.size(); i++)
  131. exclude.insert(p_exclude[i]);
  132. }
  133. Vector<RID> Physics2DShapeQueryParameters::get_exclude() const {
  134. Vector<RID> ret;
  135. ret.resize(exclude.size());
  136. int idx = 0;
  137. for (Set<RID>::Element *E = exclude.front(); E; E = E->next()) {
  138. ret[idx] = E->get();
  139. }
  140. return ret;
  141. }
  142. void Physics2DShapeQueryParameters::_bind_methods() {
  143. ClassDB::bind_method(D_METHOD("set_shape", "shape"), &Physics2DShapeQueryParameters::set_shape);
  144. ClassDB::bind_method(D_METHOD("set_shape_rid", "shape"), &Physics2DShapeQueryParameters::set_shape_rid);
  145. ClassDB::bind_method(D_METHOD("get_shape_rid"), &Physics2DShapeQueryParameters::get_shape_rid);
  146. ClassDB::bind_method(D_METHOD("set_transform", "transform"), &Physics2DShapeQueryParameters::set_transform);
  147. ClassDB::bind_method(D_METHOD("get_transform"), &Physics2DShapeQueryParameters::get_transform);
  148. ClassDB::bind_method(D_METHOD("set_motion", "motion"), &Physics2DShapeQueryParameters::set_motion);
  149. ClassDB::bind_method(D_METHOD("get_motion"), &Physics2DShapeQueryParameters::get_motion);
  150. ClassDB::bind_method(D_METHOD("set_margin", "margin"), &Physics2DShapeQueryParameters::set_margin);
  151. ClassDB::bind_method(D_METHOD("get_margin"), &Physics2DShapeQueryParameters::get_margin);
  152. ClassDB::bind_method(D_METHOD("set_collision_layer", "collision_layer"), &Physics2DShapeQueryParameters::set_collision_layer);
  153. ClassDB::bind_method(D_METHOD("get_collision_layer"), &Physics2DShapeQueryParameters::get_collision_layer);
  154. ClassDB::bind_method(D_METHOD("set_object_type_mask", "object_type_mask"), &Physics2DShapeQueryParameters::set_object_type_mask);
  155. ClassDB::bind_method(D_METHOD("get_object_type_mask"), &Physics2DShapeQueryParameters::get_object_type_mask);
  156. ClassDB::bind_method(D_METHOD("set_exclude", "exclude"), &Physics2DShapeQueryParameters::set_exclude);
  157. ClassDB::bind_method(D_METHOD("get_exclude"), &Physics2DShapeQueryParameters::get_exclude);
  158. }
  159. Physics2DShapeQueryParameters::Physics2DShapeQueryParameters() {
  160. margin = 0;
  161. collision_layer = 0x7FFFFFFF;
  162. object_type_mask = Physics2DDirectSpaceState::TYPE_MASK_COLLISION;
  163. }
  164. Dictionary Physics2DDirectSpaceState::_intersect_ray(const Vector2 &p_from, const Vector2 &p_to, const Vector<RID> &p_exclude, uint32_t p_layers, uint32_t p_object_type_mask) {
  165. RayResult inters;
  166. Set<RID> exclude;
  167. for (int i = 0; i < p_exclude.size(); i++)
  168. exclude.insert(p_exclude[i]);
  169. bool res = intersect_ray(p_from, p_to, inters, exclude, p_layers, p_object_type_mask);
  170. if (!res)
  171. return Dictionary();
  172. Dictionary d;
  173. d["position"] = inters.position;
  174. d["normal"] = inters.normal;
  175. d["collider_id"] = inters.collider_id;
  176. d["collider"] = inters.collider;
  177. d["shape"] = inters.shape;
  178. d["rid"] = inters.rid;
  179. d["metadata"] = inters.metadata;
  180. return d;
  181. }
  182. Array Physics2DDirectSpaceState::_intersect_shape(const Ref<Physics2DShapeQueryParameters> &p_shape_query, int p_max_results) {
  183. Vector<ShapeResult> sr;
  184. sr.resize(p_max_results);
  185. int rc = intersect_shape(p_shape_query->shape, p_shape_query->transform, p_shape_query->motion, p_shape_query->margin, sr.ptr(), sr.size(), p_shape_query->exclude, p_shape_query->collision_layer, p_shape_query->object_type_mask);
  186. Array ret;
  187. ret.resize(rc);
  188. for (int i = 0; i < rc; i++) {
  189. Dictionary d;
  190. d["rid"] = sr[i].rid;
  191. d["collider_id"] = sr[i].collider_id;
  192. d["collider"] = sr[i].collider;
  193. d["shape"] = sr[i].shape;
  194. d["metadata"] = sr[i].metadata;
  195. ret[i] = d;
  196. }
  197. return ret;
  198. }
  199. Array Physics2DDirectSpaceState::_cast_motion(const Ref<Physics2DShapeQueryParameters> &p_shape_query) {
  200. float closest_safe, closest_unsafe;
  201. bool res = cast_motion(p_shape_query->shape, p_shape_query->transform, p_shape_query->motion, p_shape_query->margin, closest_safe, closest_unsafe, p_shape_query->exclude, p_shape_query->collision_layer, p_shape_query->object_type_mask);
  202. if (!res)
  203. return Array();
  204. Array ret;
  205. ret.resize(2);
  206. ret[0] = closest_safe;
  207. ret[1] = closest_unsafe;
  208. return ret;
  209. }
  210. Array Physics2DDirectSpaceState::_intersect_point(const Vector2 &p_point, int p_max_results, const Vector<RID> &p_exclude, uint32_t p_layers, uint32_t p_object_type_mask) {
  211. Set<RID> exclude;
  212. for (int i = 0; i < p_exclude.size(); i++)
  213. exclude.insert(p_exclude[i]);
  214. Vector<ShapeResult> ret;
  215. ret.resize(p_max_results);
  216. int rc = intersect_point(p_point, ret.ptr(), ret.size(), exclude, p_layers, p_object_type_mask);
  217. if (rc == 0)
  218. return Array();
  219. Array r;
  220. r.resize(rc);
  221. for (int i = 0; i < rc; i++) {
  222. Dictionary d;
  223. d["rid"] = ret[i].rid;
  224. d["collider_id"] = ret[i].collider_id;
  225. d["collider"] = ret[i].collider;
  226. d["shape"] = ret[i].shape;
  227. d["metadata"] = ret[i].metadata;
  228. r[i] = d;
  229. }
  230. return r;
  231. }
  232. Array Physics2DDirectSpaceState::_collide_shape(const Ref<Physics2DShapeQueryParameters> &p_shape_query, int p_max_results) {
  233. Vector<Vector2> ret;
  234. ret.resize(p_max_results * 2);
  235. int rc = 0;
  236. bool res = collide_shape(p_shape_query->shape, p_shape_query->transform, p_shape_query->motion, p_shape_query->margin, ret.ptr(), p_max_results, rc, p_shape_query->exclude, p_shape_query->collision_layer, p_shape_query->object_type_mask);
  237. if (!res)
  238. return Array();
  239. Array r;
  240. r.resize(rc * 2);
  241. for (int i = 0; i < rc * 2; i++)
  242. r[i] = ret[i];
  243. return r;
  244. }
  245. Dictionary Physics2DDirectSpaceState::_get_rest_info(const Ref<Physics2DShapeQueryParameters> &p_shape_query) {
  246. ShapeRestInfo sri;
  247. bool res = rest_info(p_shape_query->shape, p_shape_query->transform, p_shape_query->motion, p_shape_query->margin, &sri, p_shape_query->exclude, p_shape_query->collision_layer, p_shape_query->object_type_mask);
  248. Dictionary r;
  249. if (!res)
  250. return r;
  251. r["point"] = sri.point;
  252. r["normal"] = sri.normal;
  253. r["rid"] = sri.rid;
  254. r["collider_id"] = sri.collider_id;
  255. r["shape"] = sri.shape;
  256. r["linear_velocity"] = sri.linear_velocity;
  257. r["metadata"] = sri.metadata;
  258. return r;
  259. }
  260. Physics2DDirectSpaceState::Physics2DDirectSpaceState() {
  261. }
  262. void Physics2DDirectSpaceState::_bind_methods() {
  263. ClassDB::bind_method(D_METHOD("intersect_point", "point", "max_results", "exclude", "collision_layer", "type_mask"), &Physics2DDirectSpaceState::_intersect_point, DEFVAL(32), DEFVAL(Array()), DEFVAL(0x7FFFFFFF), DEFVAL(TYPE_MASK_COLLISION));
  264. ClassDB::bind_method(D_METHOD("intersect_ray", "from", "to", "exclude", "collision_layer", "type_mask"), &Physics2DDirectSpaceState::_intersect_ray, DEFVAL(Array()), DEFVAL(0x7FFFFFFF), DEFVAL(TYPE_MASK_COLLISION));
  265. ClassDB::bind_method(D_METHOD("intersect_shape", "shape", "max_results"), &Physics2DDirectSpaceState::_intersect_shape, DEFVAL(32));
  266. ClassDB::bind_method(D_METHOD("cast_motion", "shape"), &Physics2DDirectSpaceState::_cast_motion);
  267. ClassDB::bind_method(D_METHOD("collide_shape", "shape", "max_results"), &Physics2DDirectSpaceState::_collide_shape, DEFVAL(32));
  268. ClassDB::bind_method(D_METHOD("get_rest_info", "shape"), &Physics2DDirectSpaceState::_get_rest_info);
  269. //ClassDB::bind_method(D_METHOD("cast_motion","shape","xform","motion","exclude","umask"),&Physics2DDirectSpaceState::_intersect_shape,DEFVAL(Array()),DEFVAL(0));
  270. BIND_ENUM_CONSTANT(TYPE_MASK_STATIC_BODY);
  271. BIND_ENUM_CONSTANT(TYPE_MASK_KINEMATIC_BODY);
  272. BIND_ENUM_CONSTANT(TYPE_MASK_RIGID_BODY);
  273. BIND_ENUM_CONSTANT(TYPE_MASK_CHARACTER_BODY);
  274. BIND_ENUM_CONSTANT(TYPE_MASK_AREA);
  275. BIND_ENUM_CONSTANT(TYPE_MASK_COLLISION);
  276. }
  277. int Physics2DShapeQueryResult::get_result_count() const {
  278. return result.size();
  279. }
  280. RID Physics2DShapeQueryResult::get_result_rid(int p_idx) const {
  281. return result[p_idx].rid;
  282. }
  283. ObjectID Physics2DShapeQueryResult::get_result_object_id(int p_idx) const {
  284. return result[p_idx].collider_id;
  285. }
  286. Object *Physics2DShapeQueryResult::get_result_object(int p_idx) const {
  287. return result[p_idx].collider;
  288. }
  289. int Physics2DShapeQueryResult::get_result_object_shape(int p_idx) const {
  290. return result[p_idx].shape;
  291. }
  292. Physics2DShapeQueryResult::Physics2DShapeQueryResult() {
  293. }
  294. void Physics2DShapeQueryResult::_bind_methods() {
  295. ClassDB::bind_method(D_METHOD("get_result_count"), &Physics2DShapeQueryResult::get_result_count);
  296. ClassDB::bind_method(D_METHOD("get_result_rid", "idx"), &Physics2DShapeQueryResult::get_result_rid);
  297. ClassDB::bind_method(D_METHOD("get_result_object_id", "idx"), &Physics2DShapeQueryResult::get_result_object_id);
  298. ClassDB::bind_method(D_METHOD("get_result_object", "idx"), &Physics2DShapeQueryResult::get_result_object);
  299. ClassDB::bind_method(D_METHOD("get_result_object_shape", "idx"), &Physics2DShapeQueryResult::get_result_object_shape);
  300. }
  301. ///////////////////////////////
  302. /*bool Physics2DTestMotionResult::is_colliding() const {
  303. return colliding;
  304. }*/
  305. Vector2 Physics2DTestMotionResult::get_motion() const {
  306. return result.motion;
  307. }
  308. Vector2 Physics2DTestMotionResult::get_motion_remainder() const {
  309. return result.remainder;
  310. }
  311. Vector2 Physics2DTestMotionResult::get_collision_point() const {
  312. return result.collision_point;
  313. }
  314. Vector2 Physics2DTestMotionResult::get_collision_normal() const {
  315. return result.collision_normal;
  316. }
  317. Vector2 Physics2DTestMotionResult::get_collider_velocity() const {
  318. return result.collider_velocity;
  319. }
  320. ObjectID Physics2DTestMotionResult::get_collider_id() const {
  321. return result.collider_id;
  322. }
  323. RID Physics2DTestMotionResult::get_collider_rid() const {
  324. return result.collider;
  325. }
  326. Object *Physics2DTestMotionResult::get_collider() const {
  327. return ObjectDB::get_instance(result.collider_id);
  328. }
  329. int Physics2DTestMotionResult::get_collider_shape() const {
  330. return result.collider_shape;
  331. }
  332. void Physics2DTestMotionResult::_bind_methods() {
  333. //ClassDB::bind_method(D_METHOD("is_colliding"),&Physics2DTestMotionResult::is_colliding);
  334. ClassDB::bind_method(D_METHOD("get_motion"), &Physics2DTestMotionResult::get_motion);
  335. ClassDB::bind_method(D_METHOD("get_motion_remainder"), &Physics2DTestMotionResult::get_motion_remainder);
  336. ClassDB::bind_method(D_METHOD("get_collision_point"), &Physics2DTestMotionResult::get_collision_point);
  337. ClassDB::bind_method(D_METHOD("get_collision_normal"), &Physics2DTestMotionResult::get_collision_normal);
  338. ClassDB::bind_method(D_METHOD("get_collider_velocity"), &Physics2DTestMotionResult::get_collider_velocity);
  339. ClassDB::bind_method(D_METHOD("get_collider_id"), &Physics2DTestMotionResult::get_collider_id);
  340. ClassDB::bind_method(D_METHOD("get_collider_rid"), &Physics2DTestMotionResult::get_collider_rid);
  341. ClassDB::bind_method(D_METHOD("get_collider"), &Physics2DTestMotionResult::get_collider);
  342. ClassDB::bind_method(D_METHOD("get_collider_shape"), &Physics2DTestMotionResult::get_collider_shape);
  343. }
  344. Physics2DTestMotionResult::Physics2DTestMotionResult() {
  345. colliding = false;
  346. result.collider_id = 0;
  347. result.collider_shape = 0;
  348. }
  349. ///////////////////////////////////////
  350. bool Physics2DServer::_body_test_motion(RID p_body, const Transform2D &p_from, const Vector2 &p_motion, float p_margin, const Ref<Physics2DTestMotionResult> &p_result) {
  351. MotionResult *r = NULL;
  352. if (p_result.is_valid())
  353. r = p_result->get_result_ptr();
  354. return body_test_motion(p_body, p_from, p_motion, p_margin, r);
  355. }
  356. void Physics2DServer::_bind_methods() {
  357. ClassDB::bind_method(D_METHOD("shape_create", "type"), &Physics2DServer::shape_create);
  358. ClassDB::bind_method(D_METHOD("shape_set_data", "shape", "data"), &Physics2DServer::shape_set_data);
  359. ClassDB::bind_method(D_METHOD("shape_get_type", "shape"), &Physics2DServer::shape_get_type);
  360. ClassDB::bind_method(D_METHOD("shape_get_data", "shape"), &Physics2DServer::shape_get_data);
  361. ClassDB::bind_method(D_METHOD("space_create"), &Physics2DServer::space_create);
  362. ClassDB::bind_method(D_METHOD("space_set_active", "space", "active"), &Physics2DServer::space_set_active);
  363. ClassDB::bind_method(D_METHOD("space_is_active", "space"), &Physics2DServer::space_is_active);
  364. ClassDB::bind_method(D_METHOD("space_set_param", "space", "param", "value"), &Physics2DServer::space_set_param);
  365. ClassDB::bind_method(D_METHOD("space_get_param", "space", "param"), &Physics2DServer::space_get_param);
  366. ClassDB::bind_method(D_METHOD("space_get_direct_state", "space"), &Physics2DServer::space_get_direct_state);
  367. ClassDB::bind_method(D_METHOD("area_create"), &Physics2DServer::area_create);
  368. ClassDB::bind_method(D_METHOD("area_set_space", "area", "space"), &Physics2DServer::area_set_space);
  369. ClassDB::bind_method(D_METHOD("area_get_space", "area"), &Physics2DServer::area_get_space);
  370. ClassDB::bind_method(D_METHOD("area_set_space_override_mode", "area", "mode"), &Physics2DServer::area_set_space_override_mode);
  371. ClassDB::bind_method(D_METHOD("area_get_space_override_mode", "area"), &Physics2DServer::area_get_space_override_mode);
  372. ClassDB::bind_method(D_METHOD("area_add_shape", "area", "shape", "transform"), &Physics2DServer::area_add_shape, DEFVAL(Transform2D()));
  373. ClassDB::bind_method(D_METHOD("area_set_shape", "area", "shape_idx", "shape"), &Physics2DServer::area_set_shape);
  374. ClassDB::bind_method(D_METHOD("area_set_shape_transform", "area", "shape_idx", "transform"), &Physics2DServer::area_set_shape_transform);
  375. ClassDB::bind_method(D_METHOD("area_set_shape_disabled", "area", "shape_idx", "disable"), &Physics2DServer::area_set_shape_disabled);
  376. ClassDB::bind_method(D_METHOD("area_get_shape_count", "area"), &Physics2DServer::area_get_shape_count);
  377. ClassDB::bind_method(D_METHOD("area_get_shape", "area", "shape_idx"), &Physics2DServer::area_get_shape);
  378. ClassDB::bind_method(D_METHOD("area_get_shape_transform", "area", "shape_idx"), &Physics2DServer::area_get_shape_transform);
  379. ClassDB::bind_method(D_METHOD("area_remove_shape", "area", "shape_idx"), &Physics2DServer::area_remove_shape);
  380. ClassDB::bind_method(D_METHOD("area_clear_shapes", "area"), &Physics2DServer::area_clear_shapes);
  381. ClassDB::bind_method(D_METHOD("area_set_collision_layer", "area", "layer"), &Physics2DServer::area_set_collision_layer);
  382. ClassDB::bind_method(D_METHOD("area_set_collision_mask", "area", "mask"), &Physics2DServer::area_set_collision_mask);
  383. ClassDB::bind_method(D_METHOD("area_set_param", "area", "param", "value"), &Physics2DServer::area_set_param);
  384. ClassDB::bind_method(D_METHOD("area_set_transform", "area", "transform"), &Physics2DServer::area_set_transform);
  385. ClassDB::bind_method(D_METHOD("area_get_param", "area", "param"), &Physics2DServer::area_get_param);
  386. ClassDB::bind_method(D_METHOD("area_get_transform", "area"), &Physics2DServer::area_get_transform);
  387. ClassDB::bind_method(D_METHOD("area_attach_object_instance_id", "area", "id"), &Physics2DServer::area_attach_object_instance_id);
  388. ClassDB::bind_method(D_METHOD("area_get_object_instance_id", "area"), &Physics2DServer::area_get_object_instance_id);
  389. ClassDB::bind_method(D_METHOD("area_set_monitor_callback", "area", "receiver", "method"), &Physics2DServer::area_set_monitor_callback);
  390. ClassDB::bind_method(D_METHOD("body_create", "mode", "init_sleeping"), &Physics2DServer::body_create, DEFVAL(BODY_MODE_RIGID), DEFVAL(false));
  391. ClassDB::bind_method(D_METHOD("body_set_space", "body", "space"), &Physics2DServer::body_set_space);
  392. ClassDB::bind_method(D_METHOD("body_get_space", "body"), &Physics2DServer::body_get_space);
  393. ClassDB::bind_method(D_METHOD("body_set_mode", "body", "mode"), &Physics2DServer::body_set_mode);
  394. ClassDB::bind_method(D_METHOD("body_get_mode", "body"), &Physics2DServer::body_get_mode);
  395. ClassDB::bind_method(D_METHOD("body_add_shape", "body", "shape", "transform"), &Physics2DServer::body_add_shape, DEFVAL(Transform2D()));
  396. ClassDB::bind_method(D_METHOD("body_set_shape", "body", "shape_idx", "shape"), &Physics2DServer::body_set_shape);
  397. ClassDB::bind_method(D_METHOD("body_set_shape_transform", "body", "shape_idx", "transform"), &Physics2DServer::body_set_shape_transform);
  398. ClassDB::bind_method(D_METHOD("body_set_shape_metadata", "body", "shape_idx", "metadata"), &Physics2DServer::body_set_shape_metadata);
  399. ClassDB::bind_method(D_METHOD("body_get_shape_count", "body"), &Physics2DServer::body_get_shape_count);
  400. ClassDB::bind_method(D_METHOD("body_get_shape", "body", "shape_idx"), &Physics2DServer::body_get_shape);
  401. ClassDB::bind_method(D_METHOD("body_get_shape_transform", "body", "shape_idx"), &Physics2DServer::body_get_shape_transform);
  402. ClassDB::bind_method(D_METHOD("body_get_shape_metadata", "body", "shape_idx"), &Physics2DServer::body_get_shape_metadata);
  403. ClassDB::bind_method(D_METHOD("body_remove_shape", "body", "shape_idx"), &Physics2DServer::body_remove_shape);
  404. ClassDB::bind_method(D_METHOD("body_clear_shapes", "body"), &Physics2DServer::body_clear_shapes);
  405. ClassDB::bind_method(D_METHOD("body_set_shape_disabled", "body", "shape_idx", "disable"), &Physics2DServer::body_set_shape_disabled);
  406. ClassDB::bind_method(D_METHOD("body_set_shape_as_one_way_collision", "body", "shape_idx", "enable"), &Physics2DServer::body_set_shape_as_one_way_collision);
  407. ClassDB::bind_method(D_METHOD("body_attach_object_instance_id", "body", "id"), &Physics2DServer::body_attach_object_instance_id);
  408. ClassDB::bind_method(D_METHOD("body_get_object_instance_id", "body"), &Physics2DServer::body_get_object_instance_id);
  409. ClassDB::bind_method(D_METHOD("body_set_continuous_collision_detection_mode", "body", "mode"), &Physics2DServer::body_set_continuous_collision_detection_mode);
  410. ClassDB::bind_method(D_METHOD("body_get_continuous_collision_detection_mode", "body"), &Physics2DServer::body_get_continuous_collision_detection_mode);
  411. ClassDB::bind_method(D_METHOD("body_set_collision_layer", "body", "layer"), &Physics2DServer::body_set_collision_layer);
  412. ClassDB::bind_method(D_METHOD("body_get_collision_layer", "body"), &Physics2DServer::body_get_collision_layer);
  413. ClassDB::bind_method(D_METHOD("body_set_collision_mask", "body", "mask"), &Physics2DServer::body_set_collision_mask);
  414. ClassDB::bind_method(D_METHOD("body_get_collision_mask", "body"), &Physics2DServer::body_get_collision_mask);
  415. ClassDB::bind_method(D_METHOD("body_set_param", "body", "param", "value"), &Physics2DServer::body_set_param);
  416. ClassDB::bind_method(D_METHOD("body_get_param", "body", "param"), &Physics2DServer::body_get_param);
  417. ClassDB::bind_method(D_METHOD("body_set_state", "body", "state", "value"), &Physics2DServer::body_set_state);
  418. ClassDB::bind_method(D_METHOD("body_get_state", "body", "state"), &Physics2DServer::body_get_state);
  419. ClassDB::bind_method(D_METHOD("body_apply_impulse", "body", "position", "impulse"), &Physics2DServer::body_apply_impulse);
  420. ClassDB::bind_method(D_METHOD("body_add_force", "body", "offset", "force"), &Physics2DServer::body_add_force);
  421. ClassDB::bind_method(D_METHOD("body_set_axis_velocity", "body", "axis_velocity"), &Physics2DServer::body_set_axis_velocity);
  422. ClassDB::bind_method(D_METHOD("body_add_collision_exception", "body", "excepted_body"), &Physics2DServer::body_add_collision_exception);
  423. ClassDB::bind_method(D_METHOD("body_remove_collision_exception", "body", "excepted_body"), &Physics2DServer::body_remove_collision_exception);
  424. //virtual void body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions)=0;
  425. ClassDB::bind_method(D_METHOD("body_set_max_contacts_reported", "body", "amount"), &Physics2DServer::body_set_max_contacts_reported);
  426. ClassDB::bind_method(D_METHOD("body_get_max_contacts_reported", "body"), &Physics2DServer::body_get_max_contacts_reported);
  427. ClassDB::bind_method(D_METHOD("body_set_omit_force_integration", "body", "enable"), &Physics2DServer::body_set_omit_force_integration);
  428. ClassDB::bind_method(D_METHOD("body_is_omitting_force_integration", "body"), &Physics2DServer::body_is_omitting_force_integration);
  429. ClassDB::bind_method(D_METHOD("body_set_force_integration_callback", "body", "receiver", "method", "userdata"), &Physics2DServer::body_set_force_integration_callback, DEFVAL(Variant()));
  430. ClassDB::bind_method(D_METHOD("body_test_motion", "body", "from", "motion", "margin", "result"), &Physics2DServer::_body_test_motion, DEFVAL(0.08), DEFVAL(Variant()));
  431. ClassDB::bind_method(D_METHOD("body_get_direct_state", "body"), &Physics2DServer::body_get_direct_state);
  432. /* JOINT API */
  433. ClassDB::bind_method(D_METHOD("joint_set_param", "joint", "param", "value"), &Physics2DServer::joint_set_param);
  434. ClassDB::bind_method(D_METHOD("joint_get_param", "joint", "param"), &Physics2DServer::joint_get_param);
  435. ClassDB::bind_method(D_METHOD("pin_joint_create", "anchor", "body_a", "body_b"), &Physics2DServer::pin_joint_create, DEFVAL(RID()));
  436. ClassDB::bind_method(D_METHOD("groove_joint_create", "groove1_a", "groove2_a", "anchor_b", "body_a", "body_b"), &Physics2DServer::groove_joint_create, DEFVAL(RID()), DEFVAL(RID()));
  437. ClassDB::bind_method(D_METHOD("damped_spring_joint_create", "anchor_a", "anchor_b", "body_a", "body_b"), &Physics2DServer::damped_spring_joint_create, DEFVAL(RID()));
  438. ClassDB::bind_method(D_METHOD("damped_string_joint_set_param", "joint", "param", "value"), &Physics2DServer::damped_string_joint_set_param);
  439. ClassDB::bind_method(D_METHOD("damped_string_joint_get_param", "joint", "param"), &Physics2DServer::damped_string_joint_get_param);
  440. ClassDB::bind_method(D_METHOD("joint_get_type", "joint"), &Physics2DServer::joint_get_type);
  441. ClassDB::bind_method(D_METHOD("free_rid", "rid"), &Physics2DServer::free);
  442. ClassDB::bind_method(D_METHOD("set_active", "active"), &Physics2DServer::set_active);
  443. ClassDB::bind_method(D_METHOD("get_process_info", "process_info"), &Physics2DServer::get_process_info);
  444. //ClassDB::bind_method(D_METHOD("init"),&Physics2DServer::init);
  445. //ClassDB::bind_method(D_METHOD("step"),&Physics2DServer::step);
  446. //ClassDB::bind_method(D_METHOD("sync"),&Physics2DServer::sync);
  447. //ClassDB::bind_method(D_METHOD("flush_queries"),&Physics2DServer::flush_queries);
  448. BIND_ENUM_CONSTANT(SPACE_PARAM_CONTACT_RECYCLE_RADIUS);
  449. BIND_ENUM_CONSTANT(SPACE_PARAM_CONTACT_MAX_SEPARATION);
  450. BIND_ENUM_CONSTANT(SPACE_PARAM_BODY_MAX_ALLOWED_PENETRATION);
  451. BIND_ENUM_CONSTANT(SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD);
  452. BIND_ENUM_CONSTANT(SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD);
  453. BIND_ENUM_CONSTANT(SPACE_PARAM_BODY_TIME_TO_SLEEP);
  454. BIND_ENUM_CONSTANT(SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS);
  455. BIND_ENUM_CONSTANT(SHAPE_LINE);
  456. BIND_ENUM_CONSTANT(SHAPE_SEGMENT);
  457. BIND_ENUM_CONSTANT(SHAPE_CIRCLE);
  458. BIND_ENUM_CONSTANT(SHAPE_RECTANGLE);
  459. BIND_ENUM_CONSTANT(SHAPE_CAPSULE);
  460. BIND_ENUM_CONSTANT(SHAPE_CONVEX_POLYGON);
  461. BIND_ENUM_CONSTANT(SHAPE_CONCAVE_POLYGON);
  462. BIND_ENUM_CONSTANT(SHAPE_CUSTOM);
  463. BIND_ENUM_CONSTANT(AREA_PARAM_GRAVITY);
  464. BIND_ENUM_CONSTANT(AREA_PARAM_GRAVITY_VECTOR);
  465. BIND_ENUM_CONSTANT(AREA_PARAM_GRAVITY_IS_POINT);
  466. BIND_ENUM_CONSTANT(AREA_PARAM_GRAVITY_DISTANCE_SCALE);
  467. BIND_ENUM_CONSTANT(AREA_PARAM_GRAVITY_POINT_ATTENUATION);
  468. BIND_ENUM_CONSTANT(AREA_PARAM_LINEAR_DAMP);
  469. BIND_ENUM_CONSTANT(AREA_PARAM_ANGULAR_DAMP);
  470. BIND_ENUM_CONSTANT(AREA_PARAM_PRIORITY);
  471. BIND_ENUM_CONSTANT(AREA_SPACE_OVERRIDE_DISABLED);
  472. BIND_ENUM_CONSTANT(AREA_SPACE_OVERRIDE_COMBINE);
  473. BIND_ENUM_CONSTANT(AREA_SPACE_OVERRIDE_COMBINE_REPLACE);
  474. BIND_ENUM_CONSTANT(AREA_SPACE_OVERRIDE_REPLACE);
  475. BIND_ENUM_CONSTANT(AREA_SPACE_OVERRIDE_REPLACE_COMBINE);
  476. BIND_ENUM_CONSTANT(BODY_MODE_STATIC);
  477. BIND_ENUM_CONSTANT(BODY_MODE_KINEMATIC);
  478. BIND_ENUM_CONSTANT(BODY_MODE_RIGID);
  479. BIND_ENUM_CONSTANT(BODY_MODE_CHARACTER);
  480. BIND_ENUM_CONSTANT(BODY_PARAM_BOUNCE);
  481. BIND_ENUM_CONSTANT(BODY_PARAM_FRICTION);
  482. BIND_ENUM_CONSTANT(BODY_PARAM_MASS);
  483. BIND_ENUM_CONSTANT(BODY_PARAM_INERTIA);
  484. BIND_ENUM_CONSTANT(BODY_PARAM_GRAVITY_SCALE);
  485. BIND_ENUM_CONSTANT(BODY_PARAM_LINEAR_DAMP);
  486. BIND_ENUM_CONSTANT(BODY_PARAM_ANGULAR_DAMP);
  487. BIND_ENUM_CONSTANT(BODY_PARAM_MAX);
  488. BIND_ENUM_CONSTANT(BODY_STATE_TRANSFORM);
  489. BIND_ENUM_CONSTANT(BODY_STATE_LINEAR_VELOCITY);
  490. BIND_ENUM_CONSTANT(BODY_STATE_ANGULAR_VELOCITY);
  491. BIND_ENUM_CONSTANT(BODY_STATE_SLEEPING);
  492. BIND_ENUM_CONSTANT(BODY_STATE_CAN_SLEEP);
  493. BIND_ENUM_CONSTANT(JOINT_PIN);
  494. BIND_ENUM_CONSTANT(JOINT_GROOVE);
  495. BIND_ENUM_CONSTANT(JOINT_DAMPED_SPRING);
  496. BIND_ENUM_CONSTANT(DAMPED_STRING_REST_LENGTH);
  497. BIND_ENUM_CONSTANT(DAMPED_STRING_STIFFNESS);
  498. BIND_ENUM_CONSTANT(DAMPED_STRING_DAMPING);
  499. BIND_ENUM_CONSTANT(CCD_MODE_DISABLED);
  500. BIND_ENUM_CONSTANT(CCD_MODE_CAST_RAY);
  501. BIND_ENUM_CONSTANT(CCD_MODE_CAST_SHAPE);
  502. //BIND_ENUM_CONSTANT( TYPE_BODY );
  503. //BIND_ENUM_CONSTANT( TYPE_AREA );
  504. BIND_ENUM_CONSTANT(AREA_BODY_ADDED);
  505. BIND_ENUM_CONSTANT(AREA_BODY_REMOVED);
  506. BIND_ENUM_CONSTANT(INFO_ACTIVE_OBJECTS);
  507. BIND_ENUM_CONSTANT(INFO_COLLISION_PAIRS);
  508. BIND_ENUM_CONSTANT(INFO_ISLAND_COUNT);
  509. }
  510. Physics2DServer::Physics2DServer() {
  511. //ERR_FAIL_COND( singleton!=NULL );
  512. singleton = this;
  513. }
  514. Physics2DServer::~Physics2DServer() {
  515. singleton = NULL;
  516. }