#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# tweening rects in a mesh?

edited September 2017 Posts: 121

My failed attempt. Currently, I get the error: error: [string "-- tween.lua..."]:130: attempt to index a nil value (local 'initial')

I took some look at other questions asked (i searched tween mesh) but.. ahh, I didn't really understand. Well, they set the vertices directly. Whereas I'm going more for an approach where I want to make a function that would do, like, mesh:setRect, but it would be mesh:tweenRect... anyway, im giving up and going to bed, since its late. Hopefully this is my last question for a while, im feeling like i ask too much. Yes, thank you guys for always helping me

``````-- Meshtween

-- Use this function to perform your initial setu
local width, height = WIDTH/9, WIDTH/9*1.4
function setup()
print("Hello World!")
test = mesh()
for i = 1,4 do
test:setRectColor(i, 50*i, 200-i*50, 20*i)
end
end

-- works with meshes made entirely of rectangles
-- assumes the width, height are same
-- target should have keys x, y
function tween_rect(time, myMesh, index, target)
local position = myMesh:buffer('position')
local myTable = position:get()
local temp = mesh()
local target = temp:buffer('position')
local targTable = target:get()
table.move(targTable, 1, 6, 6 * index - 5)
local t1, t2, x,y,z, x2,y2,z2 = myTable, targTable
local sub, tar = {},{}
for i = index * 6 - 5, index * 6 do
x,y,z = t1[i].x, t1[i].y, t1[i].z  --do x,y,z because tween wont work with vec3 object
x2,y2,z2 = t2[i].x, t2[1].y, t2[i].z
sub[i] = {x=x,y=y,z=z}
tar[i] = {x=x2,y=y2,z=z2}
tween(time, sub, tar)
t1[i] = vec3(sub[i].x, sub[i].y, sub[i].z)
--t2[i] = vec3(tar[i].x, tar[i].y, tar[i].z)
end
position:set(myTable)
end

-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
test:draw()
tween_rect(1, test, 1, {x=1000, y=700})
-- This sets the line thickness
strokeWidth(5)

end

``````

• Posts: 194

1. Tweening should only occur once only; calling `tween` repeatedly causes glitching.
2. This version works by calling setRect on easing

``````function tween_rect(time, m, i, x,y)
local current = {}
local rx, ry
for index = i, i + 6 do
rx = (rx or 0) + m:vertex(index).x
ry = (ry or 0) + m:vertex(index).y
end
rx = (m:vertex(i+1).x + m:vertex(i+2).x)/2 -- average
ry = (m:vertex(i+4).y + m:vertex(i+6).y)/2
-- rx and ry are center coords

current.x = rx
current.y = ry
current.width = m:vertex(i+2).x - m:vertex(i+1).x
current.height = m:vertex(i).y - m:vertex(i+1).y

tween(1,current,{x=x,y=y},function(a,b,c,d) -- tween w/ custom easing function
m:setRect(i,current.x,current.y,current.width,current.height) -- set every time
return tween.easing.quadInOut(a,b,c,d) -- custom easing (set here)
end)
end
``````
• Posts: 194

My method includes hacking the tween easing functions. Run:

``````print(debug.getinfo(tween.start)["source"])
``````
• Mod
Posts: 7,553

@xThomas I would give you some help, but I'm unsure what your objective is. Looking at your code doesn't help me, but the discussion title gives me the idea that you want to create a mesh containing a rect and you want to do something with it using a tween. If that's the case, what do you want the rect to do. If I'm wrong, could you explain what you want to do. I currently have a class that takes the 6 vertices of a mesh rectangle and moves the 4 corners around using 4 tweens. It basically stretches and squashes (distorts) the mesh image.