# Help creating a game

Hey, I need help with this code im writing, and some of it isnt mine, but I need help creating a new physics.body when two bodies collide, but I cant seem to figure out how to do this, it crashes everytime. Ideas are much appreiated.

Heres the code, most of it is borrowed.

``````-- moving balls

function setup()
displayMode(FULLSCREEN)

noSmooth()

balls = {}
touches = {}

base = physics.body(EDGE, vec2(10,-4), vec2(WIDTH-1,10))

wall = physics.body(EDGE, vec2(1000, 900), vec2(WIDTH-10, 10))

nextball = 0
end

function touched(touch)
if touch.state == ENDED then
touches[touch.id] = nil
else
--if touches[touch.id] == nil then
touches[touch.id] = touch
--end
end
end

function touchActions()
for k,v in pairs(touches) do
if CurrentTouch.state == ENDED then
--if there are no current touches then we kill all current touches to avoid bugged ball producers
touches[k] = nil
else

--add a new ball at the touch location
size = math.random(2,20)
tspot = physics.body(CIRCLE, size)
tspot.position = vec2(v.x+math.random(-1,1), v.y+math.random(-1,1))
tspot.restitution = 1

balls[nextball] = { tspot = tspot, size = size * 2, r = math.random(30,255), g = math.random(30,255), b = math.random(30,255) }

nextball = nextball + 1
end
end
end

function draw()
background(0, 0, 0, 255)
--myFPSReporter:draw(3)
strokeWidth(0)

for k,v in pairs(balls) do
if v.tspot.x < -20 or v.tspot.x > WIDTH + 20 or v.tspot.y < -20 then
balls[k].tspot:destroy()
balls[k] = nil
else
fill(v.r, v.g, v.b, 255)
ellipse(v.tspot.x, v.tspot.y, v.size)
end
end

touchActions()
end

function collide(contact)
if contact.state== BEGAN then
physics.body(CIRCLE,size)
end
end
``````

Some of the code is in and some is out of the three tildes. (3 * ~) Can you try and reformat the code so it’s easier to read and understand?

Thanks.

I think what is happening is that all your bodies are being created on top of each other, soothe new ones collide immediately with the old ones, which creates new bodies which collide with the old ones, which creates new bodies…

So I think you need to find a way to create them separately from each other.

The other problem you will have is that you are not creating a variable when you add physics objects in collide. Look at TouchActions, where you define a new tspot for each object. This name gives you a way of finding out where the ball is so you can draw an ellipse on it. If you just create a physics object without a name, you will have trouble finding it later on.

.@tyisamonster Here is a program I wrote back in July. http://twolivesleft.com/Codea/Talk/discussion/1349#Item_2 Everytime a blue ball collides with the red ball, another blue ball is created. I start out with 25 balls and limit it to 250. Maybe this will help you with creating a ball because of a collision. Or if you want a simpler example see the code below.

``````
displayMode(FULLSCREEN)

function setup()
tab={}
e1=physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))
e2=physics.body(EDGE,vec2(0,HEIGHT),vec2(WIDTH,HEIGHT))
e3=physics.body(EDGE,vec2(WIDTH,HEIGHT),vec2(WIDTH,0))
e4=physics.body(EDGE,vec2(WIDTH,0),vec2(0,0))
for z=1,10 do
table.insert(tab,createBody())
end
end

function draw()
background(40,40,50)
fill(255)
for a,b in pairs(tab) do
ellipse(b.x,b.y,20)
end
if create then
create=false
table.insert(tab,createBody())
end
text(#tab,WIDTH/2,HEIGHT/2)
end

function createBody()
b=physics.body(CIRCLE,10)
b.x=math.random(WIDTH)
b.y=math.random(HEIGHT)
b.gravityScale=0
b.linearVelocity=vec2(100,200)
b.restitution=1
return(b)
end

function collide(c)
if c.state==BEGAN then
if c.bodyA.shapeType==CIRCLE and c.bodyB.shapeType==CIRCLE then
if #tab<300 then
create=true
end
end
end
end

``````