如果出現重復 - 我已經從 stackoverflow 中閱讀了其他帖子。它們中的大多數都與 Unity3D 相關聯,并且其中很多都使用了諸如 theta/omega 之類的關鍵字。我無法理解任何這些東西。此外,我對這些公式符號的了解為 0,因此我理解所讀內容的 0%。
我嘗試為視頻游戲制作機器人,我唯一遇到的問題是如何獲得所需的旋轉度,直到我面對給定的坐標。在這里,我快速舉例說明了我的編程問題。
我在這種情況下知道的事情:
- 玩家 1 位置
- Player1 輪換
- Player2 位置(我需要面對這個玩家)
情況的二維地圖。點擊這里
這是它在 c# 中的樣子
using System;
public class Program
{
// Player 1 cordinates
public static float p1x = 2;
public static float p1y = 3;
public static float p1rotation = 0; // In degrees 0-360
// Player 2 cordinates
public static float p2x = 6;
public static float p2y = 4;
// Player 1 needed degree to face Player 2
public static float needed_distance;
public static void Main()
{
needed_distance = ??; // no clue how to get this value ...
Console.WriteLine("The needed distance is: " needed_distance);
}
}
您也可以編輯此代碼并直接在此處執行 -> https://dotnetfiddle.net/b4PTcw
請不要將此標記為重復,我的數學技能甚至低于零。如果有人試圖用我做的例子來回答我,我會更好地理解。
uj5u.com熱心網友回復:
您正在尋找Math.Atan2方法:
private static float Rotation(float p1x, float p1y, float p2x, float p2y) =>
(float)(Math.Atan2(p1x - p2x, p2y - p1y) * 180.0 / Math.PI 630) % 360.0f;
private static float Distance(float p1x, float p1y, float p2x, float p2y) =>
(float)Math.Sqrt((p1x - p2x) * (p1x - p2x) (p1y - p2y) * (p1y - p2y));
演示:
Console.WriteLine(Rotation(2, 3, 6, 4));
結果:
194.0362548828125
所以,如果玩家#1 首字母rotation1 = 0(s),他需要以 ~194度轉彎。如果玩家 #1任意初始rotation1:
float rotation1 = ...;
float requiredRotation = (Rotation(p1x, p1y, p2x, p2y) -
(rotation1 % 360f) 360f) % 360f;
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/396025.html
