线段数据类内部实现
local SLine=class("SLine")
local lines=G_Lines
local table=table
function SLine:ctor(startx,starty,endx,endy,life)
self.startx=startx
self.starty=starty
self.endx=endx
self.endy=endy
self.life=life or 10
table.insert(lines,self)
end
function SLine:update()
self.life=self.life-1
if(self.life<=0)then
table.removebyvalue(lines,self)
end
end
return SLine
碰撞检测
function MainScene:_checkCollide()
for k,v in pairs(lines) do
for kk,vv in pairs(self.emitter.flyPool) do
if(vv.canSplit==true)then
local xpos=vv:getPositionX()
local ypos=vv:getPositionY()
local hitResult=self:_lineHitCircle(v.startx,v.starty,v.endx,v.endy,vv.r,xpos,ypos)
local isHit=(vv.isDie==false and hitResult~=nil)
if(isHit)then
self.emitter:doHit(vv,hitResult.angle);
self:_handleResult(vv,xpos,ypos)
end
end
end
end
end
--------------线段与圆碰撞检测
-----------------这个是核心 ,具体要问我原理的话,请wiki百科 (圆与线段碰撞检测)
function MainScene:_lineHitCircle(x1,y1,x2,y2,r,xc,yc)
local dx_2_1 = x2 - x1
local dy_2_1 = y2 - y1
local xd=0
local yd=0
local d=0
local t = ((yc - y1) * dy_2_1 + (xc - x1) * dx_2_1) / (dy_2_1 * dy_2_1 + dx_2_1 * dx_2_1)
if(t>=0 and t<=1)then
xd = x1 + t * dx_2_1
yd = y1 + t * dy_2_1
d = math.sqrt((xd - xc) * (xd - xc) + (yd - yc) * (yd - yc))
else
d = math.sqrt((xc - x1) * (xc - x1) + (yc - y1) * (yc - y1))
if(d >4)then
d = math.sqrt((xc - x2) * (xc - x2) + (yc - y2) * (yc - y2))
end
end
if(d>r)then return nil end
local lineDis=math.sqrt((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1))
if(lineDis>=10)then
local obj={}
obj.angle=math.atan2(y2-y1,x2-x1)
return obj
end
return nil
end
四、原创完整版源码下
源码下载:Quick v3.3fruitNinja
最终效果图(gif上传大小有限制,所以只能牺牲下帧数,下面的看起来太快了没办法,真实不是这样的):

感谢本文作者CocoaChina会员chenlongyuan的分享,Cocos引擎中文官网有奖征集优秀原创Cocos教程 奖品丰厚!活动地址:?tid-274890.html
Quick-Cocos2d-x 3.5 + cocos code IDE 1.2 使用教程,增加Android真机调试教程
Quick-Cocos2d-x 3.3绑定自定义C++类02:生成并使用tolua++工具
Quick-Cocos2d-x 3.3绑定自定义C++类01:配置Lua运行及开发环境
Cocos2d-x开发的小游戏源码分享
Cocos2d-Lua(Quick-Cocos2d-x)集成第三方SDK(三)
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/shumachanpin/article-50156-4.html
难道那个3年没变样的月饼很好吗
一起围捕拉森号去