[SOLVED] How rotate mesh vertices?

Anyone here is familiar with behind quad rotation math? Trying to rotate specific quad in mesh, but can’t understand math behind it.

--[[
	       0-1-2  0-2-3
	3---2      2  3---2
	|2 /|     /|  |2 /
	|/ 1|    /1|  |/
	0---1  0---1  0
--]]

-- first
stream[offset + 01], stream[offset + 02], stream[offset + 03] = x,   y,   z
stream[offset + 04], stream[offset + 05], stream[offset + 06] = x+w, y,   z
stream[offset + 07], stream[offset + 08], stream[offset + 09] = x+w, y+h, z
-- second
stream[offset + 10], stream[offset + 11], stream[offset + 12] = x,   y,   z
stream[offset + 13], stream[offset + 14], stream[offset + 15] = x+w, y+h, z
stream[offset + 16], stream[offset + 17], stream[offset + 18] = x,   y+h, z

If you look image some quads are rotated toward right side…

because atlas generator decided to rotate image within atlas…

what rotation are you trying to accomplish exactly

what rotation are you trying to accomplish exactly

unrotate

so, the atlas somehow rotated the sprite a right quarter turn…

why can’t you rotate the sprite in the atlas?

why can’t you rotate the sprite in the atlas?

do you know how rotate image in atlas?

*TES: Oblivion music plays in background

I just bruteforced values…

if is_rotated then
	stream[offset + 01], stream[offset + 02], stream[offset + 03] = x+w, y,   z
	stream[offset + 04], stream[offset + 05], stream[offset + 06] = x+w, y+h, z
	stream[offset + 07], stream[offset + 08], stream[offset + 09] = x,   y+h, z
	---
	stream[offset + 10], stream[offset + 11], stream[offset + 12] = x+w, y,   z
	stream[offset + 13], stream[offset + 14], stream[offset + 15] = x,   y+h, z
	stream[offset + 16], stream[offset + 17], stream[offset + 18] = x,   y,   z
else
	stream[offset + 01], stream[offset + 02], stream[offset + 03] = x,   y,   z
	stream[offset + 04], stream[offset + 05], stream[offset + 06] = x+w, y,   z
	stream[offset + 07], stream[offset + 08], stream[offset + 09] = x+w, y+h, z
	--
	stream[offset + 10], stream[offset + 11], stream[offset + 12] = x,   y,   z
	stream[offset + 13], stream[offset + 14], stream[offset + 15] = x+w, y+h, z
	stream[offset + 16], stream[offset + 17], stream[offset + 18] = x,   y+h, z
end

2 Likes

That is valid way of doing it.

Another would be to use a decomposed 2D matrix as the restrictions make it quite simple:
E.g. like so (x, y) -> (y, -x)
Here’s our code for that defold/engine/gamesys/src/gamesys/components/comp_sprite.cpp at dev · defold/defold · GitHub

4 Likes