如何计算游戏角色从点1到点2的旋转角度?(C#游戏开发作业求助)
问题与解决方案
我正在完成Coursera上的C#游戏开发课程作业,任务是计算两点间距离,以及游戏角色从第一点移动到第二点需要的旋转角度。现在卡在旋转角度的计算上,不知道怎么用现有数值构建求解方程。以下是我目前写的代码:
using System; namespace ProgrammingAssignment2 { // IMPORTANT: Only add code in the section // indicated below. The code I've provided // makes your solution work with the // automated grader on Coursera /// <summary> /// Programming Assignment 2 /// </summary> class Program { // x and y coordinates for points static float point1X; static float point1Y; static float point2X; static float point2Y; /// <summary> /// Programming Assignment 2 /// </summary> /// <param name="args">command-line args</param> static void Main(string[] args) { // loop while there's more input string input = Console.ReadLine(); while (input[0] != 'q') { // extract point coordinates from string GetInputValuesFromString(input); // Add your code between this comment // and the comment below. You can of // course add more space between the // comments as needed int point1X = 5; int point2X = 4; int deltaX = -1; int deltaY = -1; int point1Y = 5; int point2Y = 4; int angle2 = -135; float hypotnuse = (deltaX * 2 + deltaY * 2); float distance = (float)Math.Sqrt((Math.Pow(point2X - point1X, 2) + Math.Pow(point2Y - point1X, 2))); float angle = (float)Math.Atan2(point2X - point1X, point2Y - point1Y); float CumulationofDistanceandHyp = ((distance * 2 + hypotnuse * 2)); Console.Write( distance + " and " + angle); Console.Write("The distance between delta Y and X is " + hypotnuse); Console.Write("Then what is " + CumulationofDistanceandHyp); // Don't add or modify any code below // this comment input = Console.ReadLine(); } } /// <summary> /// Extracts point coordinates from the given input string /// </summary> /// <param name="input">input string</param> static void GetInputValuesFromString(string input) { // extract point 1 x int spaceIndex = input.IndexOf(' '); point1X = float.Parse(input.Substring(0, spaceIndex)); // move along string and extract point 1 y input = input.Substring(spaceIndex + 1); spaceIndex = input.IndexOf(' '); point1Y = float.Parse(input.Substring(0, spaceIndex)); // move along string and extract point 2 x input = input.Substring(spaceIndex + 1); spaceIndex = input.IndexOf(' '); point2X = float.Parse(input.Substring(0, spaceIndex)); // point 2 y is the rest of the string input = input.Substring(spaceIndex + 1); point2Y = float.Parse(input); #region Unfortunately, Mono doesn't have a Split method! //string[] values = input.Split(' '); //point1X = float.Parse(values[0]); //point1Y = float.Parse(values[1]); //point2X = float.Parse(values[2]); //point2Y = float.Parse(values[3]); #endregion } } }
核心问题修正与说明
- 避免覆盖全局变量:你在
Main里定义的局部point1X、point2X等变量会覆盖类级别的全局变量,导致输入解析的坐标完全失效。直接使用类中已声明的全局变量即可。 - 距离计算修复:原代码中距离公式的第二项错误使用了
point2Y - point1X,正确应为point2Y - point1Y,标准距离公式为:float distance = (float)Math.Sqrt(Math.Pow(point2X - point1X, 2) + Math.Pow(point2Y - point1Y, 2)); - 旋转角度计算修正:
Math.Atan2的参数顺序是(y差值, x差值),对应游戏中从点1到点2的方向向量,正确调用应为:float radians = (float)Math.Atan2(point2Y - point1Y, point2X - point1X);- 如果课程要求输出角度而非弧度,需要转换:
float degrees = radians * 180f / (float)Math.PI; - 注意游戏坐标系的方向(比如0度是否朝右、旋转方向正负),如果结果和预期相反,可能需要调整符号或加偏移量(如
360 - degrees)。
- 清理冗余代码:删除硬编码的测试变量(如
deltaX、angle2)和无意义的计算变量(如hypotnuse、CumulationofDistanceandHyp),只保留作业要求的计算逻辑。 - 输出规范:按照作业要求输出距离和角度即可,避免冗余输出内容。
修正后的核心代码片段
// 计算两点间距离 float distance = (float)Math.Sqrt(Math.Pow(point2X - point1X, 2) + Math.Pow(point2Y - point1Y, 2)); // 计算旋转角度(弧度转角度) float angleRadians = (float)Math.Atan2(point2Y - point1Y, point2X - point1X); float angleDegrees = angleRadians * 180f / (float)Math.PI; // 按要求输出 Console.WriteLine($"{distance:F2} and {angleDegrees:F2}");
内容的提问来源于stack exchange,提问作者theUnderminer
相关产品推荐
相关产品推荐

