# physics collisions

How do I make the ball hit the 2 rectangles whenever the ball hits one of them it just goes through it.

``````displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT)

function setup()
stroke(255, 0, 0, 255)
strokeWidth(2)

--walls

left = physics.body(EDGE, vec2(0, 0), vec2(0, HEIGHT))
right = physics.body(EDGE, vec2(WIDTH, 0), vec2(WIDTH, HEIGHT))

--ball

ball = physics.body(CIRCLE, 10)
ball.position = vec2(WIDTH/2, HEIGHT/2)
ball.linearVelocity = vec2(ball.x, ball.y)
ball.restitution = 1.01
ball.gravityScale = 0
ball.friction = 0

--rectangle
r = physics.body(POLYGON, vec2(WIDTH/2+40, HEIGHT/10), vec2(WIDTH/2-40, HEIGHT/10),
vec2(WIDTH/2-40, HEIGHT/10-5), vec2(WIDTH/2+40, HEIGHT/10-5))
r.position = vec2(WIDTH/2, HEIGHT/10)
r.linearVelocity = vec2(WIDTH/2, HEIGHT/10)
r.restitution = 1.1
r.gravityScale = 0
r.friction = 0
r.type = STATIC

--rectangle 2

r2 = physics.body(POLYGON, vec2(WIDTH/2+40, HEIGHT/1.1), vec2(WIDTH/2-40, HEIGHT/1.1),
vec2(WIDTH/2-40, HEIGHT/1.1-5), vec2(WIDTH/2+40, HEIGHT/1.1-5))
r2.position = vec2(WIDTH/2, HEIGHT/1.1)
r2.linearVelocity = vec2(WIDTH/2, HEIGHT/1.1)
r2.restitution = 1.1
r2.gravityScale = 0
r2.friction = 0
r2.type = STATIC

end

function draw()
background(0, 0, 0, 255)
ellipse(ball.x, ball.y, 20)
rect(r.x, HEIGHT/10, 80, 5)
rect(r2.x, HEIGHT/1.1, 80, 5)

end

function touched(t)
if math.abs(t.x-r.x)<200 and math.abs(t.y-r.y)<200 then
r.x=t.x

end
if math.abs(t.x-r2.x)<200 and math.abs(t.y-r2.y)<200 then
r2.x=t.x
end
end
``````

@Joey720999 You have to set the two bodies to a certain type of physics body. Look at the reference manual under physics to see how to set it and what types you need

@Goatboy76 ok thanks

@Joey72099 I changed your code a little to get it working. You can change it to your liking. I changed the beginning velocity so it bounces from rectangle to rectangle to show it works.

``````
displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT)

function setup()
rectMode(CENTER)
stroke(255, 0, 0, 255)
strokeWidth(2)

--walls
left = physics.body(EDGE, vec2(0, 0), vec2(0, HEIGHT))
right = physics.body(EDGE, vec2(WIDTH, 0), vec2(WIDTH, HEIGHT))

--ball
ball = physics.body(CIRCLE, 10)
ball.position = vec2(WIDTH/2, HEIGHT/2)
ball.linearVelocity = vec2(0,200)
ball.restitution = 1
ball.gravityScale = 0
ball.friction = 0

--rectangle
r = physics.body(POLYGON,vec2(-40,5), vec2(-40,-5),vec2(40,-5),vec2(40,5))
r.position=vec2(WIDTH/2,HEIGHT-50)
r.restitution = 1
r.gravityScale = 0
r.type = STATIC

--rectangle 2
r2 = physics.body(POLYGON,vec2(-40,5), vec2(-40,-5),vec2(40,-5),vec2(40,5))
r2.position=vec2(WIDTH/2,50)
r2.restitution = 1
r2.gravityScale = 0
r2.type = STATIC
end

function draw()
background(0, 0, 0, 255)
ellipse(ball.x, ball.y, 20)
rect(r.x, r.y, 80, 10)
rect(r2.x, r2.y, 80, 10)
end

function touched(t)
if math.abs(t.x-r.x)<200 and math.abs(t.y-r.y)<200 then
r.x=t.x
end
if math.abs(t.x-r2.x)<200 and math.abs(t.y-r2.y)<200 then
r2.x=t.x
end
end

``````