--创建碰撞框-- 用于多个部位检测
function MathTools.createCollisionRect(sp, sp_custom)
local sp_t = {
x = not sp_custom and sp:getPositionX() or sp_custom.x or sp:getPositionX(),
y = not sp_custom and sp:getPositionY() or sp_custom.y or sp:getPositionY() ,
w = not sp_custom and sp:getContentSize().width or sp_custom.w or sp:getContentSize().width ,
h = not sp_custom and sp:getContentSize().height or sp_custom.h or sp:getContentSize().height,
ap = cc.p(sp:getAnchorPoint())
}
--忽略锚点 等价于 getBoundBox()
sp_t.x = sp_t.x - sp_t.w * sp_t.ap.x
sp_t.y = sp_t.y - sp_t.h * sp_t.ap.y
return sp_t
end
--碰撞检测 矩形 1 个部位 矩形框的大小
function MathTools.isCollisionToRect(sp_t,sp2,is_open_line,layer)
local is_rect = false
local collisionCfg = {}
local collision_id = {}
for __,v in pairs(sp2) do
local sp2 = v
for _,v2 in pairs(sp_t) do
local sp = v2
if is_open_line then
local function DrawRect()
local rect_1 = cc.DrawNode:create()
rect_1:setName("drect"..rect_1:getTag())
rect_1:drawRect(cc.p(sp.x , sp.y ),cc.p(sp.w + sp.x ,sp.h + sp.y),cc.c4b(255,100,0,100)) --后期记得加 颜色枚举
layer:addChild(rect_1,999)
local rect_2 = cc.DrawNode:create()
rect_2:setName("drect"..rect_2:getTag())
rect_2:drawRect(cc.p(sp2.x , sp2.y ),cc.p(sp2.w + sp2.x ,sp2.h + sp2.y),cc.c4b(100,255,0,100)) --后期记得加 颜色枚举
layer:addChild(rect_2,999)
end
DrawRect()--画框
end
local is_rect = not ( sp.x > sp2.x + sp2.w or
sp.x + sp.w < sp2.x or
sp.y > sp2.y + sp2.h or
sp.y + sp.h < sp2.y )
if is_rect == true then
--插入碰撞信息
table.insert(collision_id ,1,{Language.Common.Collision[_].."->"..Language.Common.Collision[__]})
table.insert(collisionCfg, 1 , { [1] = true ,[2] = collision_id })
end
end
end
return collisionCfg
end