cocos2d 判断旋转矩形是否包含某个点

本来想画个图演示一下,但是折腾了一会发现画不好,我的win10系统没有安装office,以后再看的话再补上吧。不废话了。

  如图所以,如果判断点P是否被矩形A所包含,非常容易。那么如果矩形A以中心点逆时针旋转30度呢?有兴趣的同学可以去研究一下OBB算法,应该会有一些思路。我个人的思路是矩形A以中心点逆时针旋转30度,相当于点P以矩形A的中心点顺时针旋转30度,那么就相当于判断点D是否被矩形A包含,这就是我们熟悉的判断方法了。那么如何求出旋转后的D点的坐标呢, 这里我是用仿射变化的方法,如果还记得初高中数学知识的话,也可以自己算一下。

  用仿射矩阵的话,需要用到三个矩阵,分别是点P移动到A的中心点的平移矩阵matrix1,旋转30的旋转矩阵matrix2,从A的中心点移动到P的平移矩阵matrix33。D=matrix1*matrix2*matrix3*P。下面是我的一些代码。

-- 获取绕point点旋转的旋转矩阵
function QGetRotateMatrix(point, anAngle)
    local unitMatrix = CCAffineTransformMakeIdentity() -- 单位矩阵
    local matrix1 = CCAffineTransformTranslate(unitMatrix, -point.x, -point.y)
    local matrix2 = CCAffineTransformRotate(unitMatrix, anAngle)
    local matrix3 = CCAffineTransformTranslate(unitMatrix, point.x, point.y)
    local matrix = CCAffineTransformConcat(matrix1, matrix2)
    matrix = CCAffineTransformConcat(matrix, matrix3)
    return matrix
end

-- 获取旋转anAngle角度后的矩形
--[[--
@param anAngle CCRect 未旋转矩形
@param anAngle value 旋转角度
--]]--
function QGetRotateRect(rect, anAngle)
    local cx = rect.origin.x + rect.size.width * 0.5
    local cy = rect.origin.y + rect.size.height * 0.5
    local centerPoint = qccp(cx, cy)

    local matrix = QGetRotateMatrix(centerPoint, anAngle)
    local lb = ccp(rect.origin.x, rect.origin.y)
    local lt = ccp(rect.origin.x, rect.size.height+rect.origin.y)
    local rt = ccp(rect.size.width+rect.origin.x, rect.size.height+rect.origin.y)
    local rb = ccp(rect.size.width+rect.origin.x, rect.origin.y)
    lb = CCPointApplyAffineTransform(lb, matrix)
    lt = CCPointApplyAffineTransform(lt, matrix)
    rt = CCPointApplyAffineTransform(rt, matrix)
    rb = CCPointApplyAffineTransform(rb, matrix)

    return lb, lt, rt, rb
end

-- 判断旋转后的矩形是否包围point,理论上同样适应未旋转的矩形
--[[--
@param rect CCRect 未旋转的矩形
@param point qccp 需要判断的点
@param anAngle value 旋转角度
--]]--
function QRotateRectContainPoint(rect, anAngle, point)
    local cx = rect.origin.x + rect.size.width * 0.5
    local cy = rect.origin.y + rect.size.height * 0.5
    local centerPoint = ccp(cx, cy)

    local matrix = QGetRotateMatrix(centerPoint, anAngle)
    local rotatePoint = CCPointApplyAffineTransform(point, matrix)
    
    local bRet = false
    if rotatePoint.x >= rect.origin.x and
        rotatePoint.x <= rect.origin.x + rect.size.width and
        rotatePoint.y >= rect.origin.y and
        rotatePoint.y <= rect.origin.y + rect.size.height then
        bRet = true
    end

    return bRet;
end

里面用到了一些cocos2d的矩阵函数,不懂的可以去看下源码。

转载于:https://www.cnblogs.com/eedlrj/p/8722446.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值