I was wondering if it was possible to make shapes that are not rectangles or circles in Codea. I know you can make triangles useing meshes but what about quadrilaterals, pentagons, hexagons, etc.
All those can be made with triangles
Edit: I think there is a triangulate function that converts a set of vertices into triangles, but I don’t know much about it (or if it exists)
Yes, create a table of points all the way round the outside, then use triangulate to make them into triangles that you can use for a mesh
Thanks, are there any tutorials on how to do this?
@thebombdiggity I’m not sure if you can use this, but it might give you some ideas. Tap the screen to create a triangle, then tap to add more points/shapes.
displayMode(FULLSCREEN)
function setup()
count=0
tab={}
mtab={}
m=mesh()
end
function draw()
background(40, 40, 50)
fill(255)
for a,b in pairs(tab) do
ellipse(b.x,b.y,6)
end
m.vertices=mtab
m:setColors(255,0,0)
m.draw(m)
end
function touched(t)
if t.state==BEGAN then
count = count + 1
table.insert(tab,t)
if count>1 then
table.insert(mtab,(vec2(tab[1].x,tab[1].y)))
table.insert(mtab,(vec2(tab[count-1].x,tab[count-1].y)))
table.insert(mtab,(vec2(tab[count].x,tab[count].y)))
end
end
end
--use varargs to allow for any number of points
--takes a number of vec2's and turns them into a polygon.
function poly(...)
--set up mesn
local m=mesh()
--set up table of arguments
local arg={...}
--only draw if there are more than two arguments(vec2's)
if #arg>2 then
m.vertices=triangulate(arg)
m.setColors(fill()) --Edit: does work. Basically use poly like a 'normal' shape and put fill() before it
--draw the mesh
m:draw()
--notify the user of the lack of vertices
else print("Not enough vertices")
end
end
edit: documented the code. Sorry about that-just copied and pasted from a project and forgot to document.
@TheSolderKing how exactly would I implement this piece of code? It looks very intriguing but I can’t figure out how to run it.
@SkyTheCoder, the best way is to set that function to return a mesh, then just draw that mesh later. That way it only creates it once
function setup()
fill(0, 97, 255, 255)
poly=polygon (vec2(50,50), vec2(100,50), vec2(200,100), vec2(50, 200))
end
function polygon(...)
local m=mesh()
local arg={...}
m.vertices=triangulate(arg)
m:setColors(color(fill()))
return m
end
function draw()
background(27, 27, 54, 255)
poly:draw()
end
Question: this works on Lua 5.2, but will this break under Lua 5.3? Haven’t quite digested the new rules for variable arguments… Edit: @thebombdiggity the above should work and be future proof
I just need to add local arg={...}
?
yes
Thank you @yojimbo2000 for clarifying that. As a follow up question to my original post if I do something like this:
function setup()
fill(0, 97, 255, 255)
pos= vec2(200,0)
poly=polygon (vec2(pos.y,pos.x), vec2(pos.x,pos.y), vec2(pos.y,pos.y))
end
function polygon(...)
local m=mesh()
local arg={...}
m.vertices=triangulate(arg)
m:setColors(color(fill()))
return m
end
function draw()
background(27, 27, 54, 255)
translate(pos.x,pos.y)
tween(3.0,pos,{x = 0,y=500})
print(pos.y)
poly:draw()
end
It gets gets really weird toward the end of the animation. Is there a way to make the mesh move from one point to another without it doing that? I have tried different easing types which have helped but don’t solve the problem
@thebombdiggity You shouldn’t call tween in draw because it’s doing it 60 times a second. You only need to create it once and wait for it to finish if you need to call it again. Try this example.
function setup()
fill(0, 97, 255, 255)
pos= vec2(200,0)
poly=polygon (vec2(pos.y,pos.x), vec2(pos.x,pos.y), vec2(pos.y,pos.y))
createTween()
end
function polygon(...)
local m=mesh()
local arg={...}
m.vertices=triangulate(arg)
m:setColors(color(fill()))
return m
end
function draw()
background(27, 27, 54, 255)
translate(pos.x,pos.y)
poly:draw()
end
function createTween()
pos={x=200,y=0}
tween(3,pos,{x=0,y=500},tween.easing.linear)
end
@thebombdiggity, you’re starting a tween every frame in the draw function, you shouldn’t do that, it keeps starting a new tween, and getting confused with coordinates etc, try calling the tween in setup (after declaring the poly)
edit: darn it, didn’t notice that @dave1707 got it before me xd
Sorry about the memory leak, didn’t notice that. Anyways, the way @dave1707 said works better. I can’t believe I didn’t notice that, sorry again. What are you thinking of using the polygon for? How you use the tween function depends on what you want to do, and I would be glad help you with that even though I’m awful at Tweens
how to text a letter’s outline on screen with the above mesh code
Like this? You don’t even need the above function.
pos=vec2(WIDTH/2,HEIGHT/2)
a = mesh()
vertsA={
vec2(pos.x-5,pos.y+20),
vec2(pos.x+5,pos.y+20),
vec2(pos.x+10,pos.y-10),
vec2(pos.x+7,pos.y-10),
vec2(pos.x+2,pos.y),
vec2(pos.x-2,pos.y),
vec2(pos.x-7,pos.y-10),
vec2(pos.x-10,pos.y-10)
}
a.vertices=triangulate(vertsA)
a:setColors(255,255,255)
b = mesh()
vertsB={
vec2(pos.x-10,pos.y-10),
vec2(pos.x-10,pos.y+10),
vec2(pos.x,pos.y+10),
vec2(pos.x+6,pos.y+6),
vec2(pos.x+2,pos.y),
vec2(pos.x+6,pos.y-6),
vec2(pos.x,pos.y-10)
}
b.vertices=triangulate(vertsB)
b:setColors(255,255,255)
I just did a+b, but you can do more and I might do a class that allows you to do this, if there is enough of a need.