【Unity】Mathd系列(一)Double型Vector2

本文探讨了在Unity中使用float型Vector2时遇到的精度问题,并介绍了为解决这一问题自定义的Double型Vector2结构体,详细阐述了其实现和应用场景。
摘要由CSDN通过智能技术生成

    由于Unity中的向量用的都是float型去存储,而float型的精度只有五位小数,导致了前段时间做项目的时候出现了精度上的问题。当时重新写了一个简单的类去解决它,过后觉得以后还有可能还会遇到类似的问题,于是系统的写了一下Unity中的几个结构体。

首先是Vector2d:

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

namespace Mathd
{
    public struct Vector2d
    {
        #region public members

        public double x;
        public double y;

        #endregion

        #region constructor

        public Vector2d(double p_x,double p_y) {
            x = p_x;
            y = p_y;
        }

        #endregion

        #region public properties

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

        public static Vector2d down
        {
            get
            {
                return new Vector2d(0, -1);
            }
        }
        public static Vector2d left
        {
            get
            {
                return new Vector2d(-1, 0);
            }
        }
        public static Vector2d one
        {
            get
            {
                return new Vector2d(1, 1);
            }
        }
        public static Vector2d right
        {
            get
            {
                return new Vector2d(1, 0);
            }
        }
        public static Vector2d up
        {
            get
            {
                return new Vector2d(0, 1);
            }
        }
        public static Vector2d zero
        {
            get
            {
                return new Vector2d(0, 0);

            }
        }
        public double magnitude
        {
            get
            {
                return Math.Sqrt(sqrMagnitude);
            }
        }
        public Vector2d normalized
        {
            get
            {
                Vector2d result &#
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值