# keep a small body circle inside a large one

I have created 2 circle bodies, one large is static and one small is dynamic.
I want to keep the small circle inside the large one, bouncing with the inside border. I’ve tried with edges but it’s complicate because of numerus generated contacts when entering the large cell.

Is there a simple solution ?

Thanks for your help.

Leon

@Leon_levy

I couldn’t get it to work with them both being physics bodies, so I did it this way. I just keep track of the position of the physics circle and if it gets close to the inside of the non physics circle then I simulate a collision. I’m not sure what you’re trying to do, so I just keep bouncing the smaller circle inside the larger one. Run this in portrait mode. If it gets stuck, press the replay button.

``````
function setup()
displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT)
x1=0
y1=100
c2 = physics.body(CIRCLE,25)
c2.x=WIDTH/2+80
c2.y=HEIGHT/2
c2.gravityScale=0
c2.linearVelocity=vec2(x1,y1)
xc=WIDTH/2
yc=HEIGHT/2
a=150
b=150
end

function draw()
background(30, 30, 30, 25)
stroke(255)
strokeWidth(1)
noFill()
ellipse(WIDTH/2,HEIGHT/2,300,300)
ellipse(c2.x,c2.y,50,50)
if (c2.y-yc)^2/b^2+(c2.x-xc)^2/a^2 > .65 then
if x1==0 then
x1=y1*-1
y1=0
else
y1=x1*1
x1=0
end
c2.linearVelocity=vec2(x1,y1)
end
end

``````

Hey @Leon_levy, I tried this and found it quite simple using the CHAIN physics type. I wasn’t sure if you wanted the small circle to go into the other circle and stay there or not, so I never implemented it (but it would be quite easy to implement). Here is my code:

``````
function setup()
physics.gravity(0,0)
iparameter("Detail", 3, 720, 20)
circle = physics.body(CIRCLE, 50)
circle.position = vec2(WIDTH / 2, HEIGHT / 2)
circle.linearVelocity = vec2(math.random(100, 200), math.random(100, 200))
circle.restitution = 1
points = {}
for i = 0, Detail do
a = math.rad((360 / Detail) * i)
table.insert(points, 1, vec2(math.cos(a), math.sin(a)) * 200 + vec2(WIDTH / 2, HEIGHT / 2))
end
boundary = physics.body(CHAIN, unpack(points))
prevDetail = Detail
end

function draw()
if Detail ~= prevDetail then
points = {}
for i = 0, Detail do
a = math.rad((360 / Detail) * i)
table.insert(points, 1, vec2(math.cos(a), math.sin(a)) * 200 + vec2(WIDTH / 2, HEIGHT / 2))
end
boundary = physics.body(CHAIN, unpack(points))
end
background()
fill(255, 0, 0, 255)
ellipse(circle.x, circle.y, 50)
for i, v in pairs(boundary.points) do
point(v.x, v.y)
end
prevDetail = Detail
end
``````

I have implemented Jordan’s solution and it worked fine.
Lot of thanks to you, Jordan.
I will use Dave’s collision simulation.

Thanks for your great help to both of you.

Leon from France