Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/godot_physics_3d/godot_body_pair_3d.h
10277 views
1
/**************************************************************************/
2
/* godot_body_pair_3d.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
#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; // accumulated impulse - only one of the object's impulse is needed as impulse_a == -impulse_b
47
real_t acc_normal_impulse = 0.0; // accumulated normal impulse (Pn)
48
Vector3 acc_tangent_impulse; // accumulated tangent impulse (Pt)
49
real_t acc_bias_impulse = 0.0; // accumulated normal impulse for position bias (Pnb)
50
real_t acc_bias_impulse_center_of_mass = 0.0; // accumulated normal impulse for position bias applied to com
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; // Offset in world orientation with respect to center of mass
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; //use local A coordinates to avoid numerical issues on collision detection
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
};
145
146