用lua写一个cocos2d-x 3.0的碰撞检测

-----求point绕o点逆时针旋转angle度之后,新点的位置
function getRotatedPos(o,point,angle)
    return
    {
        x=(point.x-o.x)*math.cos(math.rad(angle))-(point.y-o.y)*math.sin(math.rad(angle))+o.x,
        y=(point.x-o.x)*math.sin(math.rad(angle))+(point.y-o.y)*math.cos(math.rad(angle))+o.y,
    }
end

----node里是否包含点point
function containPoint(node,point)
    local size=node:getContentSize()
    size={width=size.width*node:getScaleX(),height=size.height*node:getScaleY()}
    local pos={x=node:getPositionX(),y=node:getPositionY() }
    local anchor=node:getAnchorPoint()
    local rotate=node:getRotation()%360
    local pointRotated=getRotatedPos(pos,point,rotate)
    local rectRotated={x=pos.x-size.width*anchor.x,y=pos.y-size.height*anchor.y,width=size.width,height=size.height}
    if pointRotated.x>=rectRotated.x and pointRotated.x<=rectRotated.x+rectRotated.width and
            pointRotated.y>=rectRotated.y and pointRotated.y<=rectRotated.y+rectRotated.height then
        return true
    end
end
function checkNodeHit(node1,node2)
    local size1=node1:getContentSize()
    size1={width=size1.width*node1:getScaleX(),height=size1.height*node1:getScaleY()}
    local size2=node2:getContentSize()
    size2={width=size2.width*node2:getScaleX(),height=size2.height*node2:getScaleY()}
    local pos1={x=node1:getPositionX(),y=node1:getPositionY()}
    local pos2={x=node2:getPositionX(),y=node2:getPositionY()}
    local anchor1=node1:getAnchorPoint()
    local anchor2=node2:getAnchorPoint()
    local rotation1=node1:getRotation()%360
    local rotation2=node2:getRotation()%360
    local rect1={x=pos1.x-size1.width*anchor1.x,y=pos1.y-size1.height*anchor1.y,width=size1.width,height=size1.height}
    local rect2={x=pos2.x-size2.width*anchor2.x,y=pos2.y-size2.height*anchor2.y,width=size2.width,height=size2.height}
    local rect1Points={
        getRotatedPos(pos1,{x=rect1.x,y=rect1.y},360-rotation1),
        getRotatedPos(pos1,{x=rect1.x+rect1.width,y=rect1.y},360-rotation1),
        getRotatedPos(pos1,{x=rect1.x,y=rect1.y+rect1.height},360-rotation1),
        getRotatedPos(pos1,{x=rect1.x+rect1.width,y=rect1.y+rect1.height},360-rotation1),
    }
    local rect2Points={
        getRotatedPos(pos2,{x=rect2.x,y=rect2.y},360-rotation2),
        getRotatedPos(pos2,{x=rect2.x+rect2.width,y=rect2.y},360-rotation2),
        getRotatedPos(pos2,{x=rect2.x,y=rect2.y+rect2.height},360-rotation2),
        getRotatedPos(pos2,{x=rect2.x+rect2.width,y=rect2.y+rect2.height},360-rotation2),
    }
    for _,p in pairs(rect1Points) do
        if containPoint(node2,p) then
            return true
        end
    end
    for _,p in pairs(rect2Points) do
        if containPoint(node1,p) then
            return true
        end
    end
end
--[[基本思路:
两个有角度的矩形是否碰撞,只需判断四个顶点是否另一个矩形内部。所以先判断一个点是否在矩形内部。这个可以以rect1的锚点为圆心旋转,摆正,得到新的矩形,同样将要判断的点绕rect1的锚点旋转相同的角度,然后判断这个点是否在rect1内。
优化:比如多次计算了sin和cos的值,这个可以存起来
局限:
不能判断快速运动中的矩形碰撞,如果矩形运动过快,两帧之间形成的空档大于另一个矩形的大小,那就无法得到准确的值,所以优化的话应该计算运动路径形成的轨迹上的碰撞检测。
不能判断切变后的矩形(有skew值的)碰撞
由于我的项目是慢速运动,两个矩形大小相近,也没有切变,所以没做这部分。
]]--

转载于:https://my.oschina.net/TAjaoSMd/blog/676602

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值