jolt_concave_polygon_shape_3d.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**************************************************************************/
  2. /* jolt_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 "jolt_concave_polygon_shape_3d.h"
  31. #include "../jolt_project_settings.h"
  32. #include "../misc/jolt_type_conversions.h"
  33. #include "Jolt/Physics/Collision/Shape/MeshShape.h"
  34. JPH::ShapeRefC JoltConcavePolygonShape3D::_build() const {
  35. const int vertex_count = (int)faces.size();
  36. const int face_count = vertex_count / 3;
  37. const int excess_vertex_count = vertex_count % 3;
  38. if (unlikely(vertex_count == 0)) {
  39. return nullptr;
  40. }
  41. ERR_FAIL_COND_V_MSG(vertex_count < 3, nullptr, vformat("Failed to build Jolt Physics concave polygon shape with %s. It must have a vertex count of at least 3. This shape belongs to %s.", to_string(), _owners_to_string()));
  42. ERR_FAIL_COND_V_MSG(excess_vertex_count != 0, nullptr, vformat("Failed to build Jolt Physics concave polygon shape with %s. It must have a vertex count that is divisible by 3. This shape belongs to %s.", to_string(), _owners_to_string()));
  43. JPH::TriangleList jolt_faces;
  44. jolt_faces.reserve((size_t)face_count);
  45. const Vector3 *faces_begin = &faces[0];
  46. const Vector3 *faces_end = faces_begin + vertex_count;
  47. JPH::uint32 triangle_index = 0;
  48. for (const Vector3 *vertex = faces_begin; vertex != faces_end; vertex += 3) {
  49. const Vector3 *v0 = vertex + 0;
  50. const Vector3 *v1 = vertex + 1;
  51. const Vector3 *v2 = vertex + 2;
  52. // Jolt uses a different winding order, so we swizzle the vertices to account for that.
  53. jolt_faces.emplace_back(
  54. JPH::Float3((float)v2->x, (float)v2->y, (float)v2->z),
  55. JPH::Float3((float)v1->x, (float)v1->y, (float)v1->z),
  56. JPH::Float3((float)v0->x, (float)v0->y, (float)v0->z),
  57. 0,
  58. triangle_index++);
  59. }
  60. JPH::MeshShapeSettings shape_settings(jolt_faces);
  61. shape_settings.mActiveEdgeCosThresholdAngle = JoltProjectSettings::get_active_edge_threshold();
  62. shape_settings.mPerTriangleUserData = JoltProjectSettings::enable_ray_cast_face_index();
  63. const JPH::ShapeSettings::ShapeResult shape_result = shape_settings.Create();
  64. ERR_FAIL_COND_V_MSG(shape_result.HasError(), nullptr, vformat("Failed to build Jolt Physics concave polygon shape with %s. It returned the following error: '%s'. This shape belongs to %s.", to_string(), to_godot(shape_result.GetError()), _owners_to_string()));
  65. return JoltShape3D::with_double_sided(shape_result.Get(), back_face_collision);
  66. }
  67. AABB JoltConcavePolygonShape3D::_calculate_aabb() const {
  68. AABB result;
  69. for (int i = 0; i < faces.size(); ++i) {
  70. const Vector3 &vertex = faces[i];
  71. if (i == 0) {
  72. result.position = vertex;
  73. } else {
  74. result.expand_to(vertex);
  75. }
  76. }
  77. return result;
  78. }
  79. Variant JoltConcavePolygonShape3D::get_data() const {
  80. Dictionary data;
  81. data["faces"] = faces;
  82. data["backface_collision"] = back_face_collision;
  83. return data;
  84. }
  85. void JoltConcavePolygonShape3D::set_data(const Variant &p_data) {
  86. ERR_FAIL_COND(p_data.get_type() != Variant::DICTIONARY);
  87. const Dictionary data = p_data;
  88. const Variant maybe_faces = data.get("faces", Variant());
  89. ERR_FAIL_COND(maybe_faces.get_type() != Variant::PACKED_VECTOR3_ARRAY);
  90. const Variant maybe_back_face_collision = data.get("backface_collision", Variant());
  91. ERR_FAIL_COND(maybe_back_face_collision.get_type() != Variant::BOOL);
  92. faces = maybe_faces;
  93. back_face_collision = maybe_back_face_collision;
  94. aabb = _calculate_aabb();
  95. destroy();
  96. }
  97. String JoltConcavePolygonShape3D::to_string() const {
  98. return vformat("{vertex_count=%d}", faces.size());
  99. }