随笔 - 6  文章 - 0  评论 - 3  阅读 - 9184

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   二二的路人甲  阅读(1207)  评论(0编辑  收藏  举报
编辑推荐:
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
阅读排行:
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示