shape_2d.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*************************************************************************/
  2. /* shape_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "shape_2d.h"
  31. #include "core/engine.h"
  32. #include "core/project_settings.h"
  33. #include "servers/physics_2d_server.h"
  34. RID Shape2D::get_rid() const {
  35. return shape;
  36. }
  37. void Shape2D::set_custom_solver_bias(real_t p_bias) {
  38. custom_bias = p_bias;
  39. Physics2DServer::get_singleton()->shape_set_custom_solver_bias(shape, custom_bias);
  40. }
  41. real_t Shape2D::get_custom_solver_bias() const {
  42. return custom_bias;
  43. }
  44. bool Shape2D::collide_with_motion(const Transform2D &p_local_xform, const Vector2 &p_local_motion, const Ref<Shape2D> &p_shape, const Transform2D &p_shape_xform, const Vector2 &p_shape_motion) {
  45. ERR_FAIL_COND_V(p_shape.is_null(), false);
  46. int r;
  47. return Physics2DServer::get_singleton()->shape_collide(get_rid(), p_local_xform, p_local_motion, p_shape->get_rid(), p_shape_xform, p_shape_motion, NULL, 0, r);
  48. }
  49. bool Shape2D::collide(const Transform2D &p_local_xform, const Ref<Shape2D> &p_shape, const Transform2D &p_shape_xform) {
  50. ERR_FAIL_COND_V(p_shape.is_null(), false);
  51. int r;
  52. return Physics2DServer::get_singleton()->shape_collide(get_rid(), p_local_xform, Vector2(), p_shape->get_rid(), p_shape_xform, Vector2(), NULL, 0, r);
  53. }
  54. Array Shape2D::collide_with_motion_and_get_contacts(const Transform2D &p_local_xform, const Vector2 &p_local_motion, const Ref<Shape2D> &p_shape, const Transform2D &p_shape_xform, const Vector2 &p_shape_motion) {
  55. ERR_FAIL_COND_V(p_shape.is_null(), Array());
  56. const int max_contacts = 16;
  57. Vector2 result[max_contacts * 2];
  58. int contacts = 0;
  59. if (!Physics2DServer::get_singleton()->shape_collide(get_rid(), p_local_xform, p_local_motion, p_shape->get_rid(), p_shape_xform, p_shape_motion, result, max_contacts, contacts))
  60. return Array();
  61. Array results;
  62. results.resize(contacts * 2);
  63. for (int i = 0; i < contacts * 2; i++) {
  64. results[i] = result[i];
  65. }
  66. return results;
  67. }
  68. Array Shape2D::collide_and_get_contacts(const Transform2D &p_local_xform, const Ref<Shape2D> &p_shape, const Transform2D &p_shape_xform) {
  69. ERR_FAIL_COND_V(p_shape.is_null(), Array());
  70. const int max_contacts = 16;
  71. Vector2 result[max_contacts * 2];
  72. int contacts = 0;
  73. if (!Physics2DServer::get_singleton()->shape_collide(get_rid(), p_local_xform, Vector2(), p_shape->get_rid(), p_shape_xform, Vector2(), result, max_contacts, contacts))
  74. return Array();
  75. Array results;
  76. results.resize(contacts * 2);
  77. for (int i = 0; i < contacts * 2; i++) {
  78. results[i] = result[i];
  79. }
  80. return results;
  81. }
  82. void Shape2D::_bind_methods() {
  83. ClassDB::bind_method(D_METHOD("set_custom_solver_bias", "bias"), &Shape2D::set_custom_solver_bias);
  84. ClassDB::bind_method(D_METHOD("get_custom_solver_bias"), &Shape2D::get_custom_solver_bias);
  85. ClassDB::bind_method(D_METHOD("collide", "local_xform", "with_shape", "shape_xform"), &Shape2D::collide);
  86. ClassDB::bind_method(D_METHOD("collide_with_motion", "local_xform", "local_motion", "with_shape", "shape_xform", "shape_motion"), &Shape2D::collide_with_motion);
  87. ClassDB::bind_method(D_METHOD("collide_and_get_contacts", "local_xform", "with_shape", "shape_xform"), &Shape2D::collide_and_get_contacts);
  88. ClassDB::bind_method(D_METHOD("collide_with_motion_and_get_contacts", "local_xform", "local_motion", "with_shape", "shape_xform", "shape_motion"), &Shape2D::collide_with_motion_and_get_contacts);
  89. ClassDB::bind_method(D_METHOD("draw", "canvas_item", "color"), &Shape2D::draw);
  90. ADD_PROPERTY(PropertyInfo(Variant::REAL, "custom_solver_bias", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_custom_solver_bias", "get_custom_solver_bias");
  91. }
  92. bool Shape2D::is_collision_outline_enabled() {
  93. #ifdef TOOLS_ENABLED
  94. if (Engine::get_singleton()->is_editor_hint()) {
  95. return true;
  96. }
  97. #endif
  98. return GLOBAL_DEF("debug/shapes/collision/draw_2d_outlines", true);
  99. }
  100. Shape2D::Shape2D(const RID &p_rid) {
  101. shape = p_rid;
  102. custom_bias = 0;
  103. }
  104. Shape2D::~Shape2D() {
  105. Physics2DServer::get_singleton()->free(shape);
  106. }