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
blendMode(ADDITIVE)
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