判断点是否在矩形内碰撞代码

bool CheckNpcSquareCollided(XNpc npc)
{
        int nCollisionRange = npc.NpcTemplate == null ? 0 : npc.NpcTemplate.CollisionRange;


XPoint centerPoint = LogicPoint + CenterOffset;
if (npc.LogicPoint == centerPoint)
return true;


        if (nCollisionRange == 0)
        {
            Vector2 dir = DestDir.Normalized();
            float fCos = dir.x;
            float fSin = dir.y;
            XPoint localPos = npc.LogicPoint - centerPoint;
            XPoint result = new XPoint();


            result.X = (int)(localPos.X * fCos + localPos.Y * fSin);
            result.Y = (int)(localPos.Y * fCos - localPos.X * fSin);


            if (result.X < -(Info.Width / 2) || result.X > (Info.Width / 2))
                return false;


            if (result.Y < -(Info.Height / 2) || result.Y > (Info.Height / 2))
                return false;
        }
        else
        {
            int nDirX = (int)((double)(centerPoint.X - npc.LogicPoint.X) * XGameDefine.NPC_DIR_LENGTH / nCollisionRange);
            int nDirY = (int)((double)(centerPoint.Y - npc.LogicPoint.Y) * XGameDefine.NPC_DIR_LENGTH / nCollisionRange);
            double fDirDistance = Math.Sqrt((double)nDirX * nDirX + (double)nDirY * nDirY);
            double p = (double)nCollisionRange / fDirDistance;
            int relativeX = (int)(nDirX * p);
            int relativeY = (int)(nDirY * p);
            XPoint PFocus = new XPoint(npc.LogicPoint.X + relativeX, npc.LogicPoint.Y + relativeY);         //得出矩形与圆形的焦点坐标


            float ATilt = Mathf.Atan((float)DestDir.Y / DestDir.X);                                         //方向与坐标系的倾斜角
            XPoint newCenterPoint = new XPoint();
            newCenterPoint.X = (int)(centerPoint.X * Mathf.Cos(ATilt) + centerPoint.Y * Mathf.Sin(ATilt));  //坐标系旋转后的中心点x坐标
            newCenterPoint.Y = (int)(centerPoint.Y * Mathf.Cos(ATilt) - centerPoint.X * Mathf.Sin(ATilt));  //坐标系旋转后的中心点y坐标
            XPoint newFocus = new XPoint();
            newFocus.X = (int)(PFocus.X * Mathf.Cos(ATilt) + PFocus.Y * Mathf.Sin(ATilt));
            newFocus.Y = (int)(PFocus.Y * Mathf.Cos(ATilt) - PFocus.X * Mathf.Sin(ATilt)); //尼玛,记得这个焦点也要旋转坐标轴啊。妹妹的
            
            XPoint p1 = new XPoint(newCenterPoint.X - Info.Width / 2, newCenterPoint.Y + Info.Height / 2);
            XPoint p2 = new XPoint(newCenterPoint.X + Info.Width / 2, newCenterPoint.Y + Info.Height / 2);
            XPoint p3 = new XPoint(newCenterPoint.X + Info.Width / 2, newCenterPoint.Y - Info.Height / 2);
            XPoint p4 = new XPoint(newCenterPoint.X - Info.Width / 2, newCenterPoint.Y - Info.Height / 2);
//             if (npc.Name == "" && XGame.CurrentFrame % 4 == 0)
//             {
//                 XBase.Log("p1234->({0},{1}),({2},{3}),({4},{5}),({6},{7}))", p1.X, p1.Y, p2.X, p2.Y, p3.X, p3.Y, p4.X, p4.Y);
//                 XBase.Log("center:({0},{1})", centerPoint.X, centerPoint.Y);
//                 XBase.Log("focus:({0},{1})", newFocus.X, newFocus.Y);
//                 XBase.Log("npc:({0},{1})", npc.LogicPoint.X, npc.LogicPoint.Y);
//                 XBase.Log("--------------------------------------------------------");
//             }
            if (newFocus.X >= p4.X && newFocus.Y >= p4.Y && newFocus.X <= p2.X && newFocus.Y <= p2.Y)
            {
                XBase.Log("ok...");
                return true;
            }
            else
                return false;
        }


return true;
}

你可能感兴趣的:(判断点是否在矩形内碰撞代码)