# Question on making an object bounce off walls

This is a very basic program I wrote. It is an ellipse moving. How do I make it bounce off the walls continuously?

Mike.

``````-- First Program

-- Use this function to perform your initial setup
function setup()
print("Hello World!")
x= WIDTH/2
y= HEIGHT/2
end

-- This function gets called once every frame
function draw()
background(12, 241, 12, 0)
ellipse(x, y, 100)
x= x+1
y= y+1
end

``````

See the code here, take the last version at the bottom, alter the physics settings to get the friction as you want it, this code handles collisions nicely too

http://twolivesleft.com/Codea/Talk/discussion/comment/17316

Also, use 3 tildes instead of 2 so the code doesn’t clump together.

Hey Mike,

This is the simplest bouncing ball demo I could do. the vx/vy is you displacement, change those 2 numbers in setup to anything between -1 & 1 or -20 & 20 if you want it really fast.

``````--# Main

function setup()
print("Hello World!")
x = WIDTH / 2
y = HEIGHT / 2
vx = 1;
vy = 1;
end

function draw()
x = x + vx
y = y + vy

background(12, 241, 12, 0)
ellipse(x, y, 100)

if x > WIDTH or x < 0 then
vx = -vx;
end

if y > HEIGHT or y < 0 then
vy = -vy;
end
end
``````

Does the “v” in “vx” or “vy” mean velocity?

Yep

Okay I just got the code down and I understand it. Now what/how would I make two objects bounce off each other?

for something like that you’ll want to use physics. Please note a lot of the practices you may or may not observe in my code are not advised. For example i should have used a table to keep track of the circles. And the way I’ve littered the code with comments is not very… pretty

``````supportedOrientations(ANY)

function setup()
displayMode(FULLSCREEN)

-- create the edge lines
line1 = physics.body(EDGE,vec2(5,5),vec2(5,HEIGHT-5))
-- creates the first line which starts from the bottom vec2(5,5)
-- and ends in the top left corner vec2(5,HEIGHT-5)
line2 = physics.body(EDGE,vec2(WIDTH-5,5),vec2(WIDTH-5,HEIGHT-5))
-- the reason we begin the lines 5 pixels above ground is so that
-- can see it better. Remmber vec2 is just a shorter way two secify
-- two points.
line3 = physics.body(EDGE,vec2(5,5),vec2(WIDTH-5,5))
-- bear in mind that these dont actually "DRAW" the lines
-- but rather puts invisible barriers.
line4 = physics.body(EDGE,vec2(5,HEIGHT-5),vec2(WIDTH-5,HEIGHT-5))
-- we will draw the lines later

circle1 = physics.body(CIRCLE,50) -- this creates the actual circle but DOESNT draw it
circle1.x = math.random(30,WIDTH-30) -- this gives a random position between
-- 30 and the screen width minus 30
circle1.y = math.random(30,HEIGHT-30) -- same as before but for the y position
circle1.gravityScale = 0 -- gravity doesnt affect this cicle
circle1.restitution = 1 -- this circle is bouncy
circle1.friction = 0 -- the amount of friction to be applied to the circle
circle1.linearVelocity = vec2(math.random(400),math.random(400))  -- the velocity of the circle

-- creating the second circle
circle2 = physics.body(CIRCLE,50)
circle2.x = math.random(30,WIDTH-30)
circle2.y = math.random(30,HEIGHT-30)
circle2.gravityScale = 0
circle2.restitution = 1
circle2.friction = 0
circle2.linearVelocity = vec2(math.random(400),math.random(400))
end

-- the function to be called wehn two physics objects collide
function collide(contact)
if contact.state == BEGAN then
sound(SOUND_HIT,50)    -- createca sound for collision
end
end

function draw()
background(50, 50, 50)
stroke(0,0,255)
strokeWidth(4)
noFill()
-- actually draw the circles
ellipse(circle1.x,circle1.y,100,100)
ellipse(circle2.x,circle2.y,100,100)
stroke(255)
strokeWidth(5)
-- actually draw the edge lines
line(5,5,5,HEIGHT-5)
line(WIDTH-5,5,WIDTH-5,HEIGHT-5)
line(5,5,WIDTH-5,5)
line(5,HEIGHT-5,WIDTH-5,HEIGHT-5)
end
``````

Wow! I did not think it’d take that much! Thank you for the explanation, now i’m going to study your code, and practice.

The code I linked to earlier above handles collisions, and also spins objects around, which is handy if they’re not circular

Deleted