蓝色星空 1 năm trước cách đây
mục cha
commit
43ac02305d
2 tập tin đã thay đổi với 241 bổ sung0 xóa
  1. 230 0
      Assets/Scripts/Blue/Test/Gongshi2.cs
  2. 11 0
      Assets/Scripts/Blue/Test/Gongshi2.cs.meta

+ 230 - 0
Assets/Scripts/Blue/Test/Gongshi2.cs

@@ -0,0 +1,230 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using UnityEngine.UI;
+
+public class Gongshi2 : MonoBehaviour
+{
+
+    public InputField tw;
+    public InputField th;
+
+    public InputField sceneWidth;
+    public InputField sceneLength;
+
+    public InputField p2x;
+    public InputField p2y;
+
+    public InputField p2x2;
+    public InputField p2y2;
+
+    void Start()
+    {
+    }
+
+    public InputField eul;
+    public InputField p3x;
+    public InputField p3y;
+    public InputField p3Z;
+
+    public InputField eul2;
+    public InputField p3x2;
+    public InputField p3y2;
+    public InputField p3Z2;
+
+
+    //发送
+    public void send()
+    {
+        t.text = "";
+        F3Dto2D();
+        FEulTo2D();
+    }
+
+
+    public float EulChange(float eul)
+    {
+        if (eul < 0)
+        {
+            eul = 360 + eul;
+        }
+        log("eul==>" + eul);
+        return eul;
+    }
+    public void F3Dto2D()
+    {
+
+        // 3D 先转 2D (Z变Y) 
+        Vector2 p3dto2d = new Vector2(-float.Parse(p3x.text), -float.Parse(p3Z.text));
+
+        log("p3dto2d==>" + p3dto2d);
+        // 获取半径
+        float r = Vector3.Distance(p3dto2d, Vector3.zero);
+        Debug.Log("r==>" + r);
+
+        log("r==>" + r);
+        // 获取当前的旋转值
+        float e1 = F2DTOEULJS(p3dto2d, Vector2.zero);
+        if (e1 == float.NaN)
+        {
+            e1 = 0;
+        }
+        Debug.Log("e1==>" + e1);
+
+        log("e1==>" + e1);
+
+        // 获取旋转归零后的2D坐标
+        Vector2 a = new Vector2(r * Mathf.Cos(((e1 - EulChange(float.Parse(eul.text))) * (Mathf.PI * 2) / 360)), r * Mathf.Sin(((e1 - EulChange(float.Parse(eul.text))) * (Mathf.PI * 2) / 360)));
+
+        log("a==>" + a);
+        Debug.Log("a==>" + a);
+        // 移动到2D坐标点
+        a = -a;
+
+        log("a==>" + a);
+        Debug.Log("-a==>" + a);
+        // 等比转换
+        p3dto2d = new Vector2(a.x / float.Parse(sceneLength.text) * float.Parse(tw.text), (float.Parse(th.text) - (a.y / float.Parse(sceneWidth.text) * float.Parse(th.text))));
+        Debug.Log("p3dto2d==>" + p3dto2d);
+        //p3dto2d = new Vector2();
+
+        log("p3dto2d==>" + p3dto2d);
+        p2x.text = p3dto2d.x.ToString("F2");
+        p2y.text = p3dto2d.y.ToString("F2");
+
+
+    }
+    public void FEulTo2D()
+    {
+
+        Vector2 initialPosition = new Vector2(float.Parse(p2x.text), float.Parse(th.text) - float.Parse(p2y.text)); // 初始点的坐标 (x, y)
+
+        log("initialPosition==>" + initialPosition);
+
+        float rotationAngle = EulChange(float.Parse(eul.text)); // 旋转角度,这里设置为30度
+
+        log("rotationAngle==>" + rotationAngle);
+        // 将角度转换为弧度
+        float radians = Mathf.Deg2Rad * rotationAngle;
+
+        log("radians==>" + radians);
+        // 计算朝向后的坐标
+        Vector2 directionVector = new Vector2(Mathf.Cos(radians), Mathf.Sin(radians)) * 100;
+        log("directionVector==>" + directionVector);
+        Vector2 newPosition = initialPosition + directionVector;
+        log("newPosition==>" + newPosition);
+        Vector2 newPosition2 = new Vector2(newPosition.x, float.Parse(th.text) - newPosition.y);
+
+        Debug.Log("新的坐标:" + newPosition);
+        p2x2.text = newPosition2.x.ToString("F2");
+        p2y2.text = newPosition2.y.ToString("F2");
+
+    }
+
+
+    public float F2DTOEULJS(Vector2 v1, Vector2 v2)
+    {
+        Vector2 pointA = v1;
+        log("pointA==>" + pointA);
+        Debug.Log("pointA====>" + pointA);
+        Vector2 pointB = v2;
+        log("pointB==>" + pointB);
+        Debug.Log("pointB====>" + pointB);
+        Vector2 pointC = new Vector2(v1.x + 10, v1.y);
+        log("pointC==>" + pointC);
+        Debug.Log("pointC====>" + pointC);
+
+        // 计算三边的长度
+        float sideAB = Vector2.Distance(pointA, pointB);
+        log("sideAB==>" + sideAB);
+        Debug.Log("sideAB====>" + sideAB);
+        float sideBC = Vector2.Distance(pointB, pointC);
+        log("sideBC==>" + sideBC);
+        Debug.Log("sideBC====>" + sideBC);
+        float sideCA = Vector2.Distance(pointC, pointA);
+        log("sideCA==>" + sideCA);
+        Debug.Log("sideCA====>" + sideCA);
+        float angleB = 0;
+        if ((2f * sideCA * sideAB) != 0)
+        {
+            angleB = Mathf.Acos((sideCA * sideCA + sideAB * sideAB - sideBC * sideBC) / (2f * sideCA * sideAB));
+
+        }
+        // 计算三个角的弧度值
+
+        if (angleB == float.NaN)
+        {
+            angleB = 0;
+        }
+        log("angleB==>" + angleB);
+        if (v1.x > v2.x && v1.y > v2.y)
+        {
+            // 将弧度值转换为角度值(0 到 180 度)
+            angleB = 360 - angleB * Mathf.Rad2Deg;
+        }
+        else if (v1.x < v2.x && v1.y > v2.y)
+        {
+            angleB = 360 - angleB * Mathf.Rad2Deg;
+
+        }
+        else
+        {
+
+            angleB = angleB * Mathf.Rad2Deg;
+        }
+        log("angleB==>" + angleB);
+        return angleB;
+    }
+
+    //获取
+    public void get()
+    {
+        t.text = "";
+        F2Dto3D();
+    }
+    public void F2Dto3D()
+    {
+        //获取 2D等比转换 3D的 2D坐标系
+        Vector2 p1 = new Vector2(float.Parse(p2x.text) / float.Parse(tw.text) * float.Parse(sceneLength.text), ((float.Parse(th.text) - (float.Parse(p2y.text))) / float.Parse(th.text) * float.Parse(sceneWidth.text)));
+        log("p1==>" + p1);
+
+        //以2D的坐标点设置锚点
+        Vector2 p2 = Vector2.zero - p1;
+        log("p2==>" + p2);
+        //获取当前2D的旋转坐标 
+        float e1 = F2DTOEULJS(p2, Vector2.zero);
+        log("e1==>" + e1);
+
+        //求出半径 
+        float r = Vector3.Distance(p2, Vector3.zero);
+
+        log("p1==>" + r);
+
+        //计算旋转后的3D坐标 
+        Vector3 a = new Vector3(-r * Mathf.Cos((F2DTOEUL() + e1) * (Mathf.PI * 2) / 360), 0, -r * Mathf.Sin((F2DTOEUL() + e1) * (Mathf.PI * 2) / 360));
+        p3x2.text = a.x.ToString("F2");
+        p3y2.text = a.y.ToString("F2");
+        p3Z2.text = a.z.ToString("F2");
+        log("a==>" + a);
+
+
+    }
+    public float F2DTOEUL()
+    {
+        // 将弧度值转换为角度值(0 到 180 度)
+        float angleB = F2DTOEULJS(new Vector2(float.Parse(p2x.text), (float.Parse(th.text) - float.Parse(p2y.text))), new Vector2(float.Parse(p2x2.text), float.Parse(th.text) - float.Parse(p2y2.text)));
+
+        log("angleB==>" + angleB);
+        eul2.text = angleB.ToString("F2");
+        Debug.Log("角度 B:" + angleB);
+        //2DposA    2DposB 
+        return angleB;
+    }
+
+    public Text t;
+    public void log(object obj)
+    {
+        t.text += "\n" + obj.ToString();
+    }
+
+}

+ 11 - 0
Assets/Scripts/Blue/Test/Gongshi2.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: fe97a43f060e9064781b24c8c9c40a9e
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: