# A physics problem

okay here i go again, here’s the source code

``````function setup()
--displayMode(FULLSCREEN)
for i=1,16 do
print(i)
end
circle = physics.body(CIRCLE, 60)
circle.x = WIDTH/2
circle.y = HEIGHT/2
circle.gravityScale = 1
end

function draw()
background(255, 255, 255)
strokeWidth(5)
fill(135, 84, 84, 255)
ellipse(circle.x,circle.y,60)
end

function touched(touch)
tx = touch.x
ty = touch.y
end
``````

from there i want to be able to interact witht he circle through touch events but… 1. the circle doesnt show up till i declare an ellipse with circles’ x and y values and 2. i cant give the circle the x and y touch values either because it throws me an error, what im trying to do is create a (2) circle(s) and have them interact with each other and also interact with walls, how would i go about doing such a thing?

@Dalorbi

Is this what you’re after.

``````
supportedOrientations(PORTRAIT)

function setup()
displayMode(FULLSCREEN)
tab={} -- table for circles

-- create the edge lines
line1 = physics.body(EDGE,vec2(5,5),vec2(5,HEIGHT-5))
line2 = physics.body(EDGE,vec2(WIDTH-5,5),vec2(WIDTH-5,HEIGHT-5))
line3 = physics.body(EDGE,vec2(5,5),vec2(WIDTH-5,5))
line4 = physics.body(EDGE,vec2(5,HEIGHT-5),vec2(WIDTH-5,HEIGHT-5))

for z=1,3 do    -- create circles
tab[z] = physics.body(CIRCLE,50)
tab[z].x=math.random(30,WIDTH-30)
tab[z].y=math.random(30,HEIGHT-30)
tab[z].gravityScale=0
tab[z].restitution=1
tab[z].friction=0
tab[z].linearVelocity=vec2(math.random(400),math.random(400))
end
end

function collide(contact)
if contact.state == BEGAN then
sound(SOUND_HIT,50)    -- createca sound for collision
end
end

function draw()
background(50, 50, 50)

-- draw the circles
stroke(0,0,255)
strokeWidth(4)
noFill()
for z=1,#tab do
ellipse(tab[z].x,tab[z].y,100,100)
end

-- draw the edge lines
stroke(255)
strokeWidth(5)
line(5,5,5,HEIGHT-5)
line(WIDTH-5,5,WIDTH-5,HEIGHT-5)
line(5,5,WIDTH-5,5)
line(5,HEIGHT-5,WIDTH-5,HEIGHT-5)
end

``````

almost exactly thanks just 2 things, 1. Is there a way i can interact with them through touch events and 2. should i always create a table for a hysics body? whats up with tables and hysics bodies anyway?

You don’t need to use tables, but they save time and code. In the above example, I use 3 circles. I could just as easily have 30 circles just by changing the 3 to 30 in the for loop in setup. If I wasn’t using a table, I would have key in 30 physics bodies which would take a lot of time and code. If you have a lot of physics bodies that are similar, use tables. If they’re different and not many of them, don’t use tables. As for interacting through touch, that’s going to depend on what you’re doing and how you want to interact. As for what’s up with physics bodies, that’s how you can do a lot of simulations or games. See the physics lab under the example programs for more examples.

that was a very, very insightful example @dave1707, in just 2 hours i’ve gotten a lot of the basic concepts in Codea Physics, just need to learn up on my vectors. And yes `"touch" events` my old enemy. It seems no matter what i do, the compiler refuses to do something like this `tab[z].x = touch.x` is there a reason the compiler hates me or is it because anything along those lines is… well… just wrong?

@Dalorbi

Add this function to my program. Touch one of the circles and drag it around. This will show you how to touch and move the circles, but it’s not coded to alter the circles x or y velocity.

``````
function touched(t)
if t.state==MOVING or t.state==BEGAN then
for z=1,#tab do
yc=tab[z].y
xc=tab[z].x
a=50
b=50
if (t.y-yc)^2/b^2+(t.x-xc)^2/a^2 <= 1 then   -- touched inside the circle
tab[z].x=t.x
tab[z].y=t.y
end
end
end
end

``````

I simplified the code a little. The built in vec2 functions can come in handy for this sort of thing.

``````function touched(t)
if t.state==MOVING or t.state==BEGAN then
for k,circle in ipairs(tab) do
-- touched inside the circle
if circle.position:dist(vec2(t.x, t.y)) < circle.radius then
circle.x = t.x
circle.y = t.y
end
end
end
end
``````

Thanks @John. I’ll have to look into that and see how “dist” works.

@John, I finally had a chance to play around with “dist” and what it does. Even though it works in this case, circles, it can’t be used for ellipses. The formula that I use will work for both circles and ellipses. At least I now know what “dist” does.