How to make walls

I am fairly new to programming and have been working on a small game which involves an ellipse moving around the screen and capturing smaller ones. The only problem I am having with it is keeping the ball on the screen. It does not involve gravity. Is there any way i can make invisible walls on the side of the screen to set a boundary.

function setup()
-- this creates the circle
c1 = physics.body(CIRCLE,70)
c1.x=400
c1.y=400
c1.type=STATIC

end

function draw()

background(147, 119, 170, 255)
--this is what is shown as the circle
ellipse(c1.x, c1.y, 100, 100)

strokeWidth(10)

-- Do your drawing here

 end

 function touched(touch)
--makes sure the circle only moves where you drag it
 if touch.state == MOVING then

 c1.x = touch.x
 c1.y = touch.y
 end 
     end    

--not finished

This entire thing seems weird, how are you having trouble with it going off screen if it can only follow your finger?

I just want the circle to remain on screen and not have half of it not showing. I just want it to look more professional

c1.x = math.max(math.min(touch.x, WIDTH - c1.radius), c1.radius)

c1.y = math.max(math.min(touch.y, HEIGHT - c1.radius), c1.radius)

That should do it.

@newow Try this.


function setup()
    -- this creates the circle
    c1 = physics.body(CIRCLE,70)
    c1.x=400
    c1.y=400
    c1.type=STATIC
end

function draw()
    background(147, 119, 170, 255)
    --this is what is shown as the circle
    ellipse(c1.x, c1.y, 100, 100)
    
    strokeWidth(10)
    
    -- Do your drawing here
    
end

function touched(touch)
    --makes sure the circle only moves where you drag it
    if touch.state == MOVING then    
        if touch.x>50 and touch.x<WIDTH-50 then
            c1.x = touch.x
        end
        if touch.y>50 and touch.y<HEIGHT-50 then
            c1.y = touch.y
        end
    end 
end    

--not finished

I see, well you will have to get rid of the

c1.x = touch.x
c1.y = touch.y

Because that would allow it to go through walls, for example, lets say you have a brick wall in the middle of the screen, and the ball is on one side of it. If you tap on the other side of the wall, the ball will magically teleport over there. So you want to “add force” to the physic body, here is a example:

function touched(t)
if t.state ~= ENDED then 
    dx = t.x-c1.x
    dy = t.y-c1.y
    if(dx < 0) then
        dx = -speed
        elseif(dx > 0) then
            dx = speed
    end
      if(dy < 0) then
        dy = -speed 
        elseif(dy > 0) then
            dy = speed
            end
 force = vec2(dx, dy) 
c1.linearVelocity = force


    end

    end

end

Basically, we are testing where the finger is around the ball, and adding a little bit of force to make it slowly move. Now to answer your question, to make the walls you would do something like this:

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

Thanks

Hopefully I don’t have anymore problems with this