【Unity】Mathd系列(二)Double型Vector3

本文探讨了在Unity中使用Double型Vector3时可能遇到的精度问题,通过对比与常规float型Vector3的方法,揭示了由于精度差异导致的细微偏差。作者提供了相关的github资源链接供进一步研究。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    Vector3可是说是比较常用的也是最容易出现精度问题的一个。经过测试发现,有一些方法跟原有的Vector3d比起来会有一定的偏差,这些都与float型的精度有关只是多数情况下近似即可。

Vector3d:

/// Vector3d.cs
/// 
/// The double type version of the Unity struct Vector3.
/// It can solve the problem that the float type may not be accurate enough.
/// 
/// Unity Vector3结构体的double版实现,以解决float型精度可能不够的问题。
/// 
/// Created by D子宇 on 2018.3.17 
/// 
/// Email: darkziyu@126.com
using System;
using UnityEngine;
using UnityEngine.Internal;

namespace Mathd
{
    public struct Vector3d
    {
        #region public members

        public double x;
        public double y;
        public double z;

        #endregion

        #region constructor

        public Vector3d(double p_x, double p_y)
        {
            x = p_x;
            y = p_y;
            z = 0;
        }
        public Vector3d(double p_x, double p_y, double p_z)
        {
            x = p_x;
            y = p_y;
            z = p_z;
        }

        #endregion

        #region public properties

        public double this[int index]
        {
            get
            {
                switch (index)
                {
                    case 0:
                        return x;
                    case 1:
                        return y;
                    case 2:
                        return z;
                    default:
                        throw new IndexOutOfRangeException("Invalid Vector3d index!");
                }
            }
            set
            {
                switch (index)
                {
                    case 0:
                        x = value;
                        break;
                    case 1:
                        y = value;
                        break;
                    case 2:
                        z = value;
                        break;
                    default:
                        throw new IndexOutOfRangeException("Invalid Vector3d index!");
                }
            }
        }

        public static Vector3d back
        {
            get
            {
                return new Vector3d(0, 0, -1);
            }
        }
        public static Vector3d down
        {
            get
            {
                return new Vector3d(0, -1, 0);
            }
        }
        public static Vector3d forward
        {
            get
            {
                return new Vector3d(0, 0, 1);
            }
        }
        public static Vector3d fwd
        {
            get
            {
                return new Vector3d(0, 0, 1);
            }
        }
        public static Vector3d left
        {
            get
            {
                return new Vector3d(-1, 0, 0);
            }
        }
        public static Vector3d one
        {
            get
            {
                return new Vector3d(1, 1, 1);
            }
        }
        public static Vector3d right
        {
            get
            {
                return new Vector3d(1, 0, 0);
            }
        }
        public static Vector3d up
        {
            get
            {
                return new Vector3d(0, 1, 0);
            }
        }
        public static Vector3d zero
        {
            get
            {
                return new Vector3d(0, 0, 0);
            }
        }
        public double magnitude
        {
            get
            {
                return Math.Sqrt(sqrMagnitude);
            }
        }
        public Vector3d normalized
        {
            get
            {
                return Normalize(this);
            }
        }
        public double sqrMagnitude
        {
            get
            {
                return x * x + y * y + z * z;
            }
        }

        #endregion

        #region public functions

        /// <summary>
        /// 夹角大小
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <returns></returns>
        public static float Angle(Vector3d from, Vector3d to)
        {
            double cos = Dot(from.normalized, to.normalized);
            if (cos < -1)
            {
                cos = -1;
            }
            if (cos > 1)
            {
                cos = 1;
            }
            return (float)(Math.Acos(cos) * (180/ Math.PI));
        }
        /// <summary>
        /// 夹角大小(弧度)
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <returns></returns>
        public static float AngleBetween(Vector3d from, Vector3d to)
        {
            double cos = Dot(from.normalized, to.normalized);
            if (cos < -1)
            {
                cos = -1;
            }
            if (cos > 1)
            {
                cos = 1;
            }
            return (float)(Math.Acos(cos));
        }
        /// <summary>
        /// 距离限制
        /// </summary>
        /// <param name="vector"></param>
        /// <param name="maxLength"></param>
        /// <returns></returns>
        public static Vector3d ClampMagnitude(Vector3d vector, double maxLength)
        {
            if (vector.sqrMagnitude > maxLength * maxLength)
            {
                return vector.normalized * maxLength;
            }
            else
            {
                return vector;
            }
        }
        /// <summary>
        /// 差乘
        /// </summary>
        /// <param name="lhs"></param>
        /// <param n
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值