Skip to content

Commit 9f38cfe

Browse files
committed
Add mid height property to CapsuleShape2D/3D
1 parent 730adf4 commit 9f38cfe

File tree

9 files changed

+81
-22
lines changed

9 files changed

+81
-22
lines changed

doc/classes/CapsuleShape2D.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
</tutorials>
1212
<members>
1313
<member name="height" type="float" setter="set_height" getter="get_height" default="30.0">
14-
The capsule's height.
14+
The capsule's full height, including the semicircles.
15+
</member>
16+
<member name="mid_height" type="float" setter="set_mid_height" getter="get_mid_height">
17+
The capsule's height, excluding the semicircles. This is the height of the central rectangular part in the middle of the capsule, and is the distance between the centers of the two semicircles. This is a wrapper for [member height].
1518
</member>
1619
<member name="radius" type="float" setter="set_radius" getter="get_radius" default="10.0">
1720
The capsule's radius.

doc/classes/CapsuleShape3D.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
</tutorials>
1313
<members>
1414
<member name="height" type="float" setter="set_height" getter="get_height" default="2.0">
15-
The capsule's height.
15+
The capsule's full height, including the hemispheres.
16+
</member>
17+
<member name="mid_height" type="float" setter="set_mid_height" getter="get_mid_height">
18+
The capsule's height, excluding the hemispheres. This is the height of the central cylindrical part in the middle of the capsule, and is the distance between the centers of the two hemispheres. This is a wrapper for [member height].
1619
</member>
1720
<member name="radius" type="float" setter="set_radius" getter="get_radius" default="0.5">
1821
The capsule's radius.

doc/classes/SpringBoneCollisionCapsule3D.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010
</tutorials>
1111
<members>
1212
<member name="height" type="float" setter="set_height" getter="get_height" default="0.5">
13-
The capsule's height.
13+
The capsule's full height, including the hemispheres.
1414
</member>
1515
<member name="inside" type="bool" setter="set_inside" getter="is_inside" default="false">
1616
If [code]true[/code], the collision acts to trap the joint within the collision.
1717
</member>
18+
<member name="mid_height" type="float" setter="set_mid_height" getter="get_mid_height">
19+
The capsule's height, excluding the hemispheres. This is the height of the central cylindrical part in the middle of the capsule, and is the distance between the centers of the two hemispheres. This is a wrapper for [member height].
20+
</member>
1821
<member name="radius" type="float" setter="set_radius" getter="get_radius" default="0.1">
1922
The capsule's radius.
2023
</member>

scene/3d/spring_bone_collision_capsule_3d.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ float SpringBoneCollisionCapsule3D::get_height() const {
6060
return height;
6161
}
6262

63+
void SpringBoneCollisionCapsule3D::set_mid_height(real_t p_mid_height) {
64+
ERR_FAIL_COND_MSG(p_mid_height < 0.0f, "SpringBoneCollisionCapsule3D mid-height cannot be negative.");
65+
height = p_mid_height + radius * 2.0f;
66+
#ifdef TOOLS_ENABLED
67+
update_gizmos();
68+
#endif // TOOLS_ENABLED
69+
}
70+
71+
real_t SpringBoneCollisionCapsule3D::get_mid_height() const {
72+
return height - radius * 2.0f;
73+
}
74+
6375
void SpringBoneCollisionCapsule3D::set_inside(bool p_enabled) {
6476
inside = p_enabled;
6577
#ifdef TOOLS_ENABLED
@@ -83,11 +95,14 @@ void SpringBoneCollisionCapsule3D::_bind_methods() {
8395
ClassDB::bind_method(D_METHOD("get_radius"), &SpringBoneCollisionCapsule3D::get_radius);
8496
ClassDB::bind_method(D_METHOD("set_height", "height"), &SpringBoneCollisionCapsule3D::set_height);
8597
ClassDB::bind_method(D_METHOD("get_height"), &SpringBoneCollisionCapsule3D::get_height);
98+
ClassDB::bind_method(D_METHOD("set_mid_height", "mid_height"), &SpringBoneCollisionCapsule3D::set_mid_height);
99+
ClassDB::bind_method(D_METHOD("get_mid_height"), &SpringBoneCollisionCapsule3D::get_mid_height);
86100
ClassDB::bind_method(D_METHOD("set_inside", "enabled"), &SpringBoneCollisionCapsule3D::set_inside);
87101
ClassDB::bind_method(D_METHOD("is_inside"), &SpringBoneCollisionCapsule3D::is_inside);
88102

89103
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0,1,0.001,or_greater,suffix:m"), "set_radius", "get_radius");
90104
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0,1,0.001,or_greater,suffix:m"), "set_height", "get_height");
105+
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mid_height", PROPERTY_HINT_RANGE, "0,1,0.001,or_greater,suffix:m", PROPERTY_USAGE_NONE), "set_mid_height", "get_mid_height");
91106
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "inside"), "set_inside", "is_inside");
92107
}
93108

scene/3d/spring_bone_collision_capsule_3d.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class SpringBoneCollisionCapsule3D : public SpringBoneCollision3D {
4949
float get_radius() const;
5050
void set_height(float p_height);
5151
float get_height() const;
52+
void set_mid_height(real_t p_mid_height);
53+
real_t get_mid_height() const;
5254
void set_inside(bool p_enabled);
5355
bool is_inside() const;
5456

scene/resources/2d/capsule_shape_2d.cpp

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Vector<Vector2> CapsuleShape2D::_get_points() const {
3838
Vector<Vector2> points;
3939
const real_t turn_step = Math::TAU / 24.0;
4040
for (int i = 0; i < 24; i++) {
41-
Vector2 ofs = Vector2(0, (i > 6 && i <= 18) ? -height * 0.5 + radius : height * 0.5 - radius);
41+
Vector2 ofs = Vector2(0, (i > 6 && i <= 18) ? -height * 0.5f + radius : height * 0.5f - radius);
4242

4343
points.push_back(Vector2(Math::sin(i * turn_step), Math::cos(i * turn_step)) * radius + ofs);
4444
if (i == 6 || i == 18) {
@@ -59,13 +59,13 @@ void CapsuleShape2D::_update_shape() {
5959
}
6060

6161
void CapsuleShape2D::set_radius(real_t p_radius) {
62-
ERR_FAIL_COND_MSG(p_radius < 0, "CapsuleShape2D radius cannot be negative.");
62+
ERR_FAIL_COND_MSG(p_radius < 0.0f, "CapsuleShape2D radius cannot be negative.");
6363
if (radius == p_radius) {
6464
return;
6565
}
6666
radius = p_radius;
67-
if (radius > height * 0.5) {
68-
height = radius * 2.0;
67+
if (height < radius * 2.0f) {
68+
height = radius * 2.0f;
6969
}
7070
_update_shape();
7171
}
@@ -75,13 +75,13 @@ real_t CapsuleShape2D::get_radius() const {
7575
}
7676

7777
void CapsuleShape2D::set_height(real_t p_height) {
78-
ERR_FAIL_COND_MSG(p_height < 0, "CapsuleShape2D height cannot be negative.");
78+
ERR_FAIL_COND_MSG(p_height < 0.0f, "CapsuleShape2D height cannot be negative.");
7979
if (height == p_height) {
8080
return;
8181
}
8282
height = p_height;
83-
if (radius > height * 0.5) {
84-
radius = height * 0.5;
83+
if (radius > height * 0.5f) {
84+
radius = height * 0.5f;
8585
}
8686
_update_shape();
8787
}
@@ -90,25 +90,35 @@ real_t CapsuleShape2D::get_height() const {
9090
return height;
9191
}
9292

93+
void CapsuleShape2D::set_mid_height(real_t p_mid_height) {
94+
ERR_FAIL_COND_MSG(p_mid_height < 0.0f, "CapsuleShape2D mid-height cannot be negative.");
95+
height = p_mid_height + radius * 2.0f;
96+
_update_shape();
97+
}
98+
99+
real_t CapsuleShape2D::get_mid_height() const {
100+
return height - radius * 2.0f;
101+
}
102+
93103
void CapsuleShape2D::draw(const RID &p_to_rid, const Color &p_color) {
94104
Vector<Vector2> points = _get_points();
95105
Vector<Color> col = { p_color };
96106
RenderingServer::get_singleton()->canvas_item_add_polygon(p_to_rid, points, col);
97107

98108
if (is_collision_outline_enabled()) {
99109
points.push_back(points[0]);
100-
col = { Color(p_color, 1.0) };
110+
col = { Color(p_color, 1.0f) };
101111
RenderingServer::get_singleton()->canvas_item_add_polyline(p_to_rid, points, col);
102112
}
103113
}
104114

105115
Rect2 CapsuleShape2D::get_rect() const {
106-
const Vector2 half_size = Vector2(radius, height * 0.5);
107-
return Rect2(-half_size, half_size * 2.0);
116+
const Vector2 half_size = Vector2(radius, height * 0.5f);
117+
return Rect2(-half_size, half_size * 2.0f);
108118
}
109119

110120
real_t CapsuleShape2D::get_enclosing_radius() const {
111-
return height * 0.5;
121+
return height * 0.5f;
112122
}
113123

114124
void CapsuleShape2D::_bind_methods() {
@@ -118,8 +128,12 @@ void CapsuleShape2D::_bind_methods() {
118128
ClassDB::bind_method(D_METHOD("set_height", "height"), &CapsuleShape2D::set_height);
119129
ClassDB::bind_method(D_METHOD("get_height"), &CapsuleShape2D::get_height);
120130

131+
ClassDB::bind_method(D_METHOD("set_mid_height", "mid_height"), &CapsuleShape2D::set_mid_height);
132+
ClassDB::bind_method(D_METHOD("get_mid_height"), &CapsuleShape2D::get_mid_height);
133+
121134
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.01,1024,0.01,or_greater,suffix:px"), "set_radius", "get_radius");
122135
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0.01,1024,0.01,or_greater,suffix:px"), "set_height", "get_height");
136+
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mid_height", PROPERTY_HINT_RANGE, "0.01,1024,0.01,or_greater,suffix:px", PROPERTY_USAGE_NONE), "set_mid_height", "get_mid_height");
123137
ADD_LINKED_PROPERTY("radius", "height");
124138
ADD_LINKED_PROPERTY("height", "radius");
125139
}

scene/resources/2d/capsule_shape_2d.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ class CapsuleShape2D : public Shape2D {
5353
void set_radius(real_t p_radius);
5454
real_t get_radius() const;
5555

56+
void set_mid_height(real_t p_mid_height);
57+
real_t get_mid_height() const;
58+
5659
virtual void draw(const RID &p_to_rid, const Color &p_color) override;
5760
virtual Rect2 get_rect() const override;
5861
virtual real_t get_enclosing_radius() const override;

scene/resources/3d/capsule_shape_3d.cpp

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Vector<Vector3> CapsuleShape3D::get_debug_mesh_lines() const {
3939

4040
Vector<Vector3> points;
4141

42-
Vector3 d(0, c_height * 0.5 - c_radius, 0);
42+
Vector3 d(0, c_height * 0.5f - c_radius, 0);
4343
for (int i = 0; i < 360; i++) {
4444
float ra = Math::deg_to_rad((float)i);
4545
float rb = Math::deg_to_rad((float)i + 1);
@@ -87,7 +87,7 @@ Ref<ArrayMesh> CapsuleShape3D::get_debug_arraymesh_faces(const Color &p_modulate
8787
}
8888

8989
real_t CapsuleShape3D::get_enclosing_radius() const {
90-
return height * 0.5;
90+
return height * 0.5f;
9191
}
9292

9393
void CapsuleShape3D::_update_shape() {
@@ -99,10 +99,10 @@ void CapsuleShape3D::_update_shape() {
9999
}
100100

101101
void CapsuleShape3D::set_radius(float p_radius) {
102-
ERR_FAIL_COND_MSG(p_radius < 0, "CapsuleShape3D radius cannot be negative.");
102+
ERR_FAIL_COND_MSG(p_radius < 0.0f, "CapsuleShape3D radius cannot be negative.");
103103
radius = p_radius;
104-
if (radius > height * 0.5) {
105-
height = radius * 2.0;
104+
if (height < radius * 2.0f) {
105+
height = radius * 2.0f;
106106
}
107107
_update_shape();
108108
emit_changed();
@@ -113,10 +113,10 @@ float CapsuleShape3D::get_radius() const {
113113
}
114114

115115
void CapsuleShape3D::set_height(float p_height) {
116-
ERR_FAIL_COND_MSG(p_height < 0, "CapsuleShape3D height cannot be negative.");
116+
ERR_FAIL_COND_MSG(p_height < 0.0f, "CapsuleShape3D height cannot be negative.");
117117
height = p_height;
118-
if (radius > height * 0.5) {
119-
radius = height * 0.5;
118+
if (radius > height * 0.5f) {
119+
radius = height * 0.5f;
120120
}
121121
_update_shape();
122122
emit_changed();
@@ -126,14 +126,28 @@ float CapsuleShape3D::get_height() const {
126126
return height;
127127
}
128128

129+
void CapsuleShape3D::set_mid_height(real_t p_mid_height) {
130+
ERR_FAIL_COND_MSG(p_mid_height < 0.0f, "CapsuleShape3D mid-height cannot be negative.");
131+
height = p_mid_height + radius * 2.0f;
132+
_update_shape();
133+
emit_changed();
134+
}
135+
136+
real_t CapsuleShape3D::get_mid_height() const {
137+
return height - radius * 2.0f;
138+
}
139+
129140
void CapsuleShape3D::_bind_methods() {
130141
ClassDB::bind_method(D_METHOD("set_radius", "radius"), &CapsuleShape3D::set_radius);
131142
ClassDB::bind_method(D_METHOD("get_radius"), &CapsuleShape3D::get_radius);
132143
ClassDB::bind_method(D_METHOD("set_height", "height"), &CapsuleShape3D::set_height);
133144
ClassDB::bind_method(D_METHOD("get_height"), &CapsuleShape3D::get_height);
145+
ClassDB::bind_method(D_METHOD("set_mid_height", "mid_height"), &CapsuleShape3D::set_mid_height);
146+
ClassDB::bind_method(D_METHOD("get_mid_height"), &CapsuleShape3D::get_mid_height);
134147

135148
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.001,100,0.001,or_greater,suffix:m"), "set_radius", "get_radius");
136149
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0.001,100,0.001,or_greater,suffix:m"), "set_height", "get_height");
150+
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mid_height", PROPERTY_HINT_RANGE, "0.001,100,0.001,or_greater,suffix:m", PROPERTY_USAGE_NONE), "set_mid_height", "get_mid_height");
137151
ADD_LINKED_PROPERTY("radius", "height");
138152
ADD_LINKED_PROPERTY("height", "radius");
139153
}

scene/resources/3d/capsule_shape_3d.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class CapsuleShape3D : public Shape3D {
4949
float get_radius() const;
5050
void set_height(float p_height);
5151
float get_height() const;
52+
void set_mid_height(real_t p_mid_height);
53+
real_t get_mid_height() const;
5254

5355
virtual Vector<Vector3> get_debug_mesh_lines() const override;
5456
virtual Ref<ArrayMesh> get_debug_arraymesh_faces(const Color &p_modulate) const override;

0 commit comments

Comments
 (0)