Jelajahi Sumber

加速度调整

lvjincheng 4 tahun lalu
induk
melakukan
3cdc9140d8

+ 53 - 21
Assets/BowArrow/Scripts/Bluetooth/ShootCheck.cs

@@ -4,6 +4,7 @@ using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using ArduinoBluetoothAPI;
+using System.Linq;
 using BestHTTP.WebSocket;
 
 public class ShootCheck : MonoBehaviour
@@ -13,6 +14,7 @@ public class ShootCheck : MonoBehaviour
     bool locked = false;
     float maxAcc = 0;
     Queue<float> keyAccList = new Queue<float>();
+    Queue<string> keyTimeList = new Queue<string>();
     public float shootSpeed;
     public static ShootCheck ins; 
     [SerializeField] InputField ipInputField = default;
@@ -39,7 +41,7 @@ public class ShootCheck : MonoBehaviour
 //socket
         string ipStr = ipInputField.text;//ipInputField.GetComponentInChildren<Text>();
         string serverIP = ipStr;
-        // serverIP = "192.168.1.109";
+        //serverIP = "172.16.20.57";
         string address = "ws://" + serverIP + ":8088/Ble/";
         webSocket = new WebSocket(new Uri(address));
 #if !UNITY_WEBGL
@@ -104,14 +106,14 @@ public class ShootCheck : MonoBehaviour
 
 
     [SerializeField] InputField ArmBowInputField = default;
-    public Text shootAngleText;
 
     public void SetShootBackTime() 
     {
         ArmBow.ins.shootBackTime=int.Parse(ArmBowInputField.text);
     }
     public void OnBluetoothReady(BluetoothShoot bluetoothShoot) {
-        bluetoothShoot.WriteData(JsonUtility.ToJson(cmd).Replace("\"", ""));
+        // bluetoothShoot.WriteData(JsonUtility.ToJson(cmd).Replace("\"", ""));
+        bluetoothShoot.WriteData("T");
     }
 
     public void OnDataReceived(byte[] bytes) {
@@ -120,30 +122,37 @@ public class ShootCheck : MonoBehaviour
             DeviceBatteryView.ins.RenderBattery(2, bytes[0]);
             return;
         }   
+        string str1 = "byte=";
+        if (webSocket != null)
+        { 
+            for (int i = 0; i < bytes.Length-1; i++)
+            {
+                str1+= bytes[i];
+            }
+             
+            // webSocket.Send(str2);    
+        } 
 
-        string str = "";
+         string str2 = "";
 
         for (int i = 0; i < (bytes.Length-2)/6; i++)
         {
             float acc = ToAcceleratedSpeed(bytes[i * 6 + 5], bytes[i * 6 + 6]);
 
             string t = "(采样时间:"+(int)bytes[i * 6 + 3] + "分"+ (int)bytes[i * 6 + 4]+"秒"+ TwoByteToInt(bytes[i * 6 + 1], bytes[i * 6 + 2])+"毫秒)" ;
-            str += "加速度:"+acc+t+"\n";
-
-            if (webSocket != null)
-            { 
-                webSocket.Send(str);    
-            }  
-
-            if (ins.check(acc) && ArmBow.ins) 
+            str2 += "加速度:"+acc+t+"\n";
+              
+            if (ins.check(acc,t) && ArmBow.ins) 
             {
-                ArmBow.ins.ADS_fire(); 
-                // if (webSocket != null)
-                // { 
-                //     webSocket.Send(str);    
-                // }   
+                ArmBow.ins.ADS_fire();  
             }
         }   
+
+        if (webSocket != null)
+        { 
+            string str3 = str1+"\n"+str2;
+            webSocket.Send(str3);    
+        } 
     }
 
     float ToAcceleratedSpeed(byte b1, byte b2)
@@ -159,7 +168,7 @@ public class ShootCheck : MonoBehaviour
         return (int)shortNum;
     }
 
-    bool check(float acc)
+    bool check(float acc,string t)
     {
         DebugLine.show(acc); //这个不需要注释,静态函数内置判断
         if (locked) 
@@ -173,6 +182,7 @@ public class ShootCheck : MonoBehaviour
                 maxAcc = acc;
             }
             keyAccList.Enqueue(acc);
+            keyTimeList.Enqueue(t);
             return false;
         } 
         else if (acc < cmd.getAcc() && maxAcc != 0) {
@@ -180,12 +190,17 @@ public class ShootCheck : MonoBehaviour
             shootSpeed = 0;
             float lasKeytAcc = 0;
             int keyAccIndex = 0;
+            float timeInterval = 0.01f;
             foreach (var keyAcc in keyAccList)
             {
                 if (keyAccIndex > 0)
                 {
-                    shootSpeed += keyAcc * 0.002f;
-                    shootSpeed -= (keyAcc - lasKeytAcc) * 0.002f / 2;
+                    shootSpeed += keyAcc * timeInterval;
+                    shootSpeed -= (keyAcc - lasKeytAcc) * timeInterval / 2;
+                }
+                else if (keyAccIndex == 0 && keyAccList.Count == 1) 
+                {
+                    shootSpeed = keyAcc * timeInterval;
                 }
                 lasKeytAcc = keyAcc;
                 keyAccIndex++;
@@ -193,10 +208,27 @@ public class ShootCheck : MonoBehaviour
             //加速度acc的单位是g,最后需要乘上
             shootSpeed *= 9.80665f;
             //积分出来的值还是太小,需要一个倍率
-            shootSpeed *= 10;
+            shootSpeed *= 30;
             Debug.LogWarning("初速度: " + shootSpeed + " 帧数: " + keyAccList.Count);
+            
+            
+            string str1 = "检测到射出的数据:\n";
+            for (int i=0;i<keyAccList.Count;i++)
+            {
+                float keyAcc = keyAccList.ElementAt(i);
+                string time = keyTimeList.ElementAt(i);
+             
+                str1 += "加速度:"+keyAcc+time+"\n"; 
+            }
+            Debug.LogWarning(str1);
+            if (webSocket != null)
+            { 
+                webSocket.Send(str1);    
+            }
             //本轮计算结束
             keyAccList.Clear();
+            keyTimeList.Clear();
+            
             maxAcc = 0;
             Dolock();
             Invoke("Unlock", 1.8f);

+ 2 - 1
Assets/BowArrow/Scripts/Game/ArmBow.cs

@@ -119,7 +119,7 @@ public class ArmBow : MonoBehaviour
     }
 
     void shoot() {
-        // Vector3 rayHitPoint = CrossHair.ins.getRayHitPoint();
+        RaycastHit raycastHit = CrossHair.ins.GetRaycastHit();
 
         // 筛选出一个稳定的发射角度---start
         Quaternion best_rotation = this.bowCamera.transform.rotation;
@@ -182,6 +182,7 @@ public class ArmBow : MonoBehaviour
         Arrow arrowComp = arrowCopy.AddComponent<Arrow>();
         arrowComp.armBow = this;
         arrowComp.shootOutPosition = shootOutPosition;
+        arrowComp.raycastHit = raycastHit;
         // Arrow.speed = DebugBowPower.ins.getPowerPercent() * 100f;
         // Arrow.speed = BaseSpeedSlider.ins.getValue();
         // Arrow.speed = Mathf.Pow(ShootCheck.ins.shootSpeed, ShootCheck.ins.shootSpeed < 13 ? 2f: (ShootCheck.ins.shootSpeed < 15 ? 2.5f : 3.0f));

+ 29 - 11
Assets/BowArrow/Scripts/Game/Arrow.cs

@@ -8,6 +8,9 @@ public class Arrow : MonoBehaviour
     public Vector3 shootOutPosition;
     public float flyTime = 0;
     public bool isHit = false;
+    public RaycastHit raycastHit;
+    private float mySpeed = 0;
+    public bool simulatedParabola = false; //模拟抛物线 
     public ArmBow armBow;
     public static float speed = GameMgr.RealSizeToGameSize(60);
 
@@ -19,19 +22,36 @@ public class Arrow : MonoBehaviour
     void Start()
     {
         newRigidbody = this.gameObject.AddComponent<Rigidbody>();
-        float speedCopy = speed;
+
+        mySpeed = speed;
         if (GameAssistUI.ins) {
-            speedCopy *= (1 + GameAssistUI.ins.shootScaleValue);
+            mySpeed *= (1 + GameAssistUI.ins.shootScaleValue);
         } 
 
-        //箭刚射出去时,角度往上抬一点
-        float distance = TargetBody.ins.GetDistance();
-        this.transform.Rotate(Vector3.left, Mathf.Clamp(distance / 50 * 20, 0, 20));
+        newRigidbody.velocity = this.transform.forward * mySpeed;
+        newRigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
 
+        //预测和判断是否需要抛物线动画
+        if (raycastHit.transform.gameObject.name == "TargetBody") {
+            Vector3 targetPosition = raycastHit.transform.position;
+            Vector3 headPosition = this.transform.position; 
+            float moveDistance = Vector3.Distance(targetPosition, headPosition);
+            float canFlyTime = Mathf.Sqrt(headPosition.y * 2 / 9.81f);
+            float estimateTime = moveDistance / mySpeed;
+            // if (estimateTime < canFlyTime) {
+            //     float t = 1.5f;
+            //     float g = 9.81f;
+            //     float vy =  g * t / 2;
+            //     float vx = moveDistance / t;
+            //     newRigidbody.velocity = new Vector3(0, vy, vx);
+            //     mySpeed = newRigidbody.velocity.magnitude;
+            // }
+            float distance = TargetBody.ins.GetDistance();
+            if (Mathf.RoundToInt(distance) >= 50) {
+                if (Random.value < 0.5) simulatedParabola = true;
+            }
+        }
 
-        newRigidbody.velocity = this.transform.forward * speedCopy;
-        newRigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
-        
         Transform cameraTF = this.transform.Find("Camera");
         cameraTF.gameObject.SetActive(true);
         cameraTF.gameObject.AddComponent<ArrowCamera>().arrow = this;
@@ -83,8 +103,6 @@ public class Arrow : MonoBehaviour
         this.activeEffectCyclone(false);
         this.activeEffectBomb(true);
         this.activeEffectTrail(false);
-        Debug.Log("Speed " + GameMgr.GameSizeToRealSize(Arrow.speed));
-        Debug.Log("Distance " + GameMgr.GameSizeToRealSize(Vector3.Distance(this.getHeadPosition(), shootOutPosition)));
     }
 
     public Vector3 getHeadPosition() {
@@ -174,6 +192,6 @@ public class Arrow : MonoBehaviour
     void activeEffectTrail(bool value)
     {
         this.transform.Find("EF_tuowei").gameObject.SetActive(value);
-        this.transform.Find("EF_tuowei/Trail").GetComponent<TrailRenderer>().time = 1.6f / speed;
+        this.transform.Find("EF_tuowei/Trail").GetComponent<TrailRenderer>().time = 1.6f / mySpeed;
     }
 }

+ 4 - 10
Assets/BowArrow/Scripts/Game/ArrowCamera.cs

@@ -10,17 +10,11 @@ public class ArrowCamera : MonoBehaviour
 
     void Start()
     {
-        float distance = TargetBody.ins.GetDistance();
-        if (Mathf.RoundToInt(distance) >= 50)
-        {
-            if (Random.value < 0.5) arrowCameraTemplate = new ArrowCameraTemplate1(this);    
-            else arrowCameraTemplate = new ArrowCameraTemplate2(this);
-        }
-        else 
-        {
-            arrowCameraTemplate = new ArrowCameraTemplate1(this);    
+        if (arrow.simulatedParabola) {
+            arrowCameraTemplate = new ArrowCameraTemplate2(this);
+        } else {
+            arrowCameraTemplate = new ArrowCameraTemplate1(this);  
         }
-        // arrowCameraTemplate = new ArrowCameraTemplate2(this);
     }
 
     void FixedUpdate()

+ 4 - 4
Assets/BowArrow/Scripts/Game/CrossHair.cs

@@ -35,13 +35,13 @@ public class CrossHair : MonoBehaviour
         this.GetComponent<Image>().enabled = this.visiable;
     }
 
-    public Vector3 GetRayHitPoint() {
-        float maxDistance = 60;
+    public RaycastHit GetRaycastHit() {
+        float maxDistance = 100;
         int layerMask = 1 << 8;
         RaycastHit hit;
         Ray ray = Camera.main.ScreenPointToRay(this.transform.position, Camera.MonoOrStereoscopicEye.Mono);
         bool raycastResult = Physics.Raycast(ray.origin, ray.direction, out hit, maxDistance, layerMask);
-        if (raycastResult) return hit.point;
-        else return Camera.main.transform.position + Camera.main.transform.forward * maxDistance;
+        // if (raycastResult) return hit.point;
+        return hit;
     }    
 }