Godot Engine – Multi-platform 2D and 3D game engine
Use float literals and real_t consistently around square values
Aaron Franke58ef14369db9
diff --git a/editor/scene/2d/sprite_2d_editor_plugin.cpp b/editor/scene/2d/sprite_2d_editor_plugin.cppindex ab4014bc972..79622ab0b17 100644--- a/editor/scene/2d/sprite_2d_editor_plugin.cpp+++ b/editor/scene/2d/sprite_2d_editor_plugin.cpp@@ -96,7 +96,7 @@ Vector<Vector2> expand(const Vector<Vector2> &points, const Rect2i &rect, floatVector2 prev = Vector2(p2[lasti].x, p2[lasti].y);for (uint64_t i = 0; i < p2.size(); i++) {Vector2 cur = Vector2(p2[i].x, p2[i].y);- if (cur.distance_to(prev) > 0.5) {+ if (cur.distance_to(prev) > 0.5f) {outPoints.push_back(cur);prev = cur;}diff --git a/editor/scene/2d/tiles/tile_data_editors.cpp b/editor/scene/2d/tiles/tile_data_editors.cppindex 8fe2bf64a77..7c431e7913a 100644--- a/editor/scene/2d/tiles/tile_data_editors.cpp+++ b/editor/scene/2d/tiles/tile_data_editors.cpp@@ -414,11 +414,11 @@ void GenericTilePolygonEditor::_grab_polygon_point(Vector2 p_pos, const Transforconst real_t grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");r_polygon_index = -1;r_point_index = -1;- float closest_distance = grab_threshold + 1.0;+ real_t closest_distance = grab_threshold + 1.0f;for (unsigned int i = 0; i < polygons.size(); i++) {const Vector<Vector2> &polygon = polygons[i];for (int j = 0; j < polygon.size(); j++) {- float distance = p_pos.distance_to(p_polygon_xform.xform(polygon[j]));+ real_t distance = p_pos.distance_to(p_polygon_xform.xform(polygon[j]));if (distance < grab_threshold && distance < closest_distance) {r_polygon_index = i;r_point_index = j;diff --git a/editor/scene/canvas_item_editor_plugin.cpp b/editor/scene/canvas_item_editor_plugin.cppindex c939e3a1420..ea6429680b9 100644--- a/editor/scene/canvas_item_editor_plugin.cpp+++ b/editor/scene/canvas_item_editor_plugin.cpp@@ -1849,7 +1849,7 @@ bool CanvasItemEditor::_gui_input_resize(const Ref<InputEvent> &p_event) {};DragType resize_drag = DRAG_NONE;- real_t radius = (select_handle->get_size().width / 2) * 1.5;+ real_t radius = select_handle->get_size().width * (1.5f / 2.0f);for (int i = 0; i < 4; i++) {int prev = (i + 3) % 4;diff --git a/modules/godot_physics_3d/godot_body_pair_3d.cpp b/modules/godot_physics_3d/godot_body_pair_3d.cppindex 1bbd1289dfa..76440125a04 100644--- a/modules/godot_physics_3d/godot_body_pair_3d.cpp+++ b/modules/godot_physics_3d/godot_body_pair_3d.cpp@@ -204,9 +204,9 @@ bool GodotBodyPair3D::_test_ccd(real_t p_step, GodotBody3D *p_A, int p_shape_A,shape_A_ptr->get_supports(p_xform_A.basis.xform_inv(mnormal).normalized(), max_supports, supports_A, support_count_A, support_type_A);// Cast a segment from each support point of A in the motion direction.- int segment_support_idx = -1;- float segment_hit_length = FLT_MAX;Vector3 segment_hit_local;+ real_t segment_hit_length = FLT_MAX;+ int segment_support_idx = -1;for (int i = 0; i < support_count_A; i++) {supports_A[i] = p_xform_A.xform(supports_A[i]);@@ -224,7 +224,7 @@ bool GodotBodyPair3D::_test_ccd(real_t p_step, GodotBody3D *p_A, int p_shape_A,Vector3 rpos, rnorm;int fi = -1;if (p_B->get_shape(p_shape_B)->intersect_segment(local_from, local_to, rpos, rnorm, fi, true)) {- float hit_length = local_from.distance_to(rpos);+ real_t hit_length = local_from.distance_to(rpos);if (hit_length < segment_hit_length) {segment_support_idx = i;segment_hit_length = hit_length;diff --git a/modules/mobile_vr/mobile_vr_interface.cpp b/modules/mobile_vr/mobile_vr_interface.cppindex 39dfae306ab..b01a65c935e 100644--- a/modules/mobile_vr/mobile_vr_interface.cpp+++ b/modules/mobile_vr/mobile_vr_interface.cpp@@ -147,27 +147,27 @@ void MobileVRInterface::set_position_from_sensors() {if (sensor_first) {sensor_first = false;} else {- acc = scrub(acc, last_accerometer_data, 2, 0.2);- magneto = scrub(magneto, last_magnetometer_data, 3, 0.3);+ acc = scrub(acc, last_accerometer_data, 2, 0.2f);+ magneto = scrub(magneto, last_magnetometer_data, 3, 0.3f);};last_accerometer_data = acc;last_magnetometer_data = magneto;- if (grav.length() < 0.1) {+ if (grav.length() < 0.1f) {// not ideal but use our accelerometer, this will contain shaky user behavior// maybe look into some math but I'm guessing that if this isn't available, it's because we lack the gyro sensor to actually work out// what a stable gravity vector isgrav = acc;- if (grav.length() > 0.1) {+ if (grav.length() > 0.1f) {has_grav = true;};} else {has_grav = true;};- bool has_magneto = magneto.length() > 0.1;- if (gyro.length() > 0.1) {+ bool has_magneto = magneto.length() > 0.1f;+ if (gyro.length() > 0.1f) {/* this can return to 0.0 if the user doesn't move the phone, so once on, it's on */has_gyro = true;};diff --git a/scene/2d/cpu_particles_2d.cpp b/scene/2d/cpu_particles_2d.cppindex ac8d910a574..bdd22977b60 100644--- a/scene/2d/cpu_particles_2d.cpp+++ b/scene/2d/cpu_particles_2d.cpp@@ -1069,14 +1069,14 @@ void CPUParticles2D::_particles_process(double p_delta) {Vector2 pos = p.transform[2];//apply linear acceleration- force += p.velocity.length() > 0.0 ? p.velocity.normalized() * tex_linear_accel * Math::lerp(parameters_min[PARAM_LINEAR_ACCEL], parameters_max[PARAM_LINEAR_ACCEL], rand_from_seed(_seed)) : Vector2();+ force += p.velocity.length() > 0.0f ? p.velocity.normalized() * tex_linear_accel * Math::lerp(parameters_min[PARAM_LINEAR_ACCEL], parameters_max[PARAM_LINEAR_ACCEL], rand_from_seed(_seed)) : Vector2();//apply radial accelerationVector2 org = emission_xform[2];Vector2 diff = pos - org;- force += diff.length() > 0.0 ? diff.normalized() * (tex_radial_accel)*Math::lerp(parameters_min[PARAM_RADIAL_ACCEL], parameters_max[PARAM_RADIAL_ACCEL], rand_from_seed(_seed)) : Vector2();+ force += diff.length() > 0.0f ? diff.normalized() * (tex_radial_accel)*Math::lerp(parameters_min[PARAM_RADIAL_ACCEL], parameters_max[PARAM_RADIAL_ACCEL], rand_from_seed(_seed)) : Vector2();//apply tangential acceleration;Vector2 yx = Vector2(diff.y, diff.x);- force += yx.length() > 0.0 ? (yx * Vector2(-1.0, 1.0)).normalized() * (tex_tangential_accel * Math::lerp(parameters_min[PARAM_TANGENTIAL_ACCEL], parameters_max[PARAM_TANGENTIAL_ACCEL], rand_from_seed(_seed))) : Vector2();+ force += yx.length() > 0.0f ? (yx * Vector2(-1.0f, 1.0f)).normalized() * (tex_tangential_accel * Math::lerp(parameters_min[PARAM_TANGENTIAL_ACCEL], parameters_max[PARAM_TANGENTIAL_ACCEL], rand_from_seed(_seed))) : Vector2();//apply attractor forcesp.velocity += force * local_delta;//orbit velocity@@ -1165,7 +1165,7 @@ void CPUParticles2D::_particles_process(double p_delta) {p.color *= p.base_color * p.start_color_rand;if (particle_flags[PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY]) {- if (p.velocity.length() > 0.0) {+ if (p.velocity.length() > 0.0f) {p.transform.columns[1] = p.velocity;}diff --git a/scene/2d/physics/character_body_2d.cpp b/scene/2d/physics/character_body_2d.cppindex 374676be128..c70acc5a60f 100644--- a/scene/2d/physics/character_body_2d.cpp+++ b/scene/2d/physics/character_body_2d.cpp@@ -156,7 +156,7 @@ void CharacterBody2D::_move_and_slide_grounded(double p_delta, bool p_was_on_flo// If we hit a ceiling platform, we set the vertical velocity to at least the platform one.if (on_ceiling && result.collider_velocity != Vector2() && result.collider_velocity.dot(up_direction) < 0) {// If ceiling sliding is on, only apply when the ceiling is flat or when the motion is upward.- if (!slide_on_ceiling || motion.dot(up_direction) < 0 || (result.collision_normal + up_direction).length() < 0.01) {+ if (!slide_on_ceiling || motion.dot(up_direction) < 0 || (result.collision_normal + up_direction).length() < 0.01f) {apply_ceiling_velocity = true;Vector2 ceiling_vertical_velocity = up_direction * up_direction.dot(result.collider_velocity);Vector2 motion_vertical_velocity = up_direction * up_direction.dot(velocity);@@ -166,7 +166,7 @@ void CharacterBody2D::_move_and_slide_grounded(double p_delta, bool p_was_on_flo}}- if (on_floor && floor_stop_on_slope && (velocity.normalized() + up_direction).length() < 0.01) {+ if (on_floor && floor_stop_on_slope && (velocity.normalized() + up_direction).length() < 0.01f) {Transform2D gt = get_global_transform();if (result.travel.length() <= margin + CMP_EPSILON) {gt.columns[2] -= result.travel;diff --git a/scene/3d/cpu_particles_3d.cpp b/scene/3d/cpu_particles_3d.cppindex 1b74765ddc0..456755aa243 100644--- a/scene/3d/cpu_particles_3d.cpp+++ b/scene/3d/cpu_particles_3d.cpp@@ -900,7 +900,7 @@ void CPUParticles3D::_particles_process(double p_delta) {}// rotate spread to directionVector3 binormal = Vector3(0.0, 1.0, 0.0).cross(direction_nrm);- if (binormal.length_squared() < 0.00000001) {+ if (binormal.length_squared() < 0.00000001f) {// direction is parallel to Y. Choose Z as the binormal.binormal = Vector3(0.0, 0.0, 1.0);}@@ -1084,19 +1084,19 @@ void CPUParticles3D::_particles_process(double p_delta) {position.z = 0.0;}//apply linear acceleration- force += p.velocity.length() > 0.0 ? p.velocity.normalized() * tex_linear_accel * Math::lerp(parameters_min[PARAM_LINEAR_ACCEL], parameters_max[PARAM_LINEAR_ACCEL], rand_from_seed(alt_seed)) : Vector3();+ force += p.velocity.length() > 0.0f ? p.velocity.normalized() * tex_linear_accel * Math::lerp(parameters_min[PARAM_LINEAR_ACCEL], parameters_max[PARAM_LINEAR_ACCEL], rand_from_seed(alt_seed)) : Vector3();//apply radial accelerationVector3 org = emission_xform.origin;Vector3 diff = position - org;- force += diff.length() > 0.0 ? diff.normalized() * (tex_radial_accel)*Math::lerp(parameters_min[PARAM_RADIAL_ACCEL], parameters_max[PARAM_RADIAL_ACCEL], rand_from_seed(alt_seed)) : Vector3();+ force += diff.length() > 0.0f ? diff.normalized() * (tex_radial_accel)*Math::lerp(parameters_min[PARAM_RADIAL_ACCEL], parameters_max[PARAM_RADIAL_ACCEL], rand_from_seed(alt_seed)) : Vector3();if (particle_flags[PARTICLE_FLAG_DISABLE_Z]) {Vector2 yx = Vector2(diff.y, diff.x);Vector2 yx2 = (yx * Vector2(-1.0, 1.0)).normalized();- force += yx.length() > 0.0 ? Vector3(yx2.x, yx2.y, 0.0) * (tex_tangential_accel * Math::lerp(parameters_min[PARAM_TANGENTIAL_ACCEL], parameters_max[PARAM_TANGENTIAL_ACCEL], rand_from_seed(alt_seed))) : Vector3();+ force += yx.length() > 0.0f ? Vector3(yx2.x, yx2.y, 0.0f) * (tex_tangential_accel * Math::lerp(parameters_min[PARAM_TANGENTIAL_ACCEL], parameters_max[PARAM_TANGENTIAL_ACCEL], rand_from_seed(alt_seed))) : Vector3();} else {Vector3 crossDiff = diff.normalized().cross(gravity.normalized());- force += crossDiff.length() > 0.0 ? crossDiff.normalized() * (tex_tangential_accel * Math::lerp(parameters_min[PARAM_TANGENTIAL_ACCEL], parameters_max[PARAM_TANGENTIAL_ACCEL], rand_from_seed(alt_seed))) : Vector3();+ force += crossDiff.length() > 0.0f ? crossDiff.normalized() * (tex_tangential_accel * Math::lerp(parameters_min[PARAM_TANGENTIAL_ACCEL], parameters_max[PARAM_TANGENTIAL_ACCEL], rand_from_seed(alt_seed))) : Vector3();}//apply attractor forcesp.velocity += force * local_delta;@@ -1196,7 +1196,7 @@ void CPUParticles3D::_particles_process(double p_delta) {if (particle_flags[PARTICLE_FLAG_DISABLE_Z]) {if (particle_flags[PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY]) {- if (p.velocity.length() > 0.0) {+ if (p.velocity.length() > 0.0f) {p.transform.basis.set_column(1, p.velocity.normalized());} else {p.transform.basis.set_column(1, p.transform.basis.get_column(1));@@ -1213,7 +1213,7 @@ void CPUParticles3D::_particles_process(double p_delta) {} else {//orient particle Y towards velocityif (particle_flags[PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY]) {- if (p.velocity.length() > 0.0) {+ if (p.velocity.length() > 0.0f) {p.transform.basis.set_column(1, p.velocity.normalized());} else {p.transform.basis.set_column(1, p.transform.basis.get_column(1).normalized());diff --git a/scene/3d/gpu_particles_collision_3d.cpp b/scene/3d/gpu_particles_collision_3d.cppindex 5bc827270e8..3e2c11e0934 100644--- a/scene/3d/gpu_particles_collision_3d.cpp+++ b/scene/3d/gpu_particles_collision_3d.cpp@@ -330,7 +330,7 @@ void GPUParticlesCollisionSDF3D::_find_closest_distance(const Vector3 &p_pos, cobool pass = true;if (!p_bvh[p_bvh_cell].bounds.has_point(p_pos)) {//outside, find closest point- Vector3 he = p_bvh[p_bvh_cell].bounds.size * 0.5;+ Vector3 he = p_bvh[p_bvh_cell].bounds.size * 0.5f;Vector3 center = p_bvh[p_bvh_cell].bounds.position + he;Vector3 rel = (p_pos - center).abs();diff --git a/scene/3d/physics/character_body_3d.cpp b/scene/3d/physics/character_body_3d.cppindex 19bebc581ba..71f86af35da 100644--- a/scene/3d/physics/character_body_3d.cpp+++ b/scene/3d/physics/character_body_3d.cpp@@ -183,7 +183,7 @@ void CharacterBody3D::_move_and_slide_grounded(double p_delta, bool p_was_on_flo// If we hit a ceiling platform, we set the vertical velocity to at least the platform one.if (collision_state.ceiling && platform_ceiling_velocity != Vector3() && platform_ceiling_velocity.dot(up_direction) < 0) {// If ceiling sliding is on, only apply when the ceiling is flat or when the motion is upward.- if (!slide_on_ceiling || motion.dot(up_direction) < 0 || (ceiling_normal + up_direction).length() < 0.01) {+ if (!slide_on_ceiling || motion.dot(up_direction) < 0 || (ceiling_normal + up_direction).length() < 0.01f) {apply_ceiling_velocity = true;Vector3 ceiling_vertical_velocity = up_direction * up_direction.dot(platform_ceiling_velocity);Vector3 motion_vertical_velocity = up_direction * up_direction.dot(velocity);@@ -193,7 +193,7 @@ void CharacterBody3D::_move_and_slide_grounded(double p_delta, bool p_was_on_flo}}- if (collision_state.floor && floor_stop_on_slope && (velocity.normalized() + up_direction).length() < 0.01) {+ if (collision_state.floor && floor_stop_on_slope && (velocity.normalized() + up_direction).length() < 0.01f) {Transform3D gt = get_global_transform();if (result.travel.length() <= margin + CMP_EPSILON) {gt.origin -= result.travel;@@ -232,7 +232,7 @@ void CharacterBody3D::_move_and_slide_grounded(double p_delta, bool p_was_on_flo// Cancel the motion.Transform3D gt = get_global_transform();real_t travel_total = result.travel.length();- real_t cancel_dist_max = MIN(0.1, margin * 20);+ real_t cancel_dist_max = MIN(0.1f, margin * 20.0f);if (travel_total <= margin + CMP_EPSILON) {gt.origin -= result.travel;result.travel = Vector3(); // Cancel for constant speed computation.diff --git a/scene/3d/voxelizer.cpp b/scene/3d/voxelizer.cppindex dd4b0a2bb3f..6cc01c2580a 100644--- a/scene/3d/voxelizer.cpp+++ b/scene/3d/voxelizer.cpp@@ -614,7 +614,7 @@ void Voxelizer::_fixup_plot(int p_idx, int p_level) {bake_cells.write[p_idx].normal[2] /= alpha;Vector3 n(bake_cells[p_idx].normal[0], bake_cells[p_idx].normal[1], bake_cells[p_idx].normal[2]);- if (n.length() < 0.01) {+ if (n.length() < 0.01f) {//too much fight over normal, zero itbake_cells.write[p_idx].normal[0] = 0;bake_cells.write[p_idx].normal[1] = 0;diff --git a/scene/gui/color_picker_shape.cpp b/scene/gui/color_picker_shape.cppindex 460e2c72928..20491ce6b69 100644--- a/scene/gui/color_picker_shape.cpp+++ b/scene/gui/color_picker_shape.cpp@@ -745,7 +745,7 @@ void ColorPickerShapeWheel::_wheel_input(const Ref<InputEvent> &p_event) {if (is_click && !spinning) {real_t dist = center.distance_to(event_position);- if (dist >= center.x * WHEEL_RADIUS * 2.0 && dist <= center.x) {+ if (dist >= center.x * (WHEEL_RADIUS * 2.0f) && dist <= center.x) {spinning = true;if (!wheel_focused) {cursor_editing = true;diff --git a/scene/main/viewport.cpp b/scene/main/viewport.cppindex d0e4145acc6..1a9d863f985 100644--- a/scene/main/viewport.cpp+++ b/scene/main/viewport.cpp@@ -2056,7 +2056,7 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) {Viewport *section_root = get_section_root_viewport();if (!gui.drag_attempted && gui.mouse_focus && section_root && !section_root->gui.global_dragging && (mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {gui.drag_accum += mm->get_relative();- float len = gui.drag_accum.length();+ real_t len = gui.drag_accum.length();if (len > gui.drag_threshold) {{ // Attempt grab, try parent controls too.CanvasItem *ci = gui.mouse_focus;