PicoPicoGameEngine

Quick Links

ピコピココード

world=b2World.new(0,10)
groundBody=world:createBody()

function RandomFloat(a,b)
  local d=1000
  return math.random(a*d,b*d)/d
end

shape=b2Shape.edge()
shape:set(-50,0,50,0)
body=world:createBody()
body:createFixture(shape)

xLo = -5.0; xHi = 5.0;
yLo = 2.0; yHi = 25.0;

density={density=5}

shape=b2Shape.polygon(density)
shape:set(-1,0, 1,0, 0,-2)
body=world:createBody("dynamic",
  {RandomFloat(xLo,xHi),
   -RandomFloat(yLo,yHi)})
body:createFixture(shape)

shape=b2Shape.polygon(density)
shape:set(-2,0, 2,0, 0,-4)
body=world:createBody("dynamic",
  {RandomFloat(xLo,xHi),
   -RandomFloat(yLo,yHi)})
body:createFixture(shape)

shape=b2Shape.polygon(density)
shape:setAsBox(1,0.5)
body=world:createBody("dynamic",
  {RandomFloat(xLo,xHi),
   -RandomFloat(yLo,yHi)})
body:createFixture(shape)

shape=b2Shape.polygon(density)
shape:setAsBox(2,1.0)
body=world:createBody("dynamic",
  {RandomFloat(xLo,xHi),
   -RandomFloat(yLo,yHi)})
body:createFixture(shape)

shape=b2Shape.circle(density)
shape.radius=1
body=world:createBody("dynamic",
  {RandomFloat(xLo,xHi),
   -RandomFloat(yLo,yHi)})
body:createFixture(shape)

shape=b2Shape.circle(density)
shape.radius=2
body=world:createBody("dynamic",
  {RandomFloat(xLo,xHi),
   -RandomFloat(yLo,yHi)})
body:createFixture(shape)

local m_point={}

function PreSolve(contact,points)
  for i,v in ipairs(points) do
    local t=v
    v.fixtureA = contact.fixtureA
    v.fixtureB = contact.fixtureB
    table.insert(m_point,v)
  end
end

world:shiftOrigin(-15,-30)

function start()
  m_point={}
  nuku={}
  world:debugDrag(pptouch())
  world:step(1.0/60,8,3,
    {preSolve=PreSolve})
  for i,v in ipairs(m_point) do
    local body1=v.fixtureA.body
    local body2=v.fixtureB.body
    local mass1=body1.mass
    local mass2=body2.mass
    if mass1 > 0 and mass2 > 0 then
      if mass2 > mass1 then
        nuku[#nuku+1]=body1
      else
        nuku[#nuku+1]=body2
      end
    end
  end
  if #nuku>0 then
    local t={}
    for i,v in ipairs(nuku) do
      local f=true
      for j,w in ipairs(t) do
        if v==w then
          f=false
          break
        end
      end
      if f then
        t[#t+1]=v
      end
    end
    for i,v in ipairs(t) do
      world:destroyBody(v)
    end
  end
  world:debugDrawBody()
  world:debugDrawJoint()
end