2
0

jolt_sphere_shape_3d.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**************************************************************************/
  2. /* jolt_sphere_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_sphere_shape_3d.h"
  31. #include "../misc/jolt_type_conversions.h"
  32. #include "Jolt/Physics/Collision/Shape/SphereShape.h"
  33. JPH::ShapeRefC JoltSphereShape3D::_build() const {
  34. ERR_FAIL_COND_V_MSG(radius <= 0.0f, nullptr, vformat("Failed to build Jolt Physics sphere shape with %s. Its radius must be greater than 0. This shape belongs to %s.", to_string(), _owners_to_string()));
  35. const JPH::SphereShapeSettings shape_settings(radius);
  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 sphere 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 JoltSphereShape3D::get_data() const {
  41. return radius;
  42. }
  43. void JoltSphereShape3D::set_data(const Variant &p_data) {
  44. ERR_FAIL_COND(p_data.get_type() != Variant::FLOAT);
  45. const float new_radius = p_data;
  46. if (unlikely(new_radius == radius)) {
  47. return;
  48. }
  49. radius = new_radius;
  50. destroy();
  51. }
  52. AABB JoltSphereShape3D::get_aabb() const {
  53. const Vector3 half_extents(radius, radius, radius);
  54. return AABB(-half_extents, half_extents * 2.0f);
  55. }
  56. String JoltSphereShape3D::to_string() const {
  57. return vformat("{radius=%f}", radius);
  58. }