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
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
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))