# Moving box with collision?

Edit-- I edited this rather than clutter up the forum with another thread.

Question:-

Would there be a ‘simple’ way of making pairs of rectangles moving up the screen have collision detection?

Ie ‘---------- ----------’ and ‘----- ---------------’ and ‘-------- ------------’

I want objects to collide and bounce off where the rectangles ‘–’ are, but the objects will fall through the gaps.

I have tried myself, after trying several different ideas I took to reading the builtin reference guide for physics but it makes my brain melt when it talks of weird alien things like vec2 etc.

If there is no ‘very easy’ way to do this, just tell me and I’ll move on to a simpler idea.

Thank you

I’ve been playing with this all afternoon, I got my line to move and the ball is controlled by tilting the pad but the ball doesn’t fall through the gap.

Some advice would be wonderful (and yeah, I know my code will be terrible)

``````
-- Physics

-- Use this function to perform your initial setup
function setup()
w2=physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))
w3=physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT))
ball=physics.body(CIRCLE,25)
ball.x=250
ball.y=650
ball.restitution=0.4
x=350
yy=1
end

-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
pushStyle()
fill(255,255,0)
x=x+Gravity.x*10
ellipse(x,ball.y,50)
popStyle()
w1=physics.body(EDGE,vec2(0,yy),vec2(WIDTH/2-50,yy))
w4=physics.body(EDGE,vec2(WIDTH/2+50,yy),vec2(WIDTH,yy))
yy=yy+1
-- This sets the line thickness
strokeWidth(4)
line(0,yy,WIDTH/2-50,yy)
line(WIDTH/2+50,yy,WIDTH,yy)
line(0,0,0,HEIGHT)
line(WIDTH,HEIGHT,WIDTH,0)
end

``````

``````ball.x
``````

Why? Cause ball.x is not inside the gap and thats why the ball cant fall through.

@SteveNoob Does this code help any.

``````
-- Physics

function setup()
yy=0
w1=physics.body(EDGE,vec2(0,0),vec2(WIDTH/2-50,0))
w2=physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))
w3=physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT))
w4=physics.body(EDGE,vec2(WIDTH/2+50,0),vec2(WIDTH,0))
ball=physics.body(CIRCLE,25)
ball.x=250
ball.y=650
ball.restitution=1
ball.sleepingAllowed=false
end

function draw()
background(40, 40, 50)
ball.x=ball.x+Gravity.x*10
fill(255,255,0)
ellipse(ball.x,ball.y,50)
yy=yy+1
w1.y=yy
w4.y=yy
strokeWidth(4)
line(0,w1.y,WIDTH/2-50,w1.y)
line(WIDTH/2+50,w1.y,WIDTH,w1.y)
line(0,0,0,HEIGHT)
line(WIDTH,HEIGHT,WIDTH,0)
end

``````

Thanks both of you.

dave, wow… you not only made it smaller but its smoother and works perfectly.

Thank you, mine looks so messy and terrible next to yours.

Edit: Removed

@SteveNoob, my apologies, I did not mean to write the entire thing for you. It still needs polishing to be a game for example there is no winning or losing, graphics are very plain etc, but regardless I have removed the code above so you can continue to explore how to do this yourself.

JakAttak, I’m sorry for what I said.

I’m just so frustrated and I feel like I’m really really stupid and thick. I’ve decided Codea is beyond my old brain and I’ll leave the coding to you experts.

Take care and again, sorry for being mean.

@SteveNoob You feel that way because you don’t understand Lua and Codea. None of us were born experts. You just have to start out small. Don’t try to write something beyond what you know. Start with what you understand, then start adding another command or function and try different things with that. Once you understand what that’s doing, add something else and play with that. Eventually you’ll understand enough to write what you want. I’ve been playing around with Codea a little over a year and there’s still more that I don’t understand than I do. Don’t expect to know it all overnight because it isn’t going to happen. Try things and if you still can’t figure it out, ask. Take small examples of code that are given on the forum and try to understand what they’re doing. Comment out lines of code or change something and see what happens. Experiment.

Edit: See above

You wrote the entire game for me…

Well I guess theres no point me bothering now. …great

…great

@SteveNoob He didn’t write the game for you because nothing says you have to use his. Continue to write your game on your own to learn and if you get stuck on something that doesn’t work, you can refer to his game to see how it was done. Just about everything on this forum has already been written in one form or another. There have been things that some people have asked about that I’ve already written 25-30 years ago. There are very few original ideas that are showing up and the Internet is loaded with examples of most of them. So don’t stop, there’s a lot more you still have to do and learn.