| 1 | /**************************************************************************/ |
| 2 | /* godot_shape_2d.h */ |
| 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 | |
| 31 | #ifndef GODOT_SHAPE_2D_H |
| 32 | #define GODOT_SHAPE_2D_H |
| 33 | |
| 34 | #include "servers/physics_server_2d.h" |
| 35 | |
| 36 | class GodotShape2D; |
| 37 | |
| 38 | class GodotShapeOwner2D { |
| 39 | public: |
| 40 | virtual void _shape_changed() = 0; |
| 41 | virtual void remove_shape(GodotShape2D *p_shape) = 0; |
| 42 | |
| 43 | virtual ~GodotShapeOwner2D() {} |
| 44 | }; |
| 45 | |
| 46 | class GodotShape2D { |
| 47 | RID self; |
| 48 | Rect2 aabb; |
| 49 | bool configured = false; |
| 50 | real_t custom_bias = 0.0; |
| 51 | |
| 52 | HashMap<GodotShapeOwner2D *, int> owners; |
| 53 | |
| 54 | protected: |
| 55 | const double segment_is_valid_support_threshold = 0.99998; |
| 56 | const double segment_is_valid_support_threshold_lower = |
| 57 | Math::sqrt(1.0 - segment_is_valid_support_threshold * segment_is_valid_support_threshold); |
| 58 | |
| 59 | void configure(const Rect2 &p_aabb); |
| 60 | |
| 61 | public: |
| 62 | _FORCE_INLINE_ void set_self(const RID &p_self) { self = p_self; } |
| 63 | _FORCE_INLINE_ RID get_self() const { return self; } |
| 64 | |
| 65 | virtual PhysicsServer2D::ShapeType get_type() const = 0; |
| 66 | |
| 67 | _FORCE_INLINE_ Rect2 get_aabb() const { return aabb; } |
| 68 | _FORCE_INLINE_ bool is_configured() const { return configured; } |
| 69 | |
| 70 | virtual bool allows_one_way_collision() const { return true; } |
| 71 | |
| 72 | virtual bool is_concave() const { return false; } |
| 73 | |
| 74 | virtual bool contains_point(const Vector2 &p_point) const = 0; |
| 75 | |
| 76 | virtual void project_rangev(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const = 0; |
| 77 | virtual void project_range_castv(const Vector2 &p_cast, const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const = 0; |
| 78 | virtual Vector2 get_support(const Vector2 &p_normal) const; |
| 79 | virtual void get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const = 0; |
| 80 | |
| 81 | virtual bool intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const = 0; |
| 82 | virtual real_t get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const = 0; |
| 83 | virtual void set_data(const Variant &p_data) = 0; |
| 84 | virtual Variant get_data() const = 0; |
| 85 | |
| 86 | _FORCE_INLINE_ void set_custom_bias(real_t p_bias) { custom_bias = p_bias; } |
| 87 | _FORCE_INLINE_ real_t get_custom_bias() const { return custom_bias; } |
| 88 | |
| 89 | void add_owner(GodotShapeOwner2D *p_owner); |
| 90 | void remove_owner(GodotShapeOwner2D *p_owner); |
| 91 | bool is_owner(GodotShapeOwner2D *p_owner) const; |
| 92 | const HashMap<GodotShapeOwner2D *, int> &get_owners() const; |
| 93 | |
| 94 | _FORCE_INLINE_ void get_supports_transformed_cast(const Vector2 &p_cast, const Vector2 &p_normal, const Transform2D &p_xform, Vector2 *r_supports, int &r_amount) const { |
| 95 | get_supports(p_xform.basis_xform_inv(p_normal).normalized(), r_supports, r_amount); |
| 96 | for (int i = 0; i < r_amount; i++) { |
| 97 | r_supports[i] = p_xform.xform(r_supports[i]); |
| 98 | } |
| 99 | |
| 100 | if (r_amount == 1) { |
| 101 | if (Math::abs(p_normal.dot(p_cast.normalized())) < segment_is_valid_support_threshold_lower) { |
| 102 | //make line because they are parallel |
| 103 | r_amount = 2; |
| 104 | r_supports[1] = r_supports[0] + p_cast; |
| 105 | } else if (p_cast.dot(p_normal) > 0) { |
| 106 | //normal points towards cast, add cast |
| 107 | r_supports[0] += p_cast; |
| 108 | } |
| 109 | |
| 110 | } else { |
| 111 | if (Math::abs(p_normal.dot(p_cast.normalized())) < segment_is_valid_support_threshold_lower) { |
| 112 | //optimize line and make it larger because they are parallel |
| 113 | if ((r_supports[1] - r_supports[0]).dot(p_cast) > 0) { |
| 114 | //larger towards 1 |
| 115 | r_supports[1] += p_cast; |
| 116 | } else { |
| 117 | //larger towards 0 |
| 118 | r_supports[0] += p_cast; |
| 119 | } |
| 120 | } else if (p_cast.dot(p_normal) > 0) { |
| 121 | //normal points towards cast, add cast |
| 122 | r_supports[0] += p_cast; |
| 123 | r_supports[1] += p_cast; |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | GodotShape2D() {} |
| 128 | virtual ~GodotShape2D(); |
| 129 | }; |
| 130 | |
| 131 | //let the optimizer do the magic |
| 132 | #define DEFAULT_PROJECT_RANGE_CAST \ |
| 133 | virtual void project_range_castv(const Vector2 &p_cast, const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const override { \ |
| 134 | project_range_cast(p_cast, p_normal, p_transform, r_min, r_max); \ |
| 135 | } \ |
| 136 | _FORCE_INLINE_ void project_range_cast(const Vector2 &p_cast, const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const { \ |
| 137 | real_t mina, maxa; \ |
| 138 | real_t minb, maxb; \ |
| 139 | Transform2D ofsb = p_transform; \ |
| 140 | ofsb.columns[2] += p_cast; \ |
| 141 | project_range(p_normal, p_transform, mina, maxa); \ |
| 142 | project_range(p_normal, ofsb, minb, maxb); \ |
| 143 | r_min = MIN(mina, minb); \ |
| 144 | r_max = MAX(maxa, maxb); \ |
| 145 | } |
| 146 | |
| 147 | class GodotWorldBoundaryShape2D : public GodotShape2D { |
| 148 | Vector2 normal; |
| 149 | real_t d = 0.0; |
| 150 | |
| 151 | public: |
| 152 | _FORCE_INLINE_ Vector2 get_normal() const { return normal; } |
| 153 | _FORCE_INLINE_ real_t get_d() const { return d; } |
| 154 | |
| 155 | virtual PhysicsServer2D::ShapeType get_type() const override { return PhysicsServer2D::SHAPE_WORLD_BOUNDARY; } |
| 156 | |
| 157 | virtual void project_rangev(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const override { project_range(p_normal, p_transform, r_min, r_max); } |
| 158 | virtual void get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const override; |
| 159 | |
| 160 | virtual bool contains_point(const Vector2 &p_point) const override; |
| 161 | virtual bool intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const override; |
| 162 | virtual real_t get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const override; |
| 163 | |
| 164 | virtual void set_data(const Variant &p_data) override; |
| 165 | virtual Variant get_data() const override; |
| 166 | |
| 167 | _FORCE_INLINE_ void project_range(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const { |
| 168 | //real large |
| 169 | r_min = -1e10; |
| 170 | r_max = 1e10; |
| 171 | } |
| 172 | |
| 173 | virtual void project_range_castv(const Vector2 &p_cast, const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const override { |
| 174 | project_range_cast(p_cast, p_normal, p_transform, r_min, r_max); |
| 175 | } |
| 176 | |
| 177 | _FORCE_INLINE_ void project_range_cast(const Vector2 &p_cast, const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const { |
| 178 | //real large |
| 179 | r_min = -1e10; |
| 180 | r_max = 1e10; |
| 181 | } |
| 182 | }; |
| 183 | |
| 184 | class GodotSeparationRayShape2D : public GodotShape2D { |
| 185 | real_t length = 0.0; |
| 186 | bool slide_on_slope = false; |
| 187 | |
| 188 | public: |
| 189 | _FORCE_INLINE_ real_t get_length() const { return length; } |
| 190 | _FORCE_INLINE_ bool get_slide_on_slope() const { return slide_on_slope; } |
| 191 | |
| 192 | virtual PhysicsServer2D::ShapeType get_type() const override { return PhysicsServer2D::SHAPE_SEPARATION_RAY; } |
| 193 | |
| 194 | virtual bool allows_one_way_collision() const override { return false; } |
| 195 | |
| 196 | virtual void project_rangev(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const override { project_range(p_normal, p_transform, r_min, r_max); } |
| 197 | virtual void get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const override; |
| 198 | |
| 199 | virtual bool contains_point(const Vector2 &p_point) const override; |
| 200 | virtual bool intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const override; |
| 201 | virtual real_t get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const override; |
| 202 | |
| 203 | virtual void set_data(const Variant &p_data) override; |
| 204 | virtual Variant get_data() const override; |
| 205 | |
| 206 | _FORCE_INLINE_ void project_range(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const { |
| 207 | //real large |
| 208 | r_max = p_normal.dot(p_transform.get_origin()); |
| 209 | r_min = p_normal.dot(p_transform.xform(Vector2(0, length))); |
| 210 | if (r_max < r_min) { |
| 211 | SWAP(r_max, r_min); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | DEFAULT_PROJECT_RANGE_CAST |
| 216 | |
| 217 | _FORCE_INLINE_ GodotSeparationRayShape2D() {} |
| 218 | _FORCE_INLINE_ GodotSeparationRayShape2D(real_t p_length) { length = p_length; } |
| 219 | }; |
| 220 | |
| 221 | class GodotSegmentShape2D : public GodotShape2D { |
| 222 | Vector2 a; |
| 223 | Vector2 b; |
| 224 | Vector2 n; |
| 225 | |
| 226 | public: |
| 227 | _FORCE_INLINE_ const Vector2 &get_a() const { return a; } |
| 228 | _FORCE_INLINE_ const Vector2 &get_b() const { return b; } |
| 229 | _FORCE_INLINE_ const Vector2 &get_normal() const { return n; } |
| 230 | |
| 231 | virtual PhysicsServer2D::ShapeType get_type() const override { return PhysicsServer2D::SHAPE_SEGMENT; } |
| 232 | |
| 233 | _FORCE_INLINE_ Vector2 get_xformed_normal(const Transform2D &p_xform) const { |
| 234 | return (p_xform.xform(b) - p_xform.xform(a)).normalized().orthogonal(); |
| 235 | } |
| 236 | virtual void project_rangev(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const override { project_range(p_normal, p_transform, r_min, r_max); } |
| 237 | virtual void get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const override; |
| 238 | |
| 239 | virtual bool contains_point(const Vector2 &p_point) const override; |
| 240 | virtual bool intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const override; |
| 241 | virtual real_t get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const override; |
| 242 | |
| 243 | virtual void set_data(const Variant &p_data) override; |
| 244 | virtual Variant get_data() const override; |
| 245 | |
| 246 | _FORCE_INLINE_ void project_range(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const { |
| 247 | //real large |
| 248 | r_max = p_normal.dot(p_transform.xform(a)); |
| 249 | r_min = p_normal.dot(p_transform.xform(b)); |
| 250 | if (r_max < r_min) { |
| 251 | SWAP(r_max, r_min); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | DEFAULT_PROJECT_RANGE_CAST |
| 256 | |
| 257 | _FORCE_INLINE_ GodotSegmentShape2D() {} |
| 258 | _FORCE_INLINE_ GodotSegmentShape2D(const Vector2 &p_a, const Vector2 &p_b, const Vector2 &p_n) { |
| 259 | a = p_a; |
| 260 | b = p_b; |
| 261 | n = p_n; |
| 262 | } |
| 263 | }; |
| 264 | |
| 265 | class GodotCircleShape2D : public GodotShape2D { |
| 266 | real_t radius; |
| 267 | |
| 268 | public: |
| 269 | _FORCE_INLINE_ const real_t &get_radius() const { return radius; } |
| 270 | |
| 271 | virtual PhysicsServer2D::ShapeType get_type() const override { return PhysicsServer2D::SHAPE_CIRCLE; } |
| 272 | |
| 273 | virtual void project_rangev(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const override { project_range(p_normal, p_transform, r_min, r_max); } |
| 274 | virtual void get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const override; |
| 275 | |
| 276 | virtual bool contains_point(const Vector2 &p_point) const override; |
| 277 | virtual bool intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const override; |
| 278 | virtual real_t get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const override; |
| 279 | |
| 280 | virtual void set_data(const Variant &p_data) override; |
| 281 | virtual Variant get_data() const override; |
| 282 | |
| 283 | _FORCE_INLINE_ void project_range(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const { |
| 284 | //real large |
| 285 | real_t d = p_normal.dot(p_transform.get_origin()); |
| 286 | |
| 287 | // figure out scale at point |
| 288 | Vector2 local_normal = p_transform.basis_xform_inv(p_normal); |
| 289 | real_t scale = local_normal.length(); |
| 290 | |
| 291 | r_min = d - (radius)*scale; |
| 292 | r_max = d + (radius)*scale; |
| 293 | } |
| 294 | |
| 295 | DEFAULT_PROJECT_RANGE_CAST |
| 296 | }; |
| 297 | |
| 298 | class GodotRectangleShape2D : public GodotShape2D { |
| 299 | Vector2 half_extents; |
| 300 | |
| 301 | public: |
| 302 | _FORCE_INLINE_ const Vector2 &get_half_extents() const { return half_extents; } |
| 303 | |
| 304 | virtual PhysicsServer2D::ShapeType get_type() const override { return PhysicsServer2D::SHAPE_RECTANGLE; } |
| 305 | |
| 306 | virtual void project_rangev(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const override { project_range(p_normal, p_transform, r_min, r_max); } |
| 307 | virtual void get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const override; |
| 308 | |
| 309 | virtual bool contains_point(const Vector2 &p_point) const override; |
| 310 | virtual bool intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const override; |
| 311 | virtual real_t get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const override; |
| 312 | |
| 313 | virtual void set_data(const Variant &p_data) override; |
| 314 | virtual Variant get_data() const override; |
| 315 | |
| 316 | _FORCE_INLINE_ void project_range(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const { |
| 317 | // no matter the angle, the box is mirrored anyway |
| 318 | r_max = -1e20; |
| 319 | r_min = 1e20; |
| 320 | for (int i = 0; i < 4; i++) { |
| 321 | real_t d = p_normal.dot(p_transform.xform(Vector2(((i & 1) * 2 - 1) * half_extents.x, ((i >> 1) * 2 - 1) * half_extents.y))); |
| 322 | |
| 323 | if (d > r_max) { |
| 324 | r_max = d; |
| 325 | } |
| 326 | if (d < r_min) { |
| 327 | r_min = d; |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | _FORCE_INLINE_ Vector2 get_circle_axis(const Transform2D &p_xform, const Transform2D &p_xform_inv, const Vector2 &p_circle) const { |
| 333 | Vector2 local_v = p_xform_inv.xform(p_circle); |
| 334 | |
| 335 | Vector2 he( |
| 336 | (local_v.x < 0) ? -half_extents.x : half_extents.x, |
| 337 | (local_v.y < 0) ? -half_extents.y : half_extents.y); |
| 338 | |
| 339 | return (p_xform.xform(he) - p_circle).normalized(); |
| 340 | } |
| 341 | |
| 342 | _FORCE_INLINE_ Vector2 get_box_axis(const Transform2D &p_xform, const Transform2D &p_xform_inv, const GodotRectangleShape2D *p_B, const Transform2D &p_B_xform, const Transform2D &p_B_xform_inv) const { |
| 343 | Vector2 a, b; |
| 344 | |
| 345 | { |
| 346 | Vector2 local_v = p_xform_inv.xform(p_B_xform.get_origin()); |
| 347 | |
| 348 | Vector2 he( |
| 349 | (local_v.x < 0) ? -half_extents.x : half_extents.x, |
| 350 | (local_v.y < 0) ? -half_extents.y : half_extents.y); |
| 351 | |
| 352 | a = p_xform.xform(he); |
| 353 | } |
| 354 | { |
| 355 | Vector2 local_v = p_B_xform_inv.xform(p_xform.get_origin()); |
| 356 | |
| 357 | Vector2 he( |
| 358 | (local_v.x < 0) ? -p_B->half_extents.x : p_B->half_extents.x, |
| 359 | (local_v.y < 0) ? -p_B->half_extents.y : p_B->half_extents.y); |
| 360 | |
| 361 | b = p_B_xform.xform(he); |
| 362 | } |
| 363 | |
| 364 | return (a - b).normalized(); |
| 365 | } |
| 366 | |
| 367 | DEFAULT_PROJECT_RANGE_CAST |
| 368 | }; |
| 369 | |
| 370 | class GodotCapsuleShape2D : public GodotShape2D { |
| 371 | real_t radius = 0.0; |
| 372 | real_t height = 0.0; |
| 373 | |
| 374 | public: |
| 375 | _FORCE_INLINE_ const real_t &get_radius() const { return radius; } |
| 376 | _FORCE_INLINE_ const real_t &get_height() const { return height; } |
| 377 | |
| 378 | virtual PhysicsServer2D::ShapeType get_type() const override { return PhysicsServer2D::SHAPE_CAPSULE; } |
| 379 | |
| 380 | virtual void project_rangev(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const override { project_range(p_normal, p_transform, r_min, r_max); } |
| 381 | virtual void get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const override; |
| 382 | |
| 383 | virtual bool contains_point(const Vector2 &p_point) const override; |
| 384 | virtual bool intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const override; |
| 385 | virtual real_t get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const override; |
| 386 | |
| 387 | virtual void set_data(const Variant &p_data) override; |
| 388 | virtual Variant get_data() const override; |
| 389 | |
| 390 | _FORCE_INLINE_ void project_range(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const { |
| 391 | // no matter the angle, the box is mirrored anyway |
| 392 | Vector2 n = p_transform.basis_xform_inv(p_normal).normalized(); |
| 393 | real_t h = height * 0.5 - radius; |
| 394 | |
| 395 | n *= radius; |
| 396 | n.y += (n.y > 0) ? h : -h; |
| 397 | |
| 398 | r_max = p_normal.dot(p_transform.xform(n)); |
| 399 | r_min = p_normal.dot(p_transform.xform(-n)); |
| 400 | |
| 401 | if (r_max < r_min) { |
| 402 | SWAP(r_max, r_min); |
| 403 | } |
| 404 | |
| 405 | //ERR_FAIL_COND( r_max < r_min ); |
| 406 | } |
| 407 | |
| 408 | DEFAULT_PROJECT_RANGE_CAST |
| 409 | }; |
| 410 | |
| 411 | class GodotConvexPolygonShape2D : public GodotShape2D { |
| 412 | struct Point { |
| 413 | Vector2 pos; |
| 414 | Vector2 normal; //normal to next segment |
| 415 | }; |
| 416 | |
| 417 | Point *points = nullptr; |
| 418 | int point_count = 0; |
| 419 | |
| 420 | public: |
| 421 | _FORCE_INLINE_ int get_point_count() const { return point_count; } |
| 422 | _FORCE_INLINE_ const Vector2 &get_point(int p_idx) const { return points[p_idx].pos; } |
| 423 | _FORCE_INLINE_ const Vector2 &get_segment_normal(int p_idx) const { return points[p_idx].normal; } |
| 424 | _FORCE_INLINE_ Vector2 get_xformed_segment_normal(const Transform2D &p_xform, int p_idx) const { |
| 425 | Vector2 a = points[p_idx].pos; |
| 426 | p_idx++; |
| 427 | Vector2 b = points[p_idx == point_count ? 0 : p_idx].pos; |
| 428 | return (p_xform.xform(b) - p_xform.xform(a)).normalized().orthogonal(); |
| 429 | } |
| 430 | |
| 431 | virtual PhysicsServer2D::ShapeType get_type() const override { return PhysicsServer2D::SHAPE_CONVEX_POLYGON; } |
| 432 | |
| 433 | virtual void project_rangev(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const override { project_range(p_normal, p_transform, r_min, r_max); } |
| 434 | virtual void get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const override; |
| 435 | |
| 436 | virtual bool contains_point(const Vector2 &p_point) const override; |
| 437 | virtual bool intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const override; |
| 438 | virtual real_t get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const override; |
| 439 | |
| 440 | virtual void set_data(const Variant &p_data) override; |
| 441 | virtual Variant get_data() const override; |
| 442 | |
| 443 | _FORCE_INLINE_ void project_range(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const { |
| 444 | if (!points || point_count <= 0) { |
| 445 | r_min = r_max = 0; |
| 446 | return; |
| 447 | } |
| 448 | |
| 449 | r_min = r_max = p_normal.dot(p_transform.xform(points[0].pos)); |
| 450 | for (int i = 1; i < point_count; i++) { |
| 451 | real_t d = p_normal.dot(p_transform.xform(points[i].pos)); |
| 452 | if (d > r_max) { |
| 453 | r_max = d; |
| 454 | } |
| 455 | if (d < r_min) { |
| 456 | r_min = d; |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | DEFAULT_PROJECT_RANGE_CAST |
| 462 | |
| 463 | GodotConvexPolygonShape2D() {} |
| 464 | ~GodotConvexPolygonShape2D(); |
| 465 | }; |
| 466 | |
| 467 | class GodotConcaveShape2D : public GodotShape2D { |
| 468 | public: |
| 469 | virtual bool is_concave() const override { return true; } |
| 470 | |
| 471 | // Returns true to stop the query. |
| 472 | typedef bool (*QueryCallback)(void *p_userdata, GodotShape2D *p_convex); |
| 473 | |
| 474 | virtual void cull(const Rect2 &p_local_aabb, QueryCallback p_callback, void *p_userdata) const = 0; |
| 475 | }; |
| 476 | |
| 477 | class GodotConcavePolygonShape2D : public GodotConcaveShape2D { |
| 478 | struct Segment { |
| 479 | int points[2] = {}; |
| 480 | }; |
| 481 | |
| 482 | Vector<Segment> segments; |
| 483 | Vector<Point2> points; |
| 484 | |
| 485 | struct BVH { |
| 486 | Rect2 aabb; |
| 487 | int left = 0, right = 0; |
| 488 | }; |
| 489 | |
| 490 | Vector<BVH> bvh; |
| 491 | int bvh_depth = 0; |
| 492 | |
| 493 | struct BVH_CompareX { |
| 494 | _FORCE_INLINE_ bool operator()(const BVH &a, const BVH &b) const { |
| 495 | return (a.aabb.position.x + a.aabb.size.x * 0.5) < (b.aabb.position.x + b.aabb.size.x * 0.5); |
| 496 | } |
| 497 | }; |
| 498 | |
| 499 | struct BVH_CompareY { |
| 500 | _FORCE_INLINE_ bool operator()(const BVH &a, const BVH &b) const { |
| 501 | return (a.aabb.position.y + a.aabb.size.y * 0.5) < (b.aabb.position.y + b.aabb.size.y * 0.5); |
| 502 | } |
| 503 | }; |
| 504 | |
| 505 | int _generate_bvh(BVH *p_bvh, int p_len, int p_depth); |
| 506 | |
| 507 | public: |
| 508 | virtual PhysicsServer2D::ShapeType get_type() const override { return PhysicsServer2D::SHAPE_CONCAVE_POLYGON; } |
| 509 | |
| 510 | virtual void project_rangev(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const override { |
| 511 | r_min = 0; |
| 512 | r_max = 0; |
| 513 | ERR_FAIL_MSG("Unsupported call to project_rangev in GodotConcavePolygonShape2D" ); |
| 514 | } |
| 515 | |
| 516 | void project_range(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const { |
| 517 | r_min = 0; |
| 518 | r_max = 0; |
| 519 | ERR_FAIL_MSG("Unsupported call to project_range in GodotConcavePolygonShape2D" ); |
| 520 | } |
| 521 | |
| 522 | virtual void get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const override; |
| 523 | |
| 524 | virtual bool contains_point(const Vector2 &p_point) const override; |
| 525 | virtual bool intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const override; |
| 526 | |
| 527 | virtual real_t get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const override { return 0; } |
| 528 | |
| 529 | virtual void set_data(const Variant &p_data) override; |
| 530 | virtual Variant get_data() const override; |
| 531 | |
| 532 | virtual void cull(const Rect2 &p_local_aabb, QueryCallback p_callback, void *p_userdata) const override; |
| 533 | |
| 534 | DEFAULT_PROJECT_RANGE_CAST |
| 535 | }; |
| 536 | |
| 537 | #undef DEFAULT_PROJECT_RANGE_CAST |
| 538 | |
| 539 | #endif // GODOT_SHAPE_2D_H |
| 540 | |