jolt_cylinder_shape_3d.cpp 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**************************************************************************/
  2. /* jolt_cylinder_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_cylinder_shape_3d.h"
  31. #include "../jolt_project_settings.h"
  32. #include "../misc/jolt_type_conversions.h"
  33. #include "Jolt/Physics/Collision/Shape/CylinderShape.h"
  34. JPH::ShapeRefC JoltCylinderShape3D::_build() const {
  35. const float half_height = height / 2.0f;
  36. const float min_half_extent = MIN(half_height, radius);
  37. const float actual_margin = MIN(margin, min_half_extent * JoltProjectSettings::get_collision_margin_fraction());
  38. const JPH::CylinderShapeSettings shape_settings(half_height, radius, actual_margin);
  39. const JPH::ShapeSettings::ShapeResult shape_result = shape_settings.Create();
  40. ERR_FAIL_COND_V_MSG(shape_result.HasError(), nullptr, vformat("Failed to build Jolt Physics cylinder shape with %s. It returned the following error: '%s'. This shape belongs to %s.", to_string(), to_godot(shape_result.GetError()), _owners_to_string()));
  41. return shape_result.Get();
  42. }
  43. Variant JoltCylinderShape3D::get_data() const {
  44. Dictionary data;
  45. data["height"] = height;
  46. data["radius"] = radius;
  47. return data;
  48. }
  49. void JoltCylinderShape3D::set_data(const Variant &p_data) {
  50. ERR_FAIL_COND(p_data.get_type() != Variant::DICTIONARY);
  51. const Dictionary data = p_data;
  52. const Variant maybe_height = data.get("height", Variant());
  53. ERR_FAIL_COND(maybe_height.get_type() != Variant::FLOAT);
  54. const Variant maybe_radius = data.get("radius", Variant());
  55. ERR_FAIL_COND(maybe_radius.get_type() != Variant::FLOAT);
  56. const float new_height = maybe_height;
  57. const float new_radius = maybe_radius;
  58. if (unlikely(new_height == height && new_radius == radius)) {
  59. return;
  60. }
  61. height = new_height;
  62. radius = new_radius;
  63. destroy();
  64. }
  65. void JoltCylinderShape3D::set_margin(float p_margin) {
  66. if (unlikely(margin == p_margin)) {
  67. return;
  68. }
  69. margin = p_margin;
  70. destroy();
  71. }
  72. AABB JoltCylinderShape3D::get_aabb() const {
  73. const Vector3 half_extents(radius, height / 2.0f, radius);
  74. return AABB(-half_extents, half_extents * 2.0f);
  75. }
  76. String JoltCylinderShape3D::to_string() const {
  77. return vformat("{height=%f radius=%f margin=%f}", height, radius, margin);
  78. }