向量运用。扇形(或三角形)碰撞检测判断。 lua代码

-- 在学习这个之前,建议复习下向量知识



local isCollison = false --碰撞状态
local dx,dy = 0, 0  --位置差
local fb_alpha = 0 --拉莫大
local fb_beta = 0 --贝塔
local fDelta = 1 --的塔值  -  -
local c_r = 0 --半径和
local fDsitSqr = 0
local a,b,c = 0,0,0
local d = 0
local t = 1
--------------------------check hit func -----------------------------
-- 扇形圆心在 圆内
local function isCollisonFposWithCirlPos()
dx = cilre.x - fanshaped.x
dy = cilre.y - fanshaped.y
fDsitSqr = dx * dx + dy * dy
------is true >> collision
isCollison =  fDsitSqr <= cilre.r * cilre.r and true or false
print("fDsitSqr value si ",fDsitSqr)
print("cilre.r * cilre.r ",cilre.r * cilre.r)
if isCollison then
return isCollison
end
end 
dx = cilre.x - fanshaped.x
dy = cilre.y - fanshaped.y
fDsitSqr = dx * dx + dy * dy -- vec_1 = cc.pSub(cc.p(c.x,c.y),cc.p(f.x,f.y))
-- 圆心在扇形的两个向量之间。(矩阵 和 定比分点公式结合) 。 
--bit 开头均为位运算。
local function isCollisonFVecWithCiclePos()
-- 叉乘的结果是一个向量,结果为Z轴的模 。绝对值为其  ab向量形成的四边形的面积,即为ab包围三角形面积2倍
-- 所以可以有第二种方法
--[[方法2 与 扇形的 三个向量 做叉乘。如果同号则在扇形(也适用于三角形)里面
-- vec_1 = cc.pSub(cc.p(c.x,c.y),cc.p(f.x,f.y))
-- local vec_1 = cc.pSub(cc.p(cilre.x,cilre.y),cc.p(fanshaped.vec_x,fanshaped.vec_y))
-- local vec_2 = cc.pSub(cc.p(cilre.x,cilre.y),cc.p(fanshaped.vec_x2,fanshaped.vec_y_2))
-- local vec_3 = cc.pSub(cc.p(cilre.x,cilre.y),cc.p(fanshaped.x,fanshaped.y))
-- local vx1 = cc.pCross(vec_1,vec_2)
-- local vx2 = cc.pCross(vec_2,vec_3)
-- local vx3 = cc.pCross(vec_3,vec_1)
-- 采取 位运算更快判断是否同号
-- if (bit._or(vx1,vx2) < 0 and bit._or(vx2,vx3) < 0 and bit._or(vx1,vx3) < 0) or (bit._or(vx1,vx2) < 0 and bit._or(vx2,vx3) < 0 and bit._or(vx1,vx3) > 0) then
在内部
end
-- print(" vx vx  vx ",vx1,vx2,vx3)]]
fDelta = fanshaped.vec_x * fanshaped.vec_y_2 -  fanshaped.vec_x2 * fanshaped.vec_y
fb_alpha = (dx * fanshaped.vec_y_2 - dy * fanshaped.vec_x2) / fDelta
fb_beta = (-dx * fanshaped.vec_y + dy * fanshaped.vec_x) / fDelta
if fb_alpha >= 0 and fb_beta >= 0 then
ar = fanshaped.r + cilre.r 
if (fDsitSqr) <= ar * ar then
isCollison = true
return isCollison
end
end
end 
---------暂时就考虑这2种情况吧
isCollison = isCollisonFposWithCirlPos()
if  isCollison then
return isCollison
elseif not isCollison then
isCollison = isCollisonFVecWithCiclePos()
return isCollison
else

end

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值