示例#1
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));
        }
示例#2
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);
            }
        }