# Tweening mesh vertices

If I want to shift the vertices of a mesh to some other point, how can I do so using Tweens?

``````function setup()
m = mesh()
m.vertices = {vec2(WIDTH/2, HEIGHT/2), vec2(WIDTH/3, HEIGHT/2), vec2(3*WIDTH/5, HEIGHT/3)}
for k,v in pairs(m.vertices) do
tween(1, v, {x = 0, y = 0})
end
end

function draw()
background()
m:draw()
end
``````

This doesn’t seem to work. But if I define a variable ‘x’ and cahange it in draw it works, like so.

``````function setup()
m = mesh()
x = WIDTH
end

function draw()
background()
x = x - 1
m.vertices = {vec2(x/2, HEIGHT/2), vec2(x/3, HEIGHT/2), vec2(3*x/5, HEIGHT/3)}
m:draw()
end
``````

I’m pretty sure there is a silly mistake in the first one.

I can define a variable x in the first case and tween x till 0, but I want to know why this method doesn’t seem to work.
Is there a better method to do the same?

@Saurabh - I’m not sure it’s a good idea to set up a separate tween for each vertex!

I’m guessing it doesn’t work because the vertex “v” you are tweening is only a temporary variable.

The second approach is much more efficient.

Maybe the second method is the way then.

@Ignatz I didn’t understand temporary variable, something like locals?

If so then even if you do the loop in draw like so

``````for k, v in pairs(m.vertices) do
print(v)
end
``````

It will print all vertices properly. I doubt it’s temporary. Or maybe I understood it incorrectly.

@Saurabh - printing inside the original for loop is fine.

But you want those vertices to be updated at each draw, long after that for loop has finished, and I think all the tweens will have gone too, because the vertices were only valide inside that loop.

@Ignatz I’m still not getting it, why are the vertices valid only in that loop and how are the Tweens gone?

Isn’t m.vertices just like any other table, storing values? Or is it something different?

If it is a normal table then it should work out, because this does

``````function setup()
t = {vec2(WIDTH/2, HEIGHT/2), vec2(100,100)}
for k,v in pairs(t) do
tween(1, v, {x = 0, y = 0})
end
end

function draw()
background()
for k,v in pairs(t) do
ellipse(v.x, v.y, 100)
end
end
``````

Sorry for messing this up so much…

I have also been trying to tween mesh objects for my project. I have been using addRect, storing each rectangle’s ID into a variable, then using setRect to move them. But I can only place them somewhere using a vector, not tween yet.

I think Ignatz is saying that tweens have a life span, and that they will expire quickly. Therefore, you need to call each tween when you need it, or set it to pingpong or loop forever to keep seeing their effects.

Your first code doesn’t reference the x and y values you are tweening in your mesh’s vertices. However, I’ve tried to get this to work and failed too, so very interested to hear a solution from someone.

You’ll have to explicitly reset the mesh vertices each time they are updated by the tween beacause they are buffered by the mesh when you set them (take a look at the mesh:buffer method). Something like this should work (I don’t say that’s a good method he ``````function setup()
m = mesh()
vertices = {vec2(WIDTH/2, HEIGHT/2), vec2(WIDTH/3, HEIGHT/2), vec2(3*WIDTH/5, HEIGHT/3)}
m.vertices = vertices
for _,v in pairs(vertices) do
tween(1, v, {x = 0, y = 0})
end
end

function draw()
background()
m.vertices = vertices
m:draw()
end
``````

Thanks toffer!! Worked well.

@toffer, thanks from me too. Will also experiment with using buffer.get to put the mesh data into a table and tweening that.

Hi,

Had some success with tweening the buffer table. Here is some very rough code, with a bounce version of your first attempt, plus a path version.

``````function setup()
m = mesh()
vertices = {vec2(WIDTH/2, HEIGHT/2), vec2(WIDTH/3, HEIGHT/2), vec2(3*WIDTH/5, HEIGHT/3)}
m.vertices = vertices
tblPosBuff = m:buffer("position"):get()
parameter.action("Animate",Animate)
parameter.action("Reset",Reset)
parameter.action("Path",Path)
end
function draw()
background()
m:buffer("position"):set(tblPosBuff)
m:draw()
end
function Animate()
tween(1,tblPosBuff,{vec3(125,128,0),vec3(0,128,0),vec3(200,0,0)},tween.easing.bounceOut)
end
function Path()
local pth = {}
table.insert(pth,{vec3(WIDTH/2, HEIGHT/2,0), vec3(WIDTH/3, HEIGHT/2,0), vec3(3*WIDTH/5, HEIGHT/3,0)})
table.insert(pth,{vec3(125,128,0),vec3(0,128,0),vec3(200,0,0)})
table.insert(pth,{vec3(125,528,0),vec3(0,528,0),vec3(200,400,0)})
table.insert(pth,{vec3(WIDTH/2, HEIGHT/2,0), vec3(WIDTH/3, HEIGHT/2,0), vec3(3*WIDTH/5, HEIGHT/3,0)})
tween.path(#pth*0.5,tblPosBuff,pth)
end
function Reset()
tblPosBuff = {vec3(WIDTH/2, HEIGHT/2,0), vec3(WIDTH/3, HEIGHT/2,0), vec3(3*WIDTH/5, HEIGHT/3,0)}
end
``````