height_map_shape.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /**************************************************************************/
  2. /* height_map_shape.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 "height_map_shape.h"
  31. #include "servers/physics_server.h"
  32. Vector<Vector3> HeightMapShape::get_debug_mesh_lines() {
  33. Vector<Vector3> points;
  34. if ((map_width != 0) && (map_depth != 0)) {
  35. // This will be slow for large maps...
  36. // also we'll have to figure out how well bullet centers this shape...
  37. Vector2 size(map_width - 1, map_depth - 1);
  38. Vector2 start = size * -0.5;
  39. PoolRealArray::Read r = map_data.read();
  40. // reserve some memory for our points..
  41. points.resize(((map_width - 1) * map_depth * 2) + (map_width * (map_depth - 1) * 2) + (map_width - 1) * (map_depth - 1) * 2);
  42. // now set our points
  43. int r_offset = 0;
  44. int w_offset = 0;
  45. for (int d = 0; d < map_depth; d++) {
  46. Vector3 height(start.x, 0.0, start.y);
  47. for (int w = 0; w < map_width; w++) {
  48. height.y = r[r_offset++];
  49. if (w != map_width - 1) {
  50. points.write[w_offset++] = height;
  51. points.write[w_offset++] = Vector3(height.x + 1.0, r[r_offset], height.z);
  52. }
  53. if (d != map_depth - 1) {
  54. points.write[w_offset++] = height;
  55. points.write[w_offset++] = Vector3(height.x, r[r_offset + map_width - 1], height.z + 1.0);
  56. }
  57. if ((w != map_width - 1) && (d != map_depth - 1)) {
  58. points.write[w_offset++] = Vector3(height.x + 1.0, r[r_offset], height.z);
  59. points.write[w_offset++] = Vector3(height.x, r[r_offset + map_width - 1], height.z + 1.0);
  60. }
  61. height.x += 1.0;
  62. }
  63. start.y += 1.0;
  64. }
  65. }
  66. return points;
  67. }
  68. real_t HeightMapShape::get_enclosing_radius() const {
  69. return Vector3(real_t(map_width), max_height - min_height, real_t(map_depth)).length();
  70. }
  71. void HeightMapShape::_update_shape() {
  72. Dictionary d;
  73. d["width"] = map_width;
  74. d["depth"] = map_depth;
  75. d["heights"] = map_data;
  76. d["min_height"] = min_height;
  77. d["max_height"] = max_height;
  78. PhysicsServer::get_singleton()->shape_set_data(get_shape(), d);
  79. Shape::_update_shape();
  80. }
  81. void HeightMapShape::set_map_width(int p_new) {
  82. if (p_new < 1) {
  83. // ignore
  84. } else if (map_width != p_new) {
  85. int was_size = map_width * map_depth;
  86. map_width = p_new;
  87. int new_size = map_width * map_depth;
  88. map_data.resize(map_width * map_depth);
  89. PoolRealArray::Write w = map_data.write();
  90. while (was_size < new_size) {
  91. w[was_size++] = 0.0;
  92. }
  93. _update_shape();
  94. notify_change_to_owners();
  95. _change_notify("map_width");
  96. _change_notify("map_data");
  97. }
  98. }
  99. int HeightMapShape::get_map_width() const {
  100. return map_width;
  101. }
  102. void HeightMapShape::set_map_depth(int p_new) {
  103. if (p_new < 1) {
  104. // ignore
  105. } else if (map_depth != p_new) {
  106. int was_size = map_width * map_depth;
  107. map_depth = p_new;
  108. int new_size = map_width * map_depth;
  109. map_data.resize(new_size);
  110. PoolRealArray::Write w = map_data.write();
  111. while (was_size < new_size) {
  112. w[was_size++] = 0.0;
  113. }
  114. _update_shape();
  115. notify_change_to_owners();
  116. _change_notify("map_depth");
  117. _change_notify("map_data");
  118. }
  119. }
  120. int HeightMapShape::get_map_depth() const {
  121. return map_depth;
  122. }
  123. void HeightMapShape::set_map_data(PoolRealArray p_new) {
  124. int size = (map_width * map_depth);
  125. if (p_new.size() != size) {
  126. // fail
  127. return;
  128. }
  129. // copy
  130. PoolRealArray::Write w = map_data.write();
  131. PoolRealArray::Read r = p_new.read();
  132. for (int i = 0; i < size; i++) {
  133. float val = r[i];
  134. w[i] = val;
  135. if (i == 0) {
  136. min_height = val;
  137. max_height = val;
  138. } else {
  139. if (min_height > val) {
  140. min_height = val;
  141. }
  142. if (max_height < val) {
  143. max_height = val;
  144. }
  145. }
  146. }
  147. _update_shape();
  148. notify_change_to_owners();
  149. _change_notify("map_data");
  150. }
  151. PoolRealArray HeightMapShape::get_map_data() const {
  152. return map_data;
  153. }
  154. void HeightMapShape::_bind_methods() {
  155. ClassDB::bind_method(D_METHOD("set_map_width", "width"), &HeightMapShape::set_map_width);
  156. ClassDB::bind_method(D_METHOD("get_map_width"), &HeightMapShape::get_map_width);
  157. ClassDB::bind_method(D_METHOD("set_map_depth", "height"), &HeightMapShape::set_map_depth);
  158. ClassDB::bind_method(D_METHOD("get_map_depth"), &HeightMapShape::get_map_depth);
  159. ClassDB::bind_method(D_METHOD("set_map_data", "data"), &HeightMapShape::set_map_data);
  160. ClassDB::bind_method(D_METHOD("get_map_data"), &HeightMapShape::get_map_data);
  161. ADD_PROPERTY(PropertyInfo(Variant::INT, "map_width", PROPERTY_HINT_RANGE, "0.001,100,0.001,or_greater"), "set_map_width", "get_map_width");
  162. ADD_PROPERTY(PropertyInfo(Variant::INT, "map_depth", PROPERTY_HINT_RANGE, "0.001,100,0.001,or_greater"), "set_map_depth", "get_map_depth");
  163. ADD_PROPERTY(PropertyInfo(Variant::POOL_REAL_ARRAY, "map_data"), "set_map_data", "get_map_data");
  164. }
  165. HeightMapShape::HeightMapShape() :
  166. Shape(RID_PRIME(PhysicsServer::get_singleton()->shape_create(PhysicsServer::SHAPE_HEIGHTMAP))) {
  167. map_width = 2;
  168. map_depth = 2;
  169. map_data.resize(map_width * map_depth);
  170. PoolRealArray::Write w = map_data.write();
  171. w[0] = 0.0;
  172. w[1] = 0.0;
  173. w[2] = 0.0;
  174. w[3] = 0.0;
  175. min_height = 0.0;
  176. max_height = 0.0;
  177. _update_shape();
  178. }