# For Loop Physics Fun!

Can someone help me with this code? I want to have physics bodies where the ellipses are, but I do not know how to assign the x and y positions all at once in a for loop. The only experience I have with assigning physics bodies to ellipses is when they are created individually, but that seems silly in this scenario.

Here is my code:

``````
-- Use this function to perform your initial setup
function setup()
tabs={}
x=WIDTH/2-50
y= HEIGHT/2+50
end

-- This function gets called once every frame
function draw()

-- This sets a dark background color
background(40, 40, 50)
fill(255, 255, 255, 255)
strokeWidth(0)

for i=0, 10 do
for z=0, 10 do

ellipse(x+i*10, y-z*10,10,10)

end
end
end

``````

``````
function setup()
tabs={}
x=WIDTH/2-50
y= HEIGHT/2+50
for i=0, 10 do
for z=0, 10 do
ellipse(x+i*10, y-z*10,10,10)
p=physics.body(CIRCLE,4)
p.x=x+i*10
p.y=y-z*10
table.insert(tabs,p)
end
end
b=physics.body(CIRCLE,20)
b.x=WIDTH/2
b.y=300
b.type=STATIC
end

function draw()
background(40, 40, 50)
fill(255)
for a,b in pairs(tabs) do
ellipse(b.x,b.y,8)
end
ellipse(b.x,b.y,40)
end

``````

@YoloSwag - you don’t want to be creating new physics bodies each time you draw, 60 times a second. If you want 11x11=121 ellipses and physics bodies, then you should create them in setup, and modify their x and y positions in draw.

You can put the physics bodies in a 11x11 table, then you can use your loops in the draw function to change their x and y properties.

@Ignatz is right. It DOES work that way, but it would take up too much memory, u know? :-L

@aurumcoder - 121 physics bodies don’t take up too much memory at all, actually. And what I was suggesting was reducing the total number of physics objects dramatically, anyway. So I don’t understand the point you were making.

Really?

@Ignatz, i think @aurumcoder2624 was saying that it DOES work the way he did it originally, but it takes too much memory, which is why it should be done like you showed

Yes

BTW I made it so u can alter the number of ellipses there are.

```-- FLPF

function setup()
tabs={}
size = 20--can be anynumber
bsize = 8 -- can be anynumber
fnumber = 9-- can be anynumber
bynumber = 7 -- can be anynumber
-- fnumber * bynumber is the amount of ellipses!
x=WIDTH/2-50
y= HEIGHT/2+50
for i=1, fnumber do
for z=1, bynumber do
ellipse(x+i*bsize, y-z*bsize,bsize,bsize)
p=physics.body(CIRCLE,4)
p.x=x+i*10
p.y=y-z*10
table.insert(tabs,p)
end
end
b=physics.body(CIRCLE, size)
b.x=WIDTH/2
b.y=300
b.type=STATIC
end

function draw()
background(40, 40, 50)
fill(255)
for a,b in pairs(tabs) do
ellipse(b.x,b.y,bsize)
end
ellipse(b.x,b.y,size)
end

```

You alter the num by altering fnumber and bynumber.

But I can't center the ellipses. Can someone help?```

@aurumcoder2624 All you did was move the 10s into variables…but what do you mean, center the ellipses?

I didn’t. I added variables, I changed 0 into 1, and I did move the tens into vars.

I altered the code so it shows a different asset. Now run the code… And compare it to @dave1707s code.

If u run the code with fnumber as 1 and bynumber as 3 or fnumber as 3 and bynumber as 1 then u will see what I mean.

@aurumcoder2624 Either way, you could have done the same thing with the exact same amount of effort in the original code.

Huh?

This centers the circles over the larger circle as you change “w”, the width of the group.

``````
displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT_ANY)

function setup()
w=20    -- change for width
h=10    -- change for height
tabs={}
x=(WIDTH-w*10-10)/2 -- center group over circle
y=HEIGHT-200
for i=1,w do
for z=1,h do
p=physics.body(CIRCLE,5)
p.x=x+i*10
p.y=y-z*10
table.insert(tabs,p)
end
end
b=physics.body(CIRCLE,50)
b.x=WIDTH/2
b.y=300
b.type=STATIC
end

function draw()
background(40, 40, 50)
fill(255)
for a,b in pairs(tabs) do
ellipse(b.x,b.y,10)
end
ellipse(b.x,b.y,100)
end

``````

Thanks, old buddy!