# Code for "Pickle in a Cup" (need help and suggestions)

Hey! I would love some help and suggestions with my code! The collision detection I need the most help with, the cup and pickle do not collide. Thanks everyone! :)>-

-- Pickle in a Cup
function setup()
pickle = physics.body(CIRCLE, 20)
-- pickle.gravityScale = 1
pickle.x = WIDTH/8
pickle.y = HEIGHT-50
cup = vec2((WIDTH/8)-50, 100)
random=1
end

function draw()
pickle.type = STATIC
pickle.y = pickle.y - 14
sprite("Dropbox:chowder-cartoon-network-wallpaper-26",WIDTH/2,HEIGHT/2, WIDTH, HEIGHT)
sprite("Dropbox:chases-pickle", pickle.x, pickle.y, 80, 150)

fill(255, 255, 255, 255)
rect(cup.x,cup.y, 100, 100)
if cup.x < 0 then
cup.x=cup.x+WIDTH/4
end
if cup.x > 768 then
cup.x=cup.x-WIDTH/4
end

if pickle.y<0 then
random=math.random(1,4)
pickle.y=HEIGHT-50
if
random==1 then pickle.x=WIDTH/8 end
if random==2 then pickle.x=(3*WIDTH)/8 end
if random==3 then pickle.x=(5*WIDTH)/8 end
if random==4 then pickle.x= (7*WIDTH)/8 end

if pickle.y<=cup.y+100 and pickle.x<=cup.x+50 and pickle.x>=cup.x-50 then
random=math.random(1,4)
pickle.y=HEIGHT-50 end
if  random==1 then pickle.x=WIDTH/8 end
if random==2 then pickle.x=(3*WIDTH)/8 end
if random==3 then pickle.x=(5*WIDTH)/8 end if random==4 then pickle.x= (7*WIDTH)/8 end

end

end

function touched(touch)
if cup.x > 0 and cup.x <768 then
if touch.state==BEGAN then
if touch.x < WIDTH/2 then
cup.x=cup.x-WIDTH/4
end
if touch.x > WIDTH/2
then cup.x=cup.x+WIDTH/4
end
end
end
end

@chasem907 pickle is defined as a physics body, but cup isn’t. You need both items defined as physics bodies for them to collide.

Also, don’t set the pickle’s type to STATIC, leave it as DYNAMIC, and don’t try and change its coordinates manually (other than setting them once), the physics engine does that for you.

Edit: Here’s a working version:

-- Pickle in a Cup
function setup()
pickle = physics.body(CIRCLE, 20)
--pickle.gravityScale = 1
pickle.x = WIDTH/8
pickle.y = HEIGHT-50
--cup = vec2((WIDTH/8)-50, 100)
cup = physics.body(POLYGON, vec2(-50, -50), vec2(-50, 50), vec2(-40, 50), vec2(-40, -40), vec2(40, -40), vec2(40, 50), vec2(50, 50), vec2(50, -50))
cup.x = WIDTH / 8
cup.y = 100
cup.type = STATIC
random=1
end

function draw()
rectMode(CENTER)
background(0)
sprite("Dropbox:chowder-cartoon-network-wallpaper-26",WIDTH/2,HEIGHT/2, WIDTH, HEIGHT)
fill(255, 255, 255, 255)
--rect(cup.x,cup.y, 100, 100)
pushMatrix()
stroke(255)
strokeWidth(5)
translate(cup.x, cup.y)
for k, v in ipairs(cup.points) do
local next = (k) % #cup.points + 1
line(v.x, v.y, cup.points[next].x, cup.points[next].y)
end
popMatrix()
sprite("Dropbox:chases-pickle", pickle.x, pickle.y, 80, 150)

if cup.x < 0 then
cup.x=cup.x+WIDTH/4
end

if cup.x > 768 then
cup.x=cup.x-WIDTH/4
end

if pickle.y<0 then
random=math.random(1,4)
pickle.y=HEIGHT-50
if random==1 then pickle.x=WIDTH/8 end
if random==2 then pickle.x=(3*WIDTH)/8 end
if random==3 then pickle.x=(5*WIDTH)/8 end
if random==4 then pickle.x= (7*WIDTH)/8 end

if pickle.y<=cup.y+100 and pickle.x<=cup.x+50 and pickle.x>=cup.x-50 then
random=math.random(1,4)
pickle.y=HEIGHT-50
end

if random==1 then pickle.x=WIDTH/8 end
if random==2 then pickle.x=(3*WIDTH)/8 end
if random==3 then pickle.x=(5*WIDTH)/8 end if random==4 then pickle.x= (7*WIDTH)/8 end
end
end

function touched(touch)
if cup.x > 0 and cup.x <768 then
if touch.state==BEGAN then
if touch.x < WIDTH/2 then
cup.x=cup.x-WIDTH/4
end
if touch.x > WIDTH/2 then
cup.x=cup.x+WIDTH/4
end
end
end
end

Thanks!