concave_polygon_shape_3d.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**************************************************************************/
  2. /* concave_polygon_shape_3d.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 "concave_polygon_shape_3d.h"
  31. #include "servers/physics_server_3d.h"
  32. Vector<Vector3> ConcavePolygonShape3D::get_debug_mesh_lines() const {
  33. HashSet<DrawEdge, DrawEdge> edges;
  34. int index_count = faces.size();
  35. ERR_FAIL_COND_V((index_count % 3) != 0, Vector<Vector3>());
  36. const Vector3 *r = faces.ptr();
  37. for (int i = 0; i < index_count; i += 3) {
  38. for (int j = 0; j < 3; j++) {
  39. DrawEdge de(r[i + j], r[i + ((j + 1) % 3)]);
  40. edges.insert(de);
  41. }
  42. }
  43. Vector<Vector3> points;
  44. points.resize(edges.size() * 2);
  45. int idx = 0;
  46. for (const DrawEdge &E : edges) {
  47. points.write[idx + 0] = E.a;
  48. points.write[idx + 1] = E.b;
  49. idx += 2;
  50. }
  51. return points;
  52. }
  53. real_t ConcavePolygonShape3D::get_enclosing_radius() const {
  54. Vector<Vector3> data = get_faces();
  55. const Vector3 *read = data.ptr();
  56. real_t r = 0.0;
  57. for (int i(0); i < data.size(); i++) {
  58. r = MAX(read[i].length_squared(), r);
  59. }
  60. return Math::sqrt(r);
  61. }
  62. void ConcavePolygonShape3D::_update_shape() {
  63. Dictionary d;
  64. d["faces"] = faces;
  65. d["backface_collision"] = backface_collision;
  66. PhysicsServer3D::get_singleton()->shape_set_data(get_shape(), d);
  67. Shape3D::_update_shape();
  68. }
  69. void ConcavePolygonShape3D::set_faces(const Vector<Vector3> &p_faces) {
  70. faces = p_faces;
  71. _update_shape();
  72. emit_changed();
  73. }
  74. Vector<Vector3> ConcavePolygonShape3D::get_faces() const {
  75. return faces;
  76. }
  77. void ConcavePolygonShape3D::set_backface_collision_enabled(bool p_enabled) {
  78. backface_collision = p_enabled;
  79. if (!faces.is_empty()) {
  80. _update_shape();
  81. emit_changed();
  82. }
  83. }
  84. bool ConcavePolygonShape3D::is_backface_collision_enabled() const {
  85. return backface_collision;
  86. }
  87. void ConcavePolygonShape3D::_bind_methods() {
  88. ClassDB::bind_method(D_METHOD("set_faces", "faces"), &ConcavePolygonShape3D::set_faces);
  89. ClassDB::bind_method(D_METHOD("get_faces"), &ConcavePolygonShape3D::get_faces);
  90. ClassDB::bind_method(D_METHOD("set_backface_collision_enabled", "enabled"), &ConcavePolygonShape3D::set_backface_collision_enabled);
  91. ClassDB::bind_method(D_METHOD("is_backface_collision_enabled"), &ConcavePolygonShape3D::is_backface_collision_enabled);
  92. ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR3_ARRAY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "set_faces", "get_faces");
  93. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "backface_collision"), "set_backface_collision_enabled", "is_backface_collision_enabled");
  94. }
  95. ConcavePolygonShape3D::ConcavePolygonShape3D() :
  96. Shape3D(PhysicsServer3D::get_singleton()->shape_create(PhysicsServer3D::SHAPE_CONCAVE_POLYGON)) {
  97. //set_planes(Vector3(1,1,1));
  98. }