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