Exemple #1
0
 public static void CatmullRom(ref TSVector2 value1, ref TSVector2 value2, ref TSVector2 value3, ref TSVector2 value4,
                               FP amount, out TSVector2 result)
 {
     result = new TSVector2(
         TSMath.CatmullRom(value1.x, value2.x, value3.x, value4.x, amount),
         TSMath.CatmullRom(value1.y, value2.y, value3.y, value4.y, amount));
 }
Exemple #2
0
 public static void Barycentric(ref TSVector2 value1, ref TSVector2 value2, ref TSVector2 value3, FP amount1,
                                FP amount2, out TSVector2 result)
 {
     result = new TSVector2(
         TSMath.Barycentric(value1.x, value2.x, value3.x, amount1, amount2),
         TSMath.Barycentric(value1.y, value2.y, value3.y, amount1, amount2));
 }
Exemple #3
0
        //New 已测
        /// <summary>
        /// 检测点是否在矩形中
        /// </summary>
        /// <param name="sCenter"></param>
        /// <param name="sDir"></param>
        /// <param name="nHalfWidth"></param>
        /// <param name="nHalfHeight"></param>
        /// <param name="sPos"></param>
        /// <returns></returns>
        public static bool CheckRectangleAndPos(TSVector2 sCenter, TSVector2 sDir, FP nHalfWidth, FP nHalfHeight, TSVector2 sPos)
        {
            //推导链接:http://jingyan.baidu.com/article/2c8c281dfbf3dd0009252a7b.html
            //假设对图片上任意点(x,y),绕一个坐标点(cx,cy)逆时针旋转a角度后的新的坐标设为(x0, y0),有公式:
            //x0= (x - cx)*cos(a) - (y - cy)*sin(a) + cx ;
            //y0= (x - cx)*sin(a) + (y - cy)*cos(a) + cy ;

            //注意,当sDir与x轴大于180度的时候计算的是逆时针旋转的角度,小于180顺时针
            FP nAngle = TSVector2.Angle(sDir, TSVector2.up);

            if (sDir.x < 0)
            {
                //当计算的角度是顺时针角度需要转换为逆时针角度
                nAngle = 360 - nAngle;
            }
            FP angle  = nAngle * FP.Deg2Rad;
            FP nCos   = TSMath.Cos(angle);
            FP nSin   = TSMath.Sin(angle);
            FP deltaX = sPos.x - sCenter.x;
            FP deltaY = sPos.y - sCenter.y;
            FP nNewX  = deltaX * nCos - deltaY * nSin + sCenter.x;
            FP nNewY  = deltaX * nSin + deltaY * nCos + sCenter.y;

            sPos.x = nNewX;
            sPos.y = nNewY;
            return(CheckAabbAndPos(sCenter, nHalfWidth, nHalfHeight, sPos));
        }
Exemple #4
0
        public static TSVector2 Lerp(TSVector2 value1, TSVector2 value2, FP amount)
        {
            amount = TSMath.Clamp(amount, 0, 1);

            return(new TSVector2(
                       TSMath.Lerp(value1.x, value2.x, amount),
                       TSMath.Lerp(value1.y, value2.y, amount)));
        }
Exemple #5
0
        //New 已测
        /// <summary>
        /// 计算点到经过两点的直线的距离
        /// </summary>
        /// <param name="sPos"></param>
        /// <param name="sPos1"></param>
        /// <param name="sPos2"></param>
        /// <returns></returns>
        public static FP DistanceFromPointToLine(TSVector2 sPos, TSVector2 sPos1, TSVector2 sPos2)
        {
            TSVector2 line1 = sPos2 - sPos1;
            TSVector2 line2 = sPos - sPos1;

            if (line1 == TSVector2.zero)
            {
                return(line2.magnitude);
            }
            return(TSMath.Abs(line2.x * line1.y - line2.y * line1.x) / line1.magnitude);
        }
Exemple #6
0
        //New 已测
        /// <summary>
        /// 检测圆与线段是否相交
        /// </summary>
        /// <param name="sOrgPos"></param>
        /// <param name="sOffset"></param>
        /// <param name="sCenter"></param>
        /// <param name="nRadius"></param>
        /// <param name="sCrossPoint"></param>
        /// <returns></returns>
        public static bool CheckCicleAndLine(TSVector2 sOrgPos, TSVector2 sOffset, TSVector2 sCenter, FP nRadius, out TSVector2 sCrossPoint)
        {
            //推导过程
            //http://blog.csdn.net/rabbit729/article/details/4285119

            sCrossPoint = sCenter;

            FP        nDis = sOffset.magnitude;
            TSVector2 d    = sOffset.normalized;
            TSVector2 e    = sCenter - sOrgPos;
            FP        a    = (e.x * d.x + e.y * d.y);
            FP        f    = a * a + nRadius * nRadius - e.LengthSquared();

            if (f >= 0)
            {
                FP s  = TSMath.Sqrt(f);
                FP t1 = a - s;
                FP t2 = a + s;
                if (TSMath.Abs(t1) > TSMath.Abs(t2))
                {
                    FP fTemp = t1;
                    t1 = t2;
                    t2 = fTemp;
                }
                //射线原点在圆外
                if ((t1 >= 0) && (t1 - nDis) <= 0)
                {
                    sCrossPoint.x = sOrgPos.x + t1 * d.x;
                    sCrossPoint.y = sOrgPos.y + t1 * d.y;
                    return(true);
                }

                //这里说明射线原点在圆内
                if (t2 >= 0)
                {
                    //如果与圆有碰撞
                    if ((t2 - nDis) <= 0)
                    {
                        sCrossPoint.x = sOrgPos.x + t2 * d.x;
                        sCrossPoint.y = sOrgPos.y + t2 * d.y;
                        return(true);
                    }
                    //如果两个点都在圆内
                    else
                    {
                        sCrossPoint = sOrgPos;
                        return(true);
                    }
                }
            }
            return(false);
        }
Exemple #7
0
        //NEW已测
        /// <summary>
        /// 检测一个点的xz平面投影是否处于2DAabb内
        /// </summary>
        /// <param name="sCenter"></param>
        /// <param name="nHalfWidth"></param>
        /// <param name="nHalfHeight"></param>
        /// <param name="sPos"></param>
        /// <returns></returns>
        public static bool CheckAabbAndPos(TSVector2 sCenter, FP nHalfWidth, FP nHalfHeight, TSVector2 sPos)
        {
            TSVector2 sOffset = sPos - sCenter;

            if (TSMath.Abs(sOffset.x) > nHalfWidth)
            {
                return(false);
            }
            if (TSMath.Abs(sOffset.y) > nHalfHeight)
            {
                return(false);
            }
            return(true);
        }
        public static TSQuaternion Slerp(TSQuaternion from, TSQuaternion to, FP t)
        {
            t = TSMath.Clamp(t, 0, 1);

            FP dot = Dot(from, to);

            if (dot < 0)
            {
                to  = Multiply(to, -1);
                dot = -dot;
            }

            FP halfTheta = FP.Acos(dot);

            return(Multiply(Multiply(from, FP.Sin((1 - t) * halfTheta)) + Multiply(to, FP.Sin(t * halfTheta)), 1 / FP.Sin(halfTheta)));
        }
Exemple #9
0
        //New 已测
        /// <summary>
        /// 检查矩形是否和线段相交
        /// </summary>
        /// <param name="sCenter"></param>
        /// <param name="sDir"></param>
        /// <param name="nHalfWidth"></param>
        /// <param name="nHalfHeight"></param>
        /// <param name="sOrgPos"></param>
        /// <param name="sOffset"></param>
        /// <returns></returns>
        public static bool CheckRectangleAndLine(TSVector2 sCenter, TSVector2 sDir, FP nHalfWidth, FP nHalfHeight, TSVector2 sOrgPos, ref TSVector2 sOffset)
        {
            FP nAngle = TSVector2.Angle(sDir, TSVector2.up);

            if (sDir.x < 0)
            {
                nAngle = 360 - nAngle;
            }
            TSVector2 sEndPos = sOrgPos + sOffset;
            FP        angle   = nAngle * FP.Deg2Rad;
            FP        nCos    = TSMath.Cos(angle);
            FP        nSin    = TSMath.Sin(angle);
            FP        deltaX  = sOrgPos.x - sCenter.x;
            FP        deltaY  = sOrgPos.y - sCenter.y;
            FP        nNewX   = deltaX * nCos - deltaY * nSin + sCenter.x;
            FP        nNewY   = deltaX * nSin + deltaY * nCos + sCenter.y;

            sOrgPos.x = nNewX;
            sOrgPos.y = nNewY;


            deltaX    = sEndPos.x - sCenter.x;
            deltaY    = sEndPos.y - sCenter.y;
            nNewX     = deltaX * nCos - deltaY * nSin + sCenter.x;
            nNewY     = deltaX * nSin + deltaY * nCos + sCenter.y;
            sEndPos.x = nNewX;
            sEndPos.y = nNewY;

            TSVector2 sNewDirection = sEndPos - sOrgPos;
            FP        nDis          = sOffset.magnitude;

            nDis = CheckAabbAndLine(sCenter, nHalfWidth, nHalfHeight, sOrgPos, sNewDirection, nDis);
            if (nDis < 0)
            {
                return(false);
            }
            else
            {
                sOffset = sOffset.normalized * nDis;
                return(true);
            }
        }
Exemple #10
0
 public static void LerpUnclamped(ref TSVector2 value1, ref TSVector2 value2, FP amount, out TSVector2 result)
 {
     result = new TSVector2(
         TSMath.Lerp(value1.x, value2.x, amount),
         TSMath.Lerp(value1.y, value2.y, amount));
 }
        public static TSQuaternion Lerp(TSQuaternion a, TSQuaternion b, FP t)
        {
            t = TSMath.Clamp(t, FP.Zero, FP.One);

            return(LerpUnclamped(a, b, t));
        }
Exemple #12
0
 public static void SmoothStep(ref TSVector2 value1, ref TSVector2 value2, FP amount, out TSVector2 result)
 {
     result = new TSVector2(
         TSMath.SmoothStep(value1.x, value2.x, amount),
         TSMath.SmoothStep(value1.y, value2.y, amount));
 }
Exemple #13
0
 public static TSVector2 SmoothStep(TSVector2 value1, TSVector2 value2, FP amount)
 {
     return(new TSVector2(
                TSMath.SmoothStep(value1.x, value2.x, amount),
                TSMath.SmoothStep(value1.y, value2.y, amount)));
 }
Exemple #14
0
 public static void Min(ref TSVector2 value1, ref TSVector2 value2, out TSVector2 result)
 {
     result.x = TSMath.Min(value1.x, value2.x);
     result.y = TSMath.Min(value1.y, value2.y);
 }
Exemple #15
0
 public static TSVector2 CatmullRom(TSVector2 value1, TSVector2 value2, TSVector2 value3, TSVector2 value4, FP amount)
 {
     return(new TSVector2(
                TSMath.CatmullRom(value1.x, value2.x, value3.x, value4.x, amount),
                TSMath.CatmullRom(value1.y, value2.y, value3.y, value4.y, amount)));
 }
Exemple #16
0
 public static TSVector2 LerpUnclamped(TSVector2 value1, TSVector2 value2, FP amount)
 {
     return(new TSVector2(
                TSMath.Lerp(value1.x, value2.x, amount),
                TSMath.Lerp(value1.y, value2.y, amount)));
 }
Exemple #17
0
 public static TSVector2 Clamp(TSVector2 value1, TSVector2 min, TSVector2 max)
 {
     return(new TSVector2(
                TSMath.Clamp(value1.x, min.x, max.x),
                TSMath.Clamp(value1.y, min.y, max.y)));
 }
Exemple #18
0
 public static void Hermite(ref TSVector2 value1, ref TSVector2 tangent1, ref TSVector2 value2, ref TSVector2 tangent2,
                            FP amount, out TSVector2 result)
 {
     result.x = TSMath.Hermite(value1.x, tangent1.x, value2.x, tangent2.x, amount);
     result.y = TSMath.Hermite(value1.y, tangent1.y, value2.y, tangent2.y, amount);
 }
Exemple #19
0
 public static void Clamp(ref TSVector2 value1, ref TSVector2 min, ref TSVector2 max, out TSVector2 result)
 {
     result = new TSVector2(
         TSMath.Clamp(value1.x, min.x, max.x),
         TSMath.Clamp(value1.y, min.y, max.y));
 }
Exemple #20
0
 public static TSVector2 Min(TSVector2 value1, TSVector2 value2)
 {
     return(new TSVector2(
                TSMath.Min(value1.x, value2.x),
                TSMath.Min(value1.y, value2.y)));
 }
Exemple #21
0
 public static TSVector2 Barycentric(TSVector2 value1, TSVector2 value2, TSVector2 value3, FP amount1, FP amount2)
 {
     return(new TSVector2(
                TSMath.Barycentric(value1.x, value2.x, value3.x, amount1, amount2),
                TSMath.Barycentric(value1.y, value2.y, value3.y, amount1, amount2)));
 }