2
0

box_shape_3d.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**************************************************************************/
  2. /* box_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 "box_shape_3d.h"
  31. #include "servers/physics_server_3d.h"
  32. Vector<Vector3> BoxShape3D::get_debug_mesh_lines() const {
  33. Vector<Vector3> lines;
  34. AABB aabb;
  35. aabb.position = -size / 2;
  36. aabb.size = size;
  37. for (int i = 0; i < 12; i++) {
  38. Vector3 a, b;
  39. aabb.get_edge(i, a, b);
  40. lines.push_back(a);
  41. lines.push_back(b);
  42. }
  43. return lines;
  44. }
  45. real_t BoxShape3D::get_enclosing_radius() const {
  46. return size.length() / 2;
  47. }
  48. void BoxShape3D::_update_shape() {
  49. PhysicsServer3D::get_singleton()->shape_set_data(get_shape(), size / 2);
  50. Shape3D::_update_shape();
  51. }
  52. #ifndef DISABLE_DEPRECATED
  53. bool BoxShape3D::_set(const StringName &p_name, const Variant &p_value) {
  54. if (p_name == "extents") { // Compatibility with Godot 3.x.
  55. // Convert to `size`, twice as big.
  56. set_size((Vector3)p_value * 2);
  57. return true;
  58. }
  59. return false;
  60. }
  61. bool BoxShape3D::_get(const StringName &p_name, Variant &r_property) const {
  62. if (p_name == "extents") { // Compatibility with Godot 3.x.
  63. // Convert to `extents`, half as big.
  64. r_property = size / 2;
  65. return true;
  66. }
  67. return false;
  68. }
  69. #endif // DISABLE_DEPRECATED
  70. void BoxShape3D::set_size(const Vector3 &p_size) {
  71. ERR_FAIL_COND_MSG(p_size.x < 0 || p_size.y < 0 || p_size.z < 0, "BoxShape3D size cannot be negative.");
  72. size = p_size;
  73. _update_shape();
  74. emit_changed();
  75. }
  76. Vector3 BoxShape3D::get_size() const {
  77. return size;
  78. }
  79. void BoxShape3D::_bind_methods() {
  80. ClassDB::bind_method(D_METHOD("set_size", "size"), &BoxShape3D::set_size);
  81. ClassDB::bind_method(D_METHOD("get_size"), &BoxShape3D::get_size);
  82. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "size", PROPERTY_HINT_NONE, "suffix:m"), "set_size", "get_size");
  83. }
  84. BoxShape3D::BoxShape3D() :
  85. Shape3D(PhysicsServer3D::get_singleton()->shape_create(PhysicsServer3D::SHAPE_BOX)) {
  86. set_size(Vector3(1, 1, 1));
  87. }