diff --git a/doc/classes/CapsuleShape2D.xml b/doc/classes/CapsuleShape2D.xml index 52ea7af2921b..c8652e4ec451 100644 --- a/doc/classes/CapsuleShape2D.xml +++ b/doc/classes/CapsuleShape2D.xml @@ -11,7 +11,10 @@ - The capsule's height. + The capsule's full height, including the semicircles. + + + 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]. The capsule's radius. diff --git a/doc/classes/CapsuleShape3D.xml b/doc/classes/CapsuleShape3D.xml index 4c6b3a870f58..4e7c3d2edb3e 100644 --- a/doc/classes/CapsuleShape3D.xml +++ b/doc/classes/CapsuleShape3D.xml @@ -12,7 +12,10 @@ - The capsule's height. + The capsule's full height, including the hemispheres. + + + 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]. The capsule's radius. diff --git a/scene/resources/2d/capsule_shape_2d.cpp b/scene/resources/2d/capsule_shape_2d.cpp index 8268040ed9d5..c5a805f22d81 100644 --- a/scene/resources/2d/capsule_shape_2d.cpp +++ b/scene/resources/2d/capsule_shape_2d.cpp @@ -38,7 +38,7 @@ Vector CapsuleShape2D::_get_points() const { Vector 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) { @@ -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(); } @@ -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(); } @@ -84,6 +84,16 @@ 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 points = _get_points(); Vector col = { p_color }; @@ -91,18 +101,18 @@ void CapsuleShape2D::draw(const RID &p_to_rid, const Color &p_color) { 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() { @@ -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"); } diff --git a/scene/resources/2d/capsule_shape_2d.h b/scene/resources/2d/capsule_shape_2d.h index cfd3fc6b2677..2522d1d04e85 100644 --- a/scene/resources/2d/capsule_shape_2d.h +++ b/scene/resources/2d/capsule_shape_2d.h @@ -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; diff --git a/scene/resources/3d/capsule_shape_3d.cpp b/scene/resources/3d/capsule_shape_3d.cpp index 9e1680106032..6daefc785624 100644 --- a/scene/resources/3d/capsule_shape_3d.cpp +++ b/scene/resources/3d/capsule_shape_3d.cpp @@ -38,7 +38,7 @@ Vector CapsuleShape3D::get_debug_mesh_lines() const { Vector 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); @@ -68,7 +68,7 @@ Vector 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() { @@ -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(); @@ -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(); @@ -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"); } diff --git a/scene/resources/3d/capsule_shape_3d.h b/scene/resources/3d/capsule_shape_3d.h index 90ee3b584abb..3e7be61aa063 100644 --- a/scene/resources/3d/capsule_shape_3d.h +++ b/scene/resources/3d/capsule_shape_3d.h @@ -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 get_debug_mesh_lines() const override; virtual real_t get_enclosing_radius() const override;