# Physics bug

I made this its not very clean but I’m just getting back in to the swing of things, I really like using the codea software and v1.5 is a really good upgrade to the previous version but there’s a problem with the physics, if you run the following script then eventually the box will just fall through the floor which is a problem, or it will go through a wall, any input is good, thanks

```-- platformer

-- Use this function to perform your initial setup
function setup()
print("Hello World!")
displayMode(FULLSCREEN)
scr=vec2()
scr.x=WIDTH/2
scr.y=HEIGHT/2
box=physics.body(POLYGON,vec2(-30,30),vec2(-30,-30),vec2(30,-30),vec2(30,30))
box.x=scr.x-500
box.y=scr.y-200
box.mass=5
box.friction = 1
box.restitution = 0.4
box.interpolate = true
box.sleepingAllowed = false
pos={}
n=9
mspeed=0
touching=0
stepx=0
stepy=0
stickv=vec2()
spos=vec2()
for i=0,1000 do
if n >= 10 then
stepy=stepy+math.random(1,6)
stepx=-100
n=0
else
stepx=0
n=n+1
end
pos[i]=vec2(i*100-400+stepx,stepy*15-200)
end
physics.body(CHAIN,false,unpack(pos))
n=0
end

function collide(c)
if c.state == BEGAN or c.state == MOVING then
touching=1
elseif c.state == ENDED then
touching=0
end
end

function xjoyStick(pos)
local ct=CurrentTouch
--local spos = vec2()
rect(pos.x,pos.y-10,190,20)
spos.x = ct.x-pos.x-scr.x
stickv.y = pos.y
if ct.state == MOVING or ct.state == BEGAN and vec2(ct.x,ct.y):dist(pos)<200 then
stickv.x = pos.x+spos.x
elseif ct.state == ENDED then
stickv.x = pos.x+97.5
end
stickv.x = math.max(stickv.x,pos.x)
stickv.x = math.min(stickv.x,pos.x+195)
ellipse(stickv.x,stickv.y,50)
mspeed=-(pos.x-stickv.x+97.5)
end

-- This function gets called once every frame
function draw()
-- Screen scrolling from box position
if box.x>WIDTH-200-scr.x then
scr.x=WIDTH-200-box.x
elseif box.xHEIGHT-100-scr.y then
scr.y=HEIGHT-100-box.y
elseif box.ypos[n+39].x then
n=n+1
end
line(pos[i-1].x,pos[i-1].y,pos[i].x,pos[i].y)
end
end

``````

I have not analyzed your code, but usually people get your problem wwhen they forgot to define pysical objects for the boudary (edges).

Not sure it’s a bit weird i think I sorted it now, more to the point, I don’t get why it’s posted half my script in normal text

.@Luatee you can post code by wrapping your code in:

``````
<pre lang="lua">
</pre>

``````

Or by putting three `tilde` characters `~~~` above and below it:

``````
~~~
code here
~~~

``````

Using the first option will give you syntax highlighting (as I added to your OP above).

That’s much better, thanks

Is there a solution i have the same problem

Yes there is, its due to the garbage collector removing lost memory i.e. anything that isn’t defined by a variable of some sort. You need to make sure you do something like body = physics.body(blah blah). Where I put

``````physics.body(CHAIN,false,unpack(pos))
``````

You should make that

``````yourvar = physics.body(CHAIN,false,unpack(pos))
``````

I don’t understand it
Here is my code
If you wait like 3 minutes the balls fall down

``` -- splitsdoosje

supportedOrientations(LANDSCAPE_ANY)
function setup()
displayMode(FULLSCREEN)

ballen={}
for i=1,10 do
ballen[i]=Ballen(math.random(WIDTH/5,WIDTH),HEIGHT,30)
end
createWalls()
end

-- This function gets called once every frame
function draw()
physics.gravity(Gravity)
-- This sets a dark background color
background(40, 40, 50)

pushStyle()--geel vlak
noStroke()
fill(240, 189, 19, 255)
rect(WIDTH/5,HEIGHT-100,WIDTH-50,HEIGHT-100)
popStyle()

pushStyle()
noStroke()--rood vlak
fill(176, 28, 28, 255)
rect(1,1,205,HEIGHT)

sprite("Documents:splitsdoos",WIDTH/2+100,HEIGHT/2)

for i=1, #ballen do --ballen tekenen
pushStyle()
strokeWidth(7)
stroke(255, 255, 255, 255)
fill(248, 19, 19, 255)
ellipse(ballen[i].bal.x,ballen[i].bal.y,ballen[i].r)
popStyle()
end
end
function createWalls()
leftWall=createWall(WIDTH/5,HEIGHT,WIDTH/5,HEIGHT-100)
rightWall=createWall(WIDTH,HEIGHT,WIDTH,HEIGHT-100)
bottomWall=createWall(WIDTH/5,HEIGHT-100,WIDTH,HEIGHT-100)

end
function createWall(x,y,x2,y2)
local w = physics.body(EDGE,vec2(x,y),vec2(x2,y2))
w.type=STATIC
w.restitution=.6
w.sleepingAllowed=false

end

Ballen = class()

function Ballen:init(x,y,r)

-- you can accept and set parameters here
self.r=60
self.bal=physics.body(CIRCLE,self.r/2)
self.bal.type=DYNAMIC
self.bal.x=x
self.bal.y=y
self.bal.restitution=.5
self.bal.gravityScale=.9
self.bal.sleepingAllowed=false

end

function Ballen:draw()
-- Codea does not automatically call this method
end

function Ballen:touched(touch)
-- Codea does not automatically call this method
end

``````

The problem is that in CreateWall, w is local, so it is destroyed on exiting the function. This means your wall doesn’t have any Codea variables referencing it, and the garbage collector will destroy it soon after.

All you need to do is add one line to the end of createWall

``````    return w
``````

and because you are putting the results of CreateWall into variables (leftWall, rightWall,…), the walls will not self destruct

Thanks