PicoPicoGameEngine

Quick Links

Source Code

g=ppgraph
pi=math.pi

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.friction=0.3
shape:set(-40,15,40,15)
body=world:createBody()
body:createFixture(shape)

polygons={}
shape=b2Shape.polygon()
shape.friction=0.3
shape:set(-0.5,0,0.5,0,0,1.5)
table.insert(polygons,shape)

shape=b2Shape.polygon()
shape.friction=0.3
shape:set(-0.1,0,0.1,0,0,1.5)
table.insert(polygons,shape)

w = 2
b = w / (2+math.sqrt(2))
s = math.sqrt(2) * b

shape=b2Shape.polygon()
shape.friction=0.3
shape:set(0.5*s,0,
          0.5*w,b,
          0.5*w,b+s,
          0.5*s,w,
          -0.5*s,w,
          -0.5*w,b+s,
          -0.5*w,b,
          -0.5*s,0)
table.insert(polygons,shape)

shape=b2Shape.polygon()
shape.friction=0.3
shape:setAsBox(0.5,0.5)
table.insert(polygons,shape)

shape=b2Shape.circle(
      {radius=0.5,friction=0.3})
shape.friction=0.3
table.insert(polygons,shape)

shape=b2Shape.edge()
shape.friction=0.3
shape:set(-1.0,0,1.0,0)
table.insert(polygons,shape)

angle = 0

function create(idx)
  local x=RandomFloat(16-10,16+10)
  local y=RandomFloat(15+0,15+20)
  local b=world:createBody({x,y},{
    angle=RandomFloat(-pi,pi),
    angularDamping=0.02})
  b:createFixture(polygons[idx])
  return b
end

for i=1,30 do
  create(math.random(1,5))
end

abutton=false
L = 11.0;
m_angle=pi

function start()
  world:step()

  point1=pppoint(16.0,25.0)
  d=pppoint(
        L * math.cos(m_angle),
        L * math.sin(m_angle))
  point2=point1+d
  m_angle=m_angle+0.25*pi/180.0

  world:debugDrawBody()
  world:debugDrawJoint()
  
  m_point={}
  world:rayCast(point1,point2,
    function(fixture,point,normal,fraction)
      table.insert(m_point,point)
      world:debugDrawShape(
        f.shape,f.body:transform())
      -- return -1       --Any
      return 1           --Multi
      -- return fraction --Closest
    end)

  g:line(point1*10,point2*10)
  for i,v in ipairs(m_point) do
    g:circle(v.x*10,v.y*10,4)
  end

end