Browse Source

Use builtin Vector2 functions for calculation of angles.

.
Anilforextra 4 years ago
parent
commit
a1f616dcfc

+ 1 - 1
core/math/transform_2d.cpp

@@ -271,7 +271,7 @@ Transform2D Transform2D::interpolate_with(const Transform2D &p_transform, real_t
 	}
 	}
 
 
 	//construct matrix
 	//construct matrix
-	Transform2D res(Math::atan2(v.y, v.x), p1.lerp(p2, p_c));
+	Transform2D res(v.angle(), p1.lerp(p2, p_c));
 	res.scale_basis(s1.lerp(s2, p_c));
 	res.scale_basis(s1.lerp(s2, p_c));
 	return res;
 	return res;
 }
 }

+ 1 - 1
core/math/vector2.cpp

@@ -79,7 +79,7 @@ real_t Vector2::angle_to(const Vector2 &p_vector2) const {
 }
 }
 
 
 real_t Vector2::angle_to_point(const Vector2 &p_vector2) const {
 real_t Vector2::angle_to_point(const Vector2 &p_vector2) const {
-	return Math::atan2(y - p_vector2.y, x - p_vector2.x);
+	return (*this - p_vector2).angle();
 }
 }
 
 
 real_t Vector2::dot(const Vector2 &p_other) const {
 real_t Vector2::dot(const Vector2 &p_other) const {

+ 1 - 1
editor/plugins/canvas_item_editor_plugin.cpp

@@ -2927,7 +2927,7 @@ void CanvasItemEditor::_draw_ruler_tool() {
 		viewport->draw_string(font, text_pos, TS->format_number(vformat("%.1f px", length_vector.length())), HALIGN_LEFT, -1, font_size, font_color, outline_size, outline_color);
 		viewport->draw_string(font, text_pos, TS->format_number(vformat("%.1f px", length_vector.length())), HALIGN_LEFT, -1, font_size, font_color, outline_size, outline_color);
 
 
 		if (draw_secondary_lines) {
 		if (draw_secondary_lines) {
-			const real_t horizontal_angle_rad = atan2(length_vector.y, length_vector.x);
+			const real_t horizontal_angle_rad = length_vector.angle();
 			const real_t vertical_angle_rad = Math_PI / 2.0 - horizontal_angle_rad;
 			const real_t vertical_angle_rad = Math_PI / 2.0 - horizontal_angle_rad;
 			const int horizontal_angle = round(180 * horizontal_angle_rad / Math_PI);
 			const int horizontal_angle = round(180 * horizontal_angle_rad / Math_PI);
 			const int vertical_angle = round(180 * vertical_angle_rad / Math_PI);
 			const int vertical_angle = round(180 * vertical_angle_rad / Math_PI);

+ 1 - 1
scene/2d/cpu_particles_2d.cpp

@@ -727,7 +727,7 @@ void CPUParticles2D::_particles_process(double p_delta) {
 			p.hue_rot_rand = Math::randf();
 			p.hue_rot_rand = Math::randf();
 			p.anim_offset_rand = Math::randf();
 			p.anim_offset_rand = Math::randf();
 
 
-			real_t angle1_rad = Math::atan2(direction.y, direction.x) + Math::deg2rad((Math::randf() * 2.0 - 1.0) * spread);
+			real_t angle1_rad = direction.angle() + Math::deg2rad((Math::randf() * 2.0 - 1.0) * spread);
 			Vector2 rot = Vector2(Math::cos(angle1_rad), Math::sin(angle1_rad));
 			Vector2 rot = Vector2(Math::cos(angle1_rad), Math::sin(angle1_rad));
 			p.velocity = rot * Math::lerp(parameters_min[PARAM_INITIAL_LINEAR_VELOCITY], parameters_min[PARAM_INITIAL_LINEAR_VELOCITY], Math::randf());
 			p.velocity = rot * Math::lerp(parameters_min[PARAM_INITIAL_LINEAR_VELOCITY], parameters_min[PARAM_INITIAL_LINEAR_VELOCITY], Math::randf());
 
 

+ 1 - 1
scene/2d/navigation_region_2d.cpp

@@ -464,7 +464,7 @@ void NavigationRegion2D::_notification(int p_what) {
 					draw_line(a, b, doors_color);
 					draw_line(a, b, doors_color);
 
 
 					// Draw a circle to illustrate the margins.
 					// Draw a circle to illustrate the margins.
-					real_t angle = (b - a).angle();
+					real_t angle = b.angle_to_point(a);
 					draw_arc(a, radius, angle + Math_PI / 2.0, angle - Math_PI / 2.0 + Math_TAU, 10, doors_color);
 					draw_arc(a, radius, angle + Math_PI / 2.0, angle - Math_PI / 2.0 + Math_TAU, 10, doors_color);
 					draw_arc(b, radius, angle - Math_PI / 2.0, angle + Math_PI / 2.0, 10, doors_color);
 					draw_arc(b, radius, angle - Math_PI / 2.0, angle + Math_PI / 2.0, 10, doors_color);
 				}
 				}

+ 1 - 1
scene/2d/skeleton_2d.cpp

@@ -456,7 +456,7 @@ void Bone2D::calculate_length_and_rotation() {
 			if (child) {
 			if (child) {
 				Vector2 child_local_pos = to_local(child->get_global_position());
 				Vector2 child_local_pos = to_local(child->get_global_position());
 				length = child_local_pos.length();
 				length = child_local_pos.length();
-				bone_angle = Math::atan2(child_local_pos.normalized().y, child_local_pos.normalized().x);
+				bone_angle = child_local_pos.normalized().angle();
 				calculated = true;
 				calculated = true;
 				break;
 				break;
 			}
 			}

+ 4 - 4
scene/gui/color_picker.cpp

@@ -827,7 +827,7 @@ void ColorPicker::_uv_input(const Ref<InputEvent> &p_event, Control *c) {
 				real_t dist = center.distance_to(bev->get_position());
 				real_t dist = center.distance_to(bev->get_position());
 
 
 				if (dist <= center.x) {
 				if (dist <= center.x) {
-					real_t rad = Math::atan2(bev->get_position().y - center.y, bev->get_position().x - center.x);
+					real_t rad = bev->get_position().angle_to_point(center);
 					h = ((rad >= 0) ? rad : (Math_TAU + rad)) / Math_TAU;
 					h = ((rad >= 0) ? rad : (Math_TAU + rad)) / Math_TAU;
 					s = CLAMP(dist / center.x, 0, 1);
 					s = CLAMP(dist / center.x, 0, 1);
 				} else {
 				} else {
@@ -844,7 +844,7 @@ void ColorPicker::_uv_input(const Ref<InputEvent> &p_event, Control *c) {
 						real_t dist = center.distance_to(bev->get_position());
 						real_t dist = center.distance_to(bev->get_position());
 
 
 						if (dist >= center.x * 0.84 && dist <= center.x) {
 						if (dist >= center.x * 0.84 && dist <= center.x) {
-							real_t rad = Math::atan2(bev->get_position().y - center.y, bev->get_position().x - center.x);
+							real_t rad = bev->get_position().angle_to_point(center);
 							h = ((rad >= 0) ? rad : (Math_TAU + rad)) / Math_TAU;
 							h = ((rad >= 0) ? rad : (Math_TAU + rad)) / Math_TAU;
 							spinning = true;
 							spinning = true;
 						} else {
 						} else {
@@ -889,12 +889,12 @@ void ColorPicker::_uv_input(const Ref<InputEvent> &p_event, Control *c) {
 		Vector2 center = c->get_size() / 2.0;
 		Vector2 center = c->get_size() / 2.0;
 		if (picker_type == SHAPE_VHS_CIRCLE) {
 		if (picker_type == SHAPE_VHS_CIRCLE) {
 			real_t dist = center.distance_to(mev->get_position());
 			real_t dist = center.distance_to(mev->get_position());
-			real_t rad = Math::atan2(mev->get_position().y - center.y, mev->get_position().x - center.x);
+			real_t rad = mev->get_position().angle_to_point(center);
 			h = ((rad >= 0) ? rad : (Math_TAU + rad)) / Math_TAU;
 			h = ((rad >= 0) ? rad : (Math_TAU + rad)) / Math_TAU;
 			s = CLAMP(dist / center.x, 0, 1);
 			s = CLAMP(dist / center.x, 0, 1);
 		} else {
 		} else {
 			if (spinning) {
 			if (spinning) {
-				real_t rad = Math::atan2(mev->get_position().y - center.y, mev->get_position().x - center.x);
+				real_t rad = mev->get_position().angle_to_point(center);
 				h = ((rad >= 0) ? rad : (Math_TAU + rad)) / Math_TAU;
 				h = ((rad >= 0) ? rad : (Math_TAU + rad)) / Math_TAU;
 			} else {
 			} else {
 				real_t corner_x = (c == wheel_uv) ? center.x - Math_SQRT12 * c->get_size().width * 0.42 : 0;
 				real_t corner_x = (c == wheel_uv) ? center.x - Math_SQRT12 * c->get_size().width * 0.42 : 0;

+ 1 - 1
scene/resources/skeleton_modification_2d_twoboneik.cpp

@@ -144,7 +144,7 @@ void SkeletonModification2DTwoBoneIK::_execute(float p_delta) {
 	// With modifications by TwistedTwigleg
 	// With modifications by TwistedTwigleg
 	Vector2 target_difference = target->get_global_position() - joint_one_bone->get_global_position();
 	Vector2 target_difference = target->get_global_position() - joint_one_bone->get_global_position();
 	float joint_one_to_target = target_difference.length();
 	float joint_one_to_target = target_difference.length();
-	float angle_atan = Math::atan2(target_difference.y, target_difference.x);
+	float angle_atan = target_difference.angle();
 
 
 	float bone_one_length = joint_one_bone->get_length() * MIN(joint_one_bone->get_global_scale().x, joint_one_bone->get_global_scale().y);
 	float bone_one_length = joint_one_bone->get_length() * MIN(joint_one_bone->get_global_scale().x, joint_one_bone->get_global_scale().y);
 	float bone_two_length = joint_two_bone->get_length() * MIN(joint_two_bone->get_global_scale().x, joint_two_bone->get_global_scale().y);
 	float bone_two_length = joint_two_bone->get_length() * MIN(joint_two_bone->get_global_scale().x, joint_two_bone->get_global_scale().y);