1 /*************************************************************************/
2 /* height_map_shape.cpp */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
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
31 #include "height_map_shape.h"
32 #include "servers/physics_server.h"
33
get_debug_mesh_lines()34 Vector<Vector3> HeightMapShape::get_debug_mesh_lines() {
35 Vector<Vector3> points;
36
37 if ((map_width != 0) && (map_depth != 0)) {
38
39 // This will be slow for large maps...
40 // also we'll have to figure out how well bullet centers this shape...
41
42 Vector2 size(map_width - 1, map_depth - 1);
43 Vector2 start = size * -0.5;
44
45 PoolRealArray::Read r = map_data.read();
46
47 // reserve some memory for our points..
48 points.resize(((map_width - 1) * map_depth * 2) + (map_width * (map_depth - 1) * 2));
49
50 // now set our points
51 int r_offset = 0;
52 int w_offset = 0;
53 for (int d = 0; d < map_depth; d++) {
54 Vector3 height(start.x, 0.0, start.y);
55
56 for (int w = 0; w < map_width; w++) {
57 height.y = r[r_offset++];
58
59 if (w != map_width - 1) {
60 points.write[w_offset++] = height;
61 points.write[w_offset++] = Vector3(height.x + 1.0, r[r_offset], height.z);
62 }
63
64 if (d != map_depth - 1) {
65 points.write[w_offset++] = height;
66 points.write[w_offset++] = Vector3(height.x, r[r_offset + map_width - 1], height.z + 1.0);
67 }
68
69 height.x += 1.0;
70 }
71
72 start.y += 1.0;
73 }
74 }
75
76 return points;
77 }
78
_update_shape()79 void HeightMapShape::_update_shape() {
80
81 Dictionary d;
82 d["width"] = map_width;
83 d["depth"] = map_depth;
84 d["heights"] = map_data;
85 d["min_height"] = min_height;
86 d["max_height"] = max_height;
87 PhysicsServer::get_singleton()->shape_set_data(get_shape(), d);
88 Shape::_update_shape();
89 }
90
set_map_width(int p_new)91 void HeightMapShape::set_map_width(int p_new) {
92 if (p_new < 1) {
93 // ignore
94 } else if (map_width != p_new) {
95 int was_size = map_width * map_depth;
96 map_width = p_new;
97
98 int new_size = map_width * map_depth;
99 map_data.resize(map_width * map_depth);
100
101 PoolRealArray::Write w = map_data.write();
102 while (was_size < new_size) {
103 w[was_size++] = 0.0;
104 }
105
106 _update_shape();
107 notify_change_to_owners();
108 _change_notify("map_width");
109 _change_notify("map_data");
110 }
111 }
112
get_map_width() const113 int HeightMapShape::get_map_width() const {
114 return map_width;
115 }
116
set_map_depth(int p_new)117 void HeightMapShape::set_map_depth(int p_new) {
118 if (p_new < 1) {
119 // ignore
120 } else if (map_depth != p_new) {
121 int was_size = map_width * map_depth;
122 map_depth = p_new;
123
124 int new_size = map_width * map_depth;
125 map_data.resize(new_size);
126
127 PoolRealArray::Write w = map_data.write();
128 while (was_size < new_size) {
129 w[was_size++] = 0.0;
130 }
131
132 _update_shape();
133 notify_change_to_owners();
134 _change_notify("map_depth");
135 _change_notify("map_data");
136 }
137 }
138
get_map_depth() const139 int HeightMapShape::get_map_depth() const {
140 return map_depth;
141 }
142
set_map_data(PoolRealArray p_new)143 void HeightMapShape::set_map_data(PoolRealArray p_new) {
144 int size = (map_width * map_depth);
145 if (p_new.size() != size) {
146 // fail
147 return;
148 }
149
150 // copy
151 PoolRealArray::Write w = map_data.write();
152 PoolRealArray::Read r = p_new.read();
153 for (int i = 0; i < size; i++) {
154 float val = r[i];
155 w[i] = val;
156 if (i == 0) {
157 min_height = val;
158 max_height = val;
159 } else {
160 if (min_height > val)
161 min_height = val;
162
163 if (max_height < val)
164 max_height = val;
165 }
166 }
167
168 _update_shape();
169 notify_change_to_owners();
170 _change_notify("map_data");
171 }
172
get_map_data() const173 PoolRealArray HeightMapShape::get_map_data() const {
174 return map_data;
175 }
176
_bind_methods()177 void HeightMapShape::_bind_methods() {
178 ClassDB::bind_method(D_METHOD("set_map_width", "width"), &HeightMapShape::set_map_width);
179 ClassDB::bind_method(D_METHOD("get_map_width"), &HeightMapShape::get_map_width);
180 ClassDB::bind_method(D_METHOD("set_map_depth", "height"), &HeightMapShape::set_map_depth);
181 ClassDB::bind_method(D_METHOD("get_map_depth"), &HeightMapShape::get_map_depth);
182 ClassDB::bind_method(D_METHOD("set_map_data", "data"), &HeightMapShape::set_map_data);
183 ClassDB::bind_method(D_METHOD("get_map_data"), &HeightMapShape::get_map_data);
184
185 ADD_PROPERTY(PropertyInfo(Variant::INT, "map_width", PROPERTY_HINT_RANGE, "1,4096,1"), "set_map_width", "get_map_width");
186 ADD_PROPERTY(PropertyInfo(Variant::INT, "map_depth", PROPERTY_HINT_RANGE, "1,4096,1"), "set_map_depth", "get_map_depth");
187 ADD_PROPERTY(PropertyInfo(Variant::POOL_REAL_ARRAY, "map_data"), "set_map_data", "get_map_data");
188 }
189
HeightMapShape()190 HeightMapShape::HeightMapShape() :
191 Shape(PhysicsServer::get_singleton()->shape_create(PhysicsServer::SHAPE_HEIGHTMAP)) {
192
193 map_width = 2;
194 map_depth = 2;
195 map_data.resize(map_width * map_depth);
196 PoolRealArray::Write w = map_data.write();
197 w[0] = 0.0;
198 w[1] = 0.0;
199 w[2] = 0.0;
200 w[3] = 0.0;
201 min_height = 0.0;
202 max_height = 0.0;
203
204 _update_shape();
205 }
206