# Reverse bounce

I was experimenting with repeating gestures (inspired by Blek) and I was able to get repeating gestures pretty easily. However, when I went to get them to bounce off of walls I ran into a problem. I know I am missing something obvious, and was wondering if you guys have any ideas. Here’s the code:

``````-- Gesture

-- Use this function to perform your initial setup
function setup()
points = {}
locked = false

parameter.watch("#points")
parameter.watch("locked")
parameter.watch("1/DeltaTime")
end

-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(255, 255, 255, 255)

-- This sets the line thickness
strokeWidth(5) stroke(0)

if locked then
local c = points[#points].changed
local d = points[#points].dir
if c then d = d * -1 end
local change = (points.p - points.p)
local newPoint = points[#points].p + change * d
if newPoint.x >= WIDTH or newPoint.x <= 0 or newPoint.y >= HEIGHT or newPoint.y <= 0 then
newPoint = points[#points].p - change
d = d * -1
c = true
end

table.insert(points, { p = newPoint, dir = d, changed = c })
table.remove(points, 1)
end

for i = 1, #points - 1 do
local p = points[i].p
local p2 = points[i + 1].p
line(p.x, p.y, p2.x, p2.y)
end
end

function touched(t)
local tPoint = vec2(t.x, t.y)
if not locked then
table.insert(points, { p = tPoint, dir = 1 })
end

if t.state == ENDED then
locked = true
end
end
``````

hello @jakattack. I am sorry i didnt try to find the bug in your code, i just rewrote it in ‘my style’ to do what i think you want to do. It is often easier to rewrite something rather that to understand someone else’s thinking. Here it is:
 i’ve removed a couple unnecessary lines. By the way, this little problem and solution you have posted is quite interesting (i mean how you managed to make the line propagate)

``````-- Gesture

-- Use this function to perform your initial setup
function setup()
points = {}
drawPoints = {}
locked = false

parameter.watch("#points")
parameter.watch("locked")
parameter.watch("1/DeltaTime")
end

-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(255, 255, 255, 255)

-- This sets the line thickness
strokeWidth(5) stroke(0)

if locked then
local change = (points.p - points.p)
local newPoint = points[#points].p + change

table.insert(points, { p = newPoint })
table.remove(points, 1)
local x,y = newPoint.x % (2*WIDTH), newPoint.y % (2*HEIGHT)

if x>WIDTH then x = WIDTH*2 - x
elseif x <0 then x = -x
end

if y>HEIGHT then y = HEIGHT*2 - y
elseif y<0 then y=-y
end
table.insert(drawPoints, { p = vec2(x,y) })
table.remove(drawPoints, 1)
end

for i = 1, #drawPoints - 1 do
local p1 = drawPoints[i].p
local p2 = drawPoints[i + 1].p
line(p1.x, p1.y, p2.x, p2.y)
end
end

function touched(t)
local tPoint = vec2(t.x, t.y)
if not locked then
table.insert(points, { p = tPoint })
table.insert(drawPoints, { p = tPoint })
end

if t.state == ENDED then
locked = true
end
end
``````

@Jmv38, I understand about understanding peoples thinking. Is the effect I was looking for, thank you. I will have to study and see how it is achieved P.S. What is “propagate”?

I have also been playing around with gestures. See the video below.

http://youtu.be/t4jAO0_KeAA

(It seems the parameter panel does not show up in the video).

I have implemented multiple gestures with bouncing off walls or screen wraparound. I am currently working on making the ‘wriggles’ interact with each other and also with physics bodies. As a working title i thought to call it ‘Wriggle Room’. A work in progress and lots of fun!

@piinthesky, is fun stuff Here is with multiple:

``````
--# Main
-- Gesture

-- Use this function to perform your initial setup
function setup()
parameter.watch("1/DeltaTime")
wriggles = {}
end

function draw()
background(255, 255, 255, 255)

strokeWidth(5)
for i, wriggle in ipairs(wriggles) do
wriggle:draw()
end
end

function touched(t)
if t.state == BEGAN then
table.insert(wriggles, Wriggle())
end

for i, wriggle in ipairs(wriggles) do
if not wriggle.locked then
wriggle:touched(t)
end
end
end
--# Wriggle
Wriggle = class()

function Wriggle:init(x)
self.points = {}
self.drawPoints = {}
self.locked = false

self.colour = color(math.random(1, 255), math.random(1, 255), math.random(1, 255))
end

function Wriggle:draw()
stroke(self.colour)
if self.locked then
local change = (self.points.p - self.points.p)
local newPoint = self.points[#self.points].p + change

table.insert(self.points, { p = newPoint })
table.remove(self.points, 1)
local x,y = newPoint.x % (2 * WIDTH), newPoint.y % (2 * HEIGHT)

if x > WIDTH then
x = WIDTH * 2 - x
elseif x < 0 then
x = -x
end

if y > HEIGHT then
y = HEIGHT * 2 - y
elseif y < 0 then
y = -y
end

table.insert(self.drawPoints, { p = vec2(x,y) })
table.remove(self.drawPoints, 1)
end

for i = 1, #self.drawPoints - 1 do
local p1 = self.drawPoints[i].p
local p2 = self.drawPoints[i + 1].p
line(p1.x, p1.y, p2.x, p2.y)
end
end

function Wriggle:touched(t)
local tPoint = vec2(t.x, t.y)
table.insert(self.points, { p = tPoint })
table.insert(self.drawPoints, { p = tPoint })

if t.state == ENDED then
self.locked = true
end
end
``````

very funny. Thanks for sharing.

@JakAttak very hypnotic - I like it!

Here’s a slight mod to add a fade to the trails

``````
--# Main
-- Gesture

-- Use this function to perform your initial setup
function setup()
parameter.watch("1/DeltaTime")
wriggles = {}
end

function draw()
background(255, 255, 255, 255)

strokeWidth(5)
for i, wriggle in ipairs(wriggles) do
wriggle:draw()
end
end

function touched(t)
if t.state == BEGAN then
table.insert(wriggles, Wriggle())
end

for i, wriggle in ipairs(wriggles) do
if not wriggle.locked then
wriggle:touched(t)
end
end
end
--# Wriggle
Wriggle = class()

function Wriggle:init(x)
self.points = {}
self.drawPoints = {}
self.locked = false
self.r=math.random(255)
self.g=math.random(255)
self.b=math.random(255)

end

function Wriggle:draw()

if self.locked then
local change = (self.points.p - self.points.p)
local newPoint = self.points[#self.points].p + change

table.insert(self.points, { p = newPoint })
table.remove(self.points, 1)
local x,y = newPoint.x % (2 * WIDTH), newPoint.y % (2 * HEIGHT)

if x > WIDTH then
x = WIDTH * 2 - x
elseif x < 0 then
x = -x
end

if y > HEIGHT then
y = HEIGHT * 2 - y
elseif y < 0 then
y = -y
end

table.insert(self.drawPoints, { p = vec2(x,y) })
table.remove(self.drawPoints, 1)
end

for i = 1, #self.drawPoints - 1 do
stroke(self.r,self.g,self.b,(i/#self.drawPoints)*255)
local p1 = self.drawPoints[i].p
local p2 = self.drawPoints[i + 1].p
line(p1.x, p1.y, p2.x, p2.y)
end
end

function Wriggle:touched(t)
local tPoint = vec2(t.x, t.y)
table.insert(self.points, { p = tPoint })
table.insert(self.drawPoints, { p = tPoint })

if t.state == ENDED then
self.locked = true
end
end
``````

This is now available on Codea Community Repository!

The WebRepo project allows easy access to the repo and the projects stored.