# Rotate mesh around its center

Hey everyone,

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:

shadow.vertices = {vec2(0, 0), vec2(64, 0), vec2(0, 64), vec2(0, 64), vec2(0, 64), vec2(64, 64)}

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

Just in case anyone wants a demo.

``````viewer.mode=FULLSCREEN

function setup()
rot=0
size=250
ms=mesh()
ms.vertices = {vec2(-size,-size), vec2(size, -size), vec2(-size,size),
vec2(-size,size), vec2(size,size), vec2(size,-size)}
ms:setColors(255,255,255)
ms:color(1,255,0,0)
ms:color(2,0,255,0)
ms:color(3,0,0,255)
ms:color(4,0,0,255)
ms:color(5,255,0,0)
ms:color(6,0,255,0)
end

function draw()
background(0, 255, 223)
rot=rot+1
translate(WIDTH/2,HEIGHT/2)
rotate(rot)
ms:draw()
end
``````

if you want to avoid using style transformation functions in the draw, you can use the setRect function of the mesh

`mesh:setRect(id, x, y, w, h, r)`

the `r` is rotation angle

Thanks to all of you!

@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 Here’s an example of modifying the vertices of a mesh. Try changing the sliders.

``````viewer.mode=STANDARD

function setup()
parameter.integer("v1",1,5,1)
parameter.integer("v2",1,5,1)
parameter.integer("v3",1,5,1)
x1,y1=100,100
x2,y2=200,100
x3,y3=150,200
m=mesh()
m.vertices={vec2(x1,y1),vec2(x2,y2),vec2(x3,y3)}
m:setColors(255,0,0)
end

function draw()
background(40, 40, 50)
m:vertex(1,x1*v1,y1*v1)
m:vertex(2,x2*v2,y2*v2)
m:vertex(3,x3*v3,y3*v3)
m:draw()
end
``````

@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.x,tab.y),
vec2(tab.x,tab.y),
vec2(tab.x,tab.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:setColors(255,0,0)
v,r=0,0
end

function draw()
background(40, 40, 50)
x=x+v
r=r+v