@@ -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
6161void 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
7777void 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+
93103void 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
105115Rect2 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
110120real_t CapsuleShape2D::get_enclosing_radius () const {
111- return height * 0.5 ;
121+ return height * 0 .5f ;
112122}
113123
114124void 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}
0 commit comments