jolt_separation_ray_shape_3d.cpp 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**************************************************************************/
  2. /* jolt_separation_ray_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_separation_ray_shape_3d.h"
  31. #include "../misc/jolt_type_conversions.h"
  32. #include "jolt_custom_ray_shape.h"
  33. JPH::ShapeRefC JoltSeparationRayShape3D::_build() const {
  34. ERR_FAIL_COND_V_MSG(length <= 0.0f, nullptr, vformat("Failed to build Jolt Physics separation ray shape with %s. Its length must be greater than 0. This shape belongs to %s.", to_string(), _owners_to_string()));
  35. const JoltCustomRayShapeSettings shape_settings(length, slide_on_slope);
  36. const JPH::ShapeSettings::ShapeResult shape_result = shape_settings.Create();
  37. ERR_FAIL_COND_V_MSG(shape_result.HasError(), nullptr, vformat("Failed to build Jolt Physics separation ray shape with %s. It returned the following error: '%s'. This shape belongs to %s.", to_string(), to_godot(shape_result.GetError()), _owners_to_string()));
  38. return shape_result.Get();
  39. }
  40. Variant JoltSeparationRayShape3D::get_data() const {
  41. Dictionary data;
  42. data["length"] = length;
  43. data["slide_on_slope"] = slide_on_slope;
  44. return data;
  45. }
  46. void JoltSeparationRayShape3D::set_data(const Variant &p_data) {
  47. ERR_FAIL_COND(p_data.get_type() != Variant::DICTIONARY);
  48. const Dictionary data = p_data;
  49. const Variant maybe_length = data.get("length", Variant());
  50. ERR_FAIL_COND(maybe_length.get_type() != Variant::FLOAT);
  51. const Variant maybe_slide_on_slope = data.get("slide_on_slope", Variant());
  52. ERR_FAIL_COND(maybe_slide_on_slope.get_type() != Variant::BOOL);
  53. const float new_length = maybe_length;
  54. const bool new_slide_on_slope = maybe_slide_on_slope;
  55. if (unlikely(new_length == length && new_slide_on_slope == slide_on_slope)) {
  56. return;
  57. }
  58. length = new_length;
  59. slide_on_slope = new_slide_on_slope;
  60. destroy();
  61. }
  62. AABB JoltSeparationRayShape3D::get_aabb() const {
  63. constexpr float size_xy = 0.1f;
  64. constexpr float half_size_xy = size_xy / 2.0f;
  65. return AABB(Vector3(-half_size_xy, -half_size_xy, 0.0f), Vector3(size_xy, size_xy, length));
  66. }
  67. String JoltSeparationRayShape3D::to_string() const {
  68. return vformat("{length=%f slide_on_slope=%s}", length, slide_on_slope);
  69. }