# Ball split physics?

I want one ball to split into two balls that bounce away from each other if I tap and those balls get split into smaller balls and so on. This is my code

``````function move(cuex,cuey,speedx,speedy)
local p = physics.body(CIRCLE,20)
p.restitution = .8
p.linearVelocity = vec2(speedx,speedy)
p.linearDamping = 0.8
p.x = cuex
p.y= cuey
return p
end
``````

Wait sorry this is my code

``````-- Stick Physics

-- Use this function to perform your initial setup
function setup()
physics.gravity(0,-100)
w1 = physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
w2 = physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))
w3 = physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT))
ball = move(100,300,100,-200, 60)
end
function touched(t)
if t.state == BEGAN then
end
end
function move(posx,posy,speedx,speedy, size)
local p = physics.body(CIRCLE,size)
p.restitution = .8
p.linearVelocity = vec2(speedx,speedy)
p.x = posx
p.y = posy
p.size = size
p.bullet = true
return p
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)

-- This sets the line thickness
strokeWidth(5)
fill(255, 0, 19, 255)
ellipse(ball.x,ball.y,(ball.size*2))
end
``````

Do you want each ball to split into 2 only when you tap the screen. When the balls split, do they keep the original size or are the 2 balls half the size of the original ball. If they reduce in size, is there a minimum size where they don’t get smaller.

They split into half the size and the minimum size is 5

Only when I touch

When you create the balls, they should go in a table. When you tap the screen, you loop thru the table and create a new ball for each ball in the table. You reduce the size of the balls.

How come this doesn’t work?

``````-- Stick Physics

-- Use this function to perform your initial setup
function setup()
physics.gravity(0,-100)
w1 = physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
w2 = physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))
w3 = physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT))
ball = move(100,300,100,-200, 60)
end
balltable = {ball}
function touched(t)
if t.state == BEGAN then
for i in balltable do
newball = move(i.x,i.y,-10,0)
end
end
end
function move(posx,posy,speedx,speedy, size)
local p = physics.body(CIRCLE,size)
p.restitution = .8
p.linearVelocity = vec2(speedx,speedy)
p.x = posx
p.y = posy
p.size = size
return p
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)

-- This sets the line thickness
strokeWidth(5)
fill(255, 0, 19, 255)
ellipse(ball.x,ball.y,(ball.size*2))

end
``````

Your for loop in touched is incorrect. The format is `for a,b in pairs(balltable) do` where a and b are any variable name you want.

Why doesn’t this work

``````function setup()
physics.gravity(0,-100)
w1 = physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
w2 = physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))
w3 = physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT))
ball = move(100,300,100,-200, 60)
end
balltable = {ball}
function touched(t)
if t.state == BEGAN then
for a,b in pairs(balltable) do
newball = move(a.x,a.y,-10,0,30)
table.insert(balltable,newball)
end
end
end
function move(posx,posy,speedx,speedy, size)
local p = physics.body(CIRCLE,size)
p.restitution = .8
p.linearVelocity = vec2(speedx,speedy)
p.x = posx
p.y = posy
p.size = size
return p
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)

-- This sets the line thickness
strokeWidth(5)
for a,b in pairs(balltable) do
fill(255, 0, 19, 255)
ellipse(b.x,b.y,(b.size*2))
end

end
``````

balltable={ball} should be inside a function, move it up a line after ball=move. The next problem you’re going to run into is in the touched function. As you’re going thru the for loop, you add another ball to the table. You’ll never reach the end of the table. Also, you need to use b as in b.x and b.y in the move() in the touched function. The variable a is just a count, b is the object.

How would I modify the first ball to change its size

This is what i got but, as you know I want the smaller balls to split into more balls

``````function setup()
physics.gravity(0,-100)
w1 = physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
w2 = physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))
w3 = physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT))
ball = move(100,300,100,-200, 60)
balltable = {ball}
balls = {ball}
end
function touched(t)
if t.state == BEGAN then
for a,b in pairs(balltable) do
newball = move(b.x+b.size/2,b.y,-100,0,b.size/2)
table.insert(balls,newball)
end
end
end
function move(posx,posy,speedx,speedy, size)
local p = physics.body(CIRCLE,size)
p.restitution = .8
p.linearVelocity = vec2(speedx,speedy)
p.x = posx
p.y = posy
p.size = size
return p
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)

-- This sets the line thickness
strokeWidth(5)
for a,b in pairs(balls) do
fill(255, 0, 19, 255)
ellipse(b.x,b.y,(b.size*2))
end

end
``````

Would you mind modifying this to do that?

I don’t think you can. I’ll have to verify that. I think you need to destroy the original ball and create 2 new half sized balls.

I checked and what I see is the physics circle has a radius variable. You can modify the value and set the radius.

Here’s a version. Tap the screen to create the first ball then tap again to split them. If you go over 512 balls, the FPS get really low and the program will probably crash.

``````Code removed. See updated code below.
``````

is there away for the balls to pass through each other instead of colliding?

but not pass through the walls

Yes. Check mask for physics body.

But, how do you make them pass through each other after they find another ball physics body