I’m trying to add shadows to my game, which are rotated and stretched over the course of the day. I’m trying to do it by using meshes. My basic setup is this one:
This is basically a rectangle. My problem is this: When using sprites, it is possible to rotate them around their center by using transform(x,y) rotate(r) sprite("...", 0, 0). However, I’m not sure how the same thing can be done with meshes. Is there a way to rotate a mesh around its center?
@Elias yes - the built in mesh example shows this using the rotate function. The way you have set up your mesh it will rotate about the bottom left (0,0). You could redefine your mesh to vec2(-32,-32),vec2(32,-32),vec2(-32,32) etc to centre it, or do a translate(-32,-32) prior to the rotation
@West Thanks! I had tried that before but apparently messed it up. @dave1707 The demo helped me a lot! @skar Typically, I use rects. But I’m not sure whether the vertices of that rect can be manipulated afterwards, which is necessary for the sahdows
@Elias i’m not sure either, i do know that the vertices are accessible through the mesh object and you can set them directly, take a look at the Roller Coaster example
please keep us posted on your progress, i’d like shadows in my work too, i’m working on a 2D side scroller but i’d like object cast shadows, not the raycast shadows, attached is what i’m NOT going for
@Elias Heres another example of modifying the vertices. Tap and drag a corner of the triangle.
viewer.mode=FULLSCREEN
function setup()
tab={ {x=WIDTH/2+100,y=HEIGHT/2-100},
{x=WIDTH/2-100,y=HEIGHT/2-100},
{x=WIDTH/2,y=HEIGHT/2}
}
m=mesh()
m.vertices={vec2(tab[1].x,tab[1].y),
vec2(tab[2].x,tab[2].y),
vec2(tab[3].x,tab[3].y)}
m:setColors(255,0,0)
end
function draw()
background(40, 40, 50)
for z=1,3 do
m:vertex(z,tab[z].x,tab[z].y)
end
m:draw()
end
function touched(t)
if t.state==BEGAN then
for z=1,3 do
if math.abs(t.x-tab[z].x)<20 and math.abs(t.y-tab[z].y)<20 then
sel=z
m:setColors(0,255,0)
end
end
end
if t.state==CHANGED and sel~=0 then
for z=1,3 do
if sel==z then
tab[z].x=t.x
tab[z].y=t.y
end
end
end
if t.state==ENDED then
sel=0
m:setColors(255,0,0)
end
end
Or if you want to move and rotate a square, try this. Tap the screen.
viewer.mode=FULLSCREEN
function setup()
x,y,w,h=150,HEIGHT/2,200,200
m=mesh()
m:addRect(x,y,w,h)
m:setColors(255,0,0)
v,r=0,0
end
function draw()
background(40, 40, 50)
x=x+v
r=r+v
m:setRect(1,x,y,200,200,math.rad(r/2))
m:draw()
end
function touched(t)
if t.state==BEGAN then
v=.5
end
end