
如图中的大螃蟹,他丫的一直在地面上跳,我想把相对于大螃蟹y轴为0以下的刚体的点给切掉,要怎么做?
如下图:原本的刚体(一个多边形是由5个点组成的,用暗绿色的1到5表示),假设绿色的是地面(或者说是切割线),要把画了虚线的部分给切掉。获取6个顶点(用红色的1到6表示)。要怎么做。


如图中的大螃蟹,他丫的一直在地面上跳,我想把相对于大螃蟹y轴为0以下的刚体的点给切掉,要怎么做?
如下图:原本的刚体(一个多边形是由5个点组成的,用暗绿色的1到5表示),假设绿色的是地面(或者说是切割线),要把画了虚线的部分给切掉。获取6个顶点(用红色的1到6表示)。要怎么做。

好像想到了。果然把问题画出来有助于思考
1.如果线段的两个端点的y轴坐标都大于地面的y轴坐标,记录这两个点
2.如果线段有以i个端点大于而另一个端点小于地面的y轴坐标,求出两个线段的交点,并记录(大于地面y轴的点和交点)
3.如果线段的两个端点的y轴坐标都小于地面的y轴坐标,直接无视
问题解决了 ,附上lua代码 。这段代码让游戏的帧率从60帧降到了50帧,看来得移到C里面去
function CBattleLayer:getCutVertexList(vertexList)
local vertexList2 = {}
local pqie1 = cc.p(1000,0)
local pqie2 = cc.p(-1000,0)
for vec_i = 1,#vertexList do
--if todo2.y<0 then todo2.y=0 end
--print(todo1,todo2.x,todo2.y)
local idx1=vec_i
local idx2=vec_i+1
if vec_i==#vertexList then
idx1 = #vertexList
idx2 = 1
end
local p1 = vertexList
local p2 = vertexList
if p1.y>=0 and p2.y>=0 then
if p1.y>=0 then -- 检测p1是否已经在队列中
if #vertexList2>0 then
local ptemp = vertexList2#vertexList2]
if ptemp.x == p1.x and ptemp.y == p1.y then
--已经在队列中了
else
table.insert(vertexList2,p1)
end
else
table.insert(vertexList2,p1)
end
end
if p2.y>=0 then
if vec_i==#vertexList then --p2是队列头了已经
if #vertexList2>0 then
local ptemp = vertexList2
if ptemp.x == p2.x and ptemp.y == p2.y then
--已经在队列中了
else
table.insert(vertexList2,p1)
end
end
else
table.insert(vertexList2,p2)
end
end
elseif p1.y>=0 or p2.y>=0 then
--求交点
local pjiao = cc.pGetIntersectPoint(p1,p2,pqie1,pqie2)
if p1.y>=0 then -- 检测p1是否已经在队列中
if #vertexList2>0 then
local ptemp = vertexList2#vertexList2]
if ptemp.x == p1.x and ptemp.y == p1.y then
--已经在队列中了
else
table.insert(vertexList2,p1)
end
else
table.insert(vertexList2,p1)
end
end
table.insert(vertexList2,pjiao)
if p2.y>=0 then
if vec_i==#vertexList then --p2是队列头了已经
if #vertexList2>0 then
local ptemp = vertexList2
if ptemp.x == p2.x and ptemp.y == p2.y then
--已经在队列中了
else
table.insert(vertexList2,p1)
end
end
else
table.insert(vertexList2,p2)
end
end
end
end
--
for todo1,todo2 in ipairs(vertexList) do
--if todo2.y<0 then todo2.y=0 end
print("v1",todo1,todo2.x,todo2.y)
end
for todo1,todo2 in ipairs(vertexList2) do
--if todo2.y<0 then todo2.y=0 end
print("v2",todo1,todo2.x,todo2.y)
end
--]]
if #vertexList2<3 then
return nil
else
return vertexList2
end
end
```