I was attempting to make a field of little pegs using a function to create them. As I created each peg they were added to a peg displaygroup so that I could later move the entire field of pegs if I wanted. However I can’t seem to get my ball and my peg field to interact or collide using the box2d engine. Here is my code for creating the ball.
function PlayObjects:newBall( ) local ballGroup = display.newGroup() ballGroup.type = "gameBall" local ball = display.newCircle( ballGroup, 0, 0, ballR ) ball:setFillColor(0,0,0) physics.addBody( ballGroup, { density=1.0 , friction=0.5, bounce=0.6, radius = ballR } ) return ballGroup end
Then I created my peg field with the code below.
function Obstacles:newPegField(w, h, rotation) local pegField = display.newGroup() local pegSize = \_H\*.005 local pegSpace = ballR\*2 for i = 1, math.floor(h/pegSpace) do for j = 1, math.floor(w/pegSpace) do local peg = display.newCircle( pegField, 0, 0, pegSize ) peg:setFillColor(0,0,0) physics.addBody( peg, "static", { density=1.0 , friction=0.5, bounce=0.3, radius = pegSize } ) if (i%2 == 1) then peg.x = j\*pegSpace else peg.x = j\*pegSpace + .5\*pegSpace end peg.y = i\*pegSpace end end return pegField end
The above constructs a field of pegs kinda like a carnival game or plinko. However I can’t seem to get the ball or the pegs to hit or interact with each other, the ball just passes through the pegs.