cocos2d-x 判断直线和直线的交点

做台球游戏用到的,球的虚线和边框的触碰点的获取

--判断直线和边框的碰撞点
--@rotate 旋转角度,数学角度
--@whitePos 白球的node相对位置
--@radius 球体半径
function help.checkCollisionPointBetweenLines(rotate, whitePos,radius)
    local _tableX, _tableY = 207.5, 99.5  -- 桌子在Node的x,y
    local _tableWidth, _tableHeight = 930 - 208, 475 - 101  -- 桌子border的长宽
    if rotate <= 90 then
        rotate = rotate / 180 * math.pi
        local _traH = math.tan(rotate) *(_tableWidth + _tableX - whitePos.x)
        if _traH <= _tableHeight + _tableY - whitePos.y then
            return _traH/math.sin(rotate)-radius/math.cos(rotate)
        else
            return (_tableY + _tableHeight - whitePos.y)/math.sin(rotate)-radius/math.sin(rotate)
        end
    elseif rotate <= 180 then
        rotate = (180- rotate) / 180 * math.pi
        local _traH = (whitePos.x-_tableX)*math.tan(rotate)
        if _traH <= _tableHeight + _tableY - whitePos.y then
            return _traH/math.sin(rotate)-radius/math.cos(rotate)
        else
            return (_tableY + _tableHeight - whitePos.y)/math.sin(rotate)-radius/math.sin(rotate)
        end
    elseif rotate <= 270 then
        rotate = (rotate - 180) / 180 * math.pi
        local _traH = (whitePos.x-_tableX)*math.tan(rotate)
        if _traH <= whitePos.y-_tableY then
            return _traH/math.sin(rotate)-radius/math.cos(rotate)
        else
            return (whitePos.y-_tableY)/math.sin(rotate)-radius/math.sin(rotate)
        end
    elseif rotate <= 360 then
        rotate = (360 - rotate) / 180 * math.pi
        local _traH = (_tableX+_tableWidth-whitePos.x)*math.tan(rotate)
        if _traH <= whitePos.y-_tableY then
            return (_tableX+_tableWidth-whitePos.x)/math.cos(rotate)-radius/math.cos(rotate)
        else
            return (whitePos.y-_tableY)/math.sin(rotate)-radius/math.sin(rotate)
        end
    end
    return 0
end
思路就是分象限讨论,这里不同的需求可以不同的实现
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值