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的矩阵函数,不懂的可以去看下源码。

posted on 2018-04-05 14:39  二二的路人甲  阅读(1165)  评论(0编辑  收藏  举报