Skip to content

Commit

Permalink
Add mid height property to CapsuleShape2D/3D
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronfranke committed Jul 1, 2024
1 parent 9425535 commit f5305aa
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 21 deletions.
5 changes: 4 additions & 1 deletion doc/classes/CapsuleShape2D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
</tutorials>
<members>
<member name="height" type="float" setter="set_height" getter="get_height" default="30.0">
The capsule's height.
The capsule's full height, including the semicircles.
</member>
<member name="mid_height" type="float" setter="set_mid_height" getter="get_mid_height">
The capsule's mid height, excluding the semicircles. This is the height of the central rectangular part of the capsule, and is the distance between the centers of the two semicircles. This is a wrapper for [member height].
</member>
<member name="radius" type="float" setter="set_radius" getter="get_radius" default="10.0">
The capsule's radius.
Expand Down
5 changes: 4 additions & 1 deletion doc/classes/CapsuleShape3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
</tutorials>
<members>
<member name="height" type="float" setter="set_height" getter="get_height" default="2.0">
The capsule's height.
The capsule's full height, including the hemispheres.
</member>
<member name="mid_height" type="float" setter="set_mid_height" getter="get_mid_height">
The capsule's mid height, excluding the hemispheres. This is the height of the central cylindrical part of the capsule, and is the distance between the centers of the two hemispheres. This is a wrapper for [member height].
</member>
<member name="radius" type="float" setter="set_radius" getter="get_radius" default="0.5">
The capsule's radius.
Expand Down
36 changes: 25 additions & 11 deletions scene/resources/2d/capsule_shape_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Vector<Vector2> CapsuleShape2D::_get_points() const {
Vector<Vector2> points;
const real_t turn_step = Math_TAU / 24.0;
for (int i = 0; i < 24; i++) {
Vector2 ofs = Vector2(0, (i > 6 && i <= 18) ? -height * 0.5 + radius : height * 0.5 - radius);
Vector2 ofs = Vector2(0, (i > 6 && i <= 18) ? -height * 0.5f + radius : height * 0.5f - radius);

points.push_back(Vector2(Math::sin(i * turn_step), Math::cos(i * turn_step)) * radius + ofs);
if (i == 6 || i == 18) {
Expand All @@ -59,10 +59,10 @@ void CapsuleShape2D::_update_shape() {
}

void CapsuleShape2D::set_radius(real_t p_radius) {
ERR_FAIL_COND_MSG(p_radius < 0, "CapsuleShape2D radius cannot be negative.");
ERR_FAIL_COND_MSG(p_radius < 0.0f, "CapsuleShape2D radius cannot be negative.");
radius = p_radius;
if (radius > height * 0.5) {
height = radius * 2.0;
if (height < radius * 2.0f) {
height = radius * 2.0f;
}
_update_shape();
}
Expand All @@ -72,10 +72,10 @@ real_t CapsuleShape2D::get_radius() const {
}

void CapsuleShape2D::set_height(real_t p_height) {
ERR_FAIL_COND_MSG(p_height < 0, "CapsuleShape2D height cannot be negative.");
ERR_FAIL_COND_MSG(p_height < 0.0f, "CapsuleShape2D height cannot be negative.");
height = p_height;
if (radius > height * 0.5) {
radius = height * 0.5;
if (radius > height * 0.5f) {
radius = height * 0.5f;
}
_update_shape();
}
Expand All @@ -84,25 +84,35 @@ real_t CapsuleShape2D::get_height() const {
return height;
}

void CapsuleShape2D::set_mid_height(real_t p_mid_height) {
ERR_FAIL_COND_MSG(p_mid_height < 0.0f, "CapsuleShape2D mid-height cannot be negative.");
height = p_mid_height + radius * 2.0f;
_update_shape();
}

real_t CapsuleShape2D::get_mid_height() const {
return height - radius * 2.0f;
}

void CapsuleShape2D::draw(const RID &p_to_rid, const Color &p_color) {
Vector<Vector2> points = _get_points();
Vector<Color> col = { p_color };
RenderingServer::get_singleton()->canvas_item_add_polygon(p_to_rid, points, col);

if (is_collision_outline_enabled()) {
points.push_back(points[0]);
col = { Color(p_color, 1.0) };
col = { Color(p_color, 1.0f) };
RenderingServer::get_singleton()->canvas_item_add_polyline(p_to_rid, points, col);
}
}

Rect2 CapsuleShape2D::get_rect() const {
const Vector2 half_size = Vector2(radius, height * 0.5);
return Rect2(-half_size, half_size * 2.0);
const Vector2 half_size = Vector2(radius, height * 0.5f);
return Rect2(-half_size, half_size * 2.0f);
}

real_t CapsuleShape2D::get_enclosing_radius() const {
return height * 0.5;
return height * 0.5f;
}

void CapsuleShape2D::_bind_methods() {
Expand All @@ -112,8 +122,12 @@ void CapsuleShape2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_height", "height"), &CapsuleShape2D::set_height);
ClassDB::bind_method(D_METHOD("get_height"), &CapsuleShape2D::get_height);

ClassDB::bind_method(D_METHOD("set_mid_height", "mid_height"), &CapsuleShape2D::set_mid_height);
ClassDB::bind_method(D_METHOD("get_mid_height"), &CapsuleShape2D::get_mid_height);

ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.01,1024,0.01,or_greater,suffix:px"), "set_radius", "get_radius");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0.01,1024,0.01,or_greater,suffix:px"), "set_height", "get_height");
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");
ADD_LINKED_PROPERTY("radius", "height");
ADD_LINKED_PROPERTY("height", "radius");
}
Expand Down
3 changes: 3 additions & 0 deletions scene/resources/2d/capsule_shape_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class CapsuleShape2D : public Shape2D {
void set_radius(real_t p_radius);
real_t get_radius() const;

void set_mid_height(real_t p_mid_height);
real_t get_mid_height() const;

virtual void draw(const RID &p_to_rid, const Color &p_color) override;
virtual Rect2 get_rect() const override;
virtual real_t get_enclosing_radius() const override;
Expand Down
30 changes: 22 additions & 8 deletions scene/resources/3d/capsule_shape_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Vector<Vector3> CapsuleShape3D::get_debug_mesh_lines() const {

Vector<Vector3> points;

Vector3 d(0, c_height * 0.5 - c_radius, 0);
Vector3 d(0, c_height * 0.5f - c_radius, 0);
for (int i = 0; i < 360; i++) {
float ra = Math::deg_to_rad((float)i);
float rb = Math::deg_to_rad((float)i + 1);
Expand Down Expand Up @@ -68,7 +68,7 @@ Vector<Vector3> CapsuleShape3D::get_debug_mesh_lines() const {
}

real_t CapsuleShape3D::get_enclosing_radius() const {
return height * 0.5;
return height * 0.5f;
}

void CapsuleShape3D::_update_shape() {
Expand All @@ -80,10 +80,10 @@ void CapsuleShape3D::_update_shape() {
}

void CapsuleShape3D::set_radius(float p_radius) {
ERR_FAIL_COND_MSG(p_radius < 0, "CapsuleShape3D radius cannot be negative.");
ERR_FAIL_COND_MSG(p_radius < 0.0f, "CapsuleShape3D radius cannot be negative.");
radius = p_radius;
if (radius > height * 0.5) {
height = radius * 2.0;
if (height < radius * 2.0f) {
height = radius * 2.0f;
}
_update_shape();
emit_changed();
Expand All @@ -94,10 +94,10 @@ float CapsuleShape3D::get_radius() const {
}

void CapsuleShape3D::set_height(float p_height) {
ERR_FAIL_COND_MSG(p_height < 0, "CapsuleShape3D height cannot be negative.");
ERR_FAIL_COND_MSG(p_height < 0.0f, "CapsuleShape3D height cannot be negative.");
height = p_height;
if (radius > height * 0.5) {
radius = height * 0.5;
if (radius > height * 0.5f) {
radius = height * 0.5f;
}
_update_shape();
emit_changed();
Expand All @@ -107,14 +107,28 @@ float CapsuleShape3D::get_height() const {
return height;
}

void CapsuleShape3D::set_mid_height(real_t p_mid_height) {
ERR_FAIL_COND_MSG(p_mid_height < 0.0f, "CapsuleShape3D mid-height cannot be negative.");
height = p_mid_height + radius * 2.0f;
_update_shape();
emit_changed();
}

real_t CapsuleShape3D::get_mid_height() const {
return height - radius * 2.0f;
}

void CapsuleShape3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_radius", "radius"), &CapsuleShape3D::set_radius);
ClassDB::bind_method(D_METHOD("get_radius"), &CapsuleShape3D::get_radius);
ClassDB::bind_method(D_METHOD("set_height", "height"), &CapsuleShape3D::set_height);
ClassDB::bind_method(D_METHOD("get_height"), &CapsuleShape3D::get_height);
ClassDB::bind_method(D_METHOD("set_mid_height", "mid_height"), &CapsuleShape3D::set_mid_height);
ClassDB::bind_method(D_METHOD("get_mid_height"), &CapsuleShape3D::get_mid_height);

ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.001,100,0.001,or_greater,suffix:m"), "set_radius", "get_radius");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0.001,100,0.001,or_greater,suffix:m"), "set_height", "get_height");
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");
ADD_LINKED_PROPERTY("radius", "height");
ADD_LINKED_PROPERTY("height", "radius");
}
Expand Down
2 changes: 2 additions & 0 deletions scene/resources/3d/capsule_shape_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class CapsuleShape3D : public Shape3D {
float get_radius() const;
void set_height(float p_height);
float get_height() const;
void set_mid_height(real_t p_mid_height);
real_t get_mid_height() const;

virtual Vector<Vector3> get_debug_mesh_lines() const override;
virtual real_t get_enclosing_radius() const override;
Expand Down

0 comments on commit f5305aa

Please sign in to comment.