box_shape_3d.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 "scene/resources/3d/primitive_meshes.h"
  32. #include "servers/physics_server_3d.h"
  33. Vector<Vector3> BoxShape3D::get_debug_mesh_lines() const {
  34. Vector<Vector3> lines;
  35. AABB aabb;
  36. aabb.position = -size / 2;
  37. aabb.size = size;
  38. for (int i = 0; i < 12; i++) {
  39. Vector3 a, b;
  40. aabb.get_edge(i, a, b);
  41. lines.push_back(a);
  42. lines.push_back(b);
  43. }
  44. return lines;
  45. }
  46. Ref<ArrayMesh> BoxShape3D::get_debug_arraymesh_faces(const Color &p_modulate) const {
  47. Array box_array;
  48. box_array.resize(RS::ARRAY_MAX);
  49. BoxMesh::create_mesh_array(box_array, size);
  50. Vector<Color> colors;
  51. const PackedVector3Array &verts = box_array[RS::ARRAY_VERTEX];
  52. const int32_t verts_size = verts.size();
  53. for (int i = 0; i < verts_size; i++) {
  54. colors.append(p_modulate);
  55. }
  56. Ref<ArrayMesh> box_mesh = memnew(ArrayMesh);
  57. box_array[RS::ARRAY_COLOR] = colors;
  58. box_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, box_array);
  59. return box_mesh;
  60. }
  61. real_t BoxShape3D::get_enclosing_radius() const {
  62. return size.length() / 2;
  63. }
  64. void BoxShape3D::_update_shape() {
  65. PhysicsServer3D::get_singleton()->shape_set_data(get_shape(), size / 2);
  66. Shape3D::_update_shape();
  67. }
  68. #ifndef DISABLE_DEPRECATED
  69. bool BoxShape3D::_set(const StringName &p_name, const Variant &p_value) {
  70. if (p_name == "extents") { // Compatibility with Godot 3.x.
  71. // Convert to `size`, twice as big.
  72. set_size((Vector3)p_value * 2);
  73. return true;
  74. }
  75. return false;
  76. }
  77. bool BoxShape3D::_get(const StringName &p_name, Variant &r_property) const {
  78. if (p_name == "extents") { // Compatibility with Godot 3.x.
  79. // Convert to `extents`, half as big.
  80. r_property = size / 2;
  81. return true;
  82. }
  83. return false;
  84. }
  85. #endif // DISABLE_DEPRECATED
  86. void BoxShape3D::set_size(const Vector3 &p_size) {
  87. ERR_FAIL_COND_MSG(p_size.x < 0 || p_size.y < 0 || p_size.z < 0, "BoxShape3D size cannot be negative.");
  88. size = p_size;
  89. _update_shape();
  90. emit_changed();
  91. }
  92. Vector3 BoxShape3D::get_size() const {
  93. return size;
  94. }
  95. void BoxShape3D::_bind_methods() {
  96. ClassDB::bind_method(D_METHOD("set_size", "size"), &BoxShape3D::set_size);
  97. ClassDB::bind_method(D_METHOD("get_size"), &BoxShape3D::get_size);
  98. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "size", PROPERTY_HINT_NONE, "suffix:m"), "set_size", "get_size");
  99. }
  100. BoxShape3D::BoxShape3D() :
  101. Shape3D(PhysicsServer3D::get_singleton()->shape_create(PhysicsServer3D::SHAPE_BOX)) {
  102. set_size(Vector3(1, 1, 1));
  103. }