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

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值