| 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 "godot_body_3d.h" |
| 34 |
#include "godot_constraint_3d.h" |
| 35 |
#include "godot_soft_body_3d.h" |
| 36 |
|
| 37 |
#include "core/templates/local_vector.h" |
| 38 |
|
| 39 |
class GodotBodyContact3D : public GodotConstraint3D { |
| 40 |
protected: |
| 41 |
struct Contact { |
| 42 |
Vector3 position; |
| 43 |
Vector3 normal; |
| 44 |
int index_A = 0, index_B = 0; |
| 45 |
Vector3 local_A, local_B; |
| 46 |
Vector3 acc_impulse; |
| 47 |
real_t acc_normal_impulse = 0.0; |
| 48 |
Vector3 acc_tangent_impulse; |
| 49 |
real_t acc_bias_impulse = 0.0; |
| 50 |
real_t acc_bias_impulse_center_of_mass = 0.0; |
| 51 |
real_t mass_normal = 0.0; |
| 52 |
real_t bias = 0.0; |
| 53 |
real_t bounce = 0.0; |
| 54 |
|
| 55 |
real_t depth = 0.0; |
| 56 |
bool active = false; |
| 57 |
bool used = false; |
| 58 |
Vector3 rA, rB; |
| 59 |
}; |
| 60 |
|
| 61 |
Vector3 sep_axis; |
| 62 |
bool collided = false; |
| 63 |
bool check_ccd = false; |
| 64 |
|
| 65 |
GodotSpace3D *space = nullptr; |
| 66 |
|
| 67 |
GodotBodyContact3D(GodotBody3D **p_body_ptr = nullptr, int p_body_count = 0) : |
| 68 |
GodotConstraint3D(p_body_ptr, p_body_count) { |
| 69 |
} |
| 70 |
}; |
| 71 |
|
| 72 |
class GodotBodyPair3D : public GodotBodyContact3D { |
| 73 |
enum { |
| 74 |
MAX_CONTACTS = 4 |
| 75 |
}; |
| 76 |
|
| 77 |
union { |
| 78 |
struct { |
| 79 |
GodotBody3D *A; |
| 80 |
GodotBody3D *B; |
| 81 |
}; |
| 82 |
|
| 83 |
GodotBody3D *_arr[2] = { nullptr, nullptr }; |
| 84 |
}; |
| 85 |
|
| 86 |
int shape_A = 0; |
| 87 |
int shape_B = 0; |
| 88 |
|
| 89 |
bool collide_A = false; |
| 90 |
bool collide_B = false; |
| 91 |
|
| 92 |
bool report_contacts_only = false; |
| 93 |
|
| 94 |
Vector3 offset_B; |
| 95 |
|
| 96 |
Contact contacts[MAX_CONTACTS]; |
| 97 |
int contact_count = 0; |
| 98 |
|
| 99 |
static void _contact_added_callback(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_B, const Vector3 &normal, void *p_userdata); |
| 100 |
|
| 101 |
void contact_added_callback(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_B, const Vector3 &normal); |
| 102 |
|
| 103 |
void validate_contacts(); |
| 104 |
bool _test_ccd(real_t p_step, GodotBody3D *p_A, int p_shape_A, const Transform3D &p_xform_A, GodotBody3D *p_B, int p_shape_B, const Transform3D &p_xform_B); |
| 105 |
|
| 106 |
public: |
| 107 |
virtual bool setup(real_t p_step) override; |
| 108 |
virtual bool pre_solve(real_t p_step) override; |
| 109 |
virtual void solve(real_t p_step) override; |
| 110 |
|
| 111 |
GodotBodyPair3D(GodotBody3D *p_A, int p_shape_A, GodotBody3D *p_B, int p_shape_B); |
| 112 |
~GodotBodyPair3D(); |
| 113 |
}; |
| 114 |
|
| 115 |
class GodotBodySoftBodyPair3D : public GodotBodyContact3D { |
| 116 |
GodotBody3D *body = nullptr; |
| 117 |
GodotSoftBody3D *soft_body = nullptr; |
| 118 |
|
| 119 |
int body_shape = 0; |
| 120 |
|
| 121 |
bool body_collides = false; |
| 122 |
bool soft_body_collides = false; |
| 123 |
|
| 124 |
bool report_contacts_only = false; |
| 125 |
|
| 126 |
LocalVector<Contact> contacts; |
| 127 |
|
| 128 |
static void _contact_added_callback(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_B, const Vector3 &normal, void *p_userdata); |
| 129 |
|
| 130 |
void contact_added_callback(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_B, const Vector3 &normal); |
| 131 |
|
| 132 |
void validate_contacts(); |
| 133 |
|
| 134 |
public: |
| 135 |
virtual bool setup(real_t p_step) override; |
| 136 |
virtual bool pre_solve(real_t p_step) override; |
| 137 |
virtual void solve(real_t p_step) override; |
| 138 |
|
| 139 |
virtual GodotSoftBody3D *get_soft_body_ptr(int p_index) const override { return soft_body; } |
| 140 |
virtual int get_soft_body_count() const override { return 1; } |
| 141 |
|
| 142 |
GodotBodySoftBodyPair3D(GodotBody3D *p_A, int p_shape_A, GodotSoftBody3D *p_B); |
| 143 |
~GodotBodySoftBodyPair3D(); |
| 144 |
}; |