做台球游戏用到的,球的虚线和边框的触碰点的获取
--判断直线和边框的碰撞点
--@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
思路就是分象限讨论,这里不同的需求可以不同的实现