Log In  


Couple people asked about how I did sprite rotation for josefnpat's collab cart https://www.lexaloffle.com/bbs/?tid=27582
Included is a sprite rotation function, and a way of layering them on top of each other to create an illusion of a 3d sprite.
Everything is commented, hopefully it's clear.
Enjoy, hope this is helpful for someone!

Cart #28035 | 2016-09-05 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
13

(the effect isn't perfect and can be a little unclear if you don't consider it when drawing sprites, if anyone has any more accurate examples of rotation functions I'd like to see!)

13


Very cool effect. Thanks!


neat! and as good as you can get imho.

tried optimizing/rewriting rotsprite, stat(1) went from 0.36 to 0.30 on your cart:

function rotsprite(sprite, posx, posy, angle)
	local scx = sprite%16*8
	local scy = flr(sprite/16)*8
	local cs = cos(angle)
	local sn = sin(angle)
	local cpx=-5*(cs-sn) +4
	local cpy=-5*(sn+cs) +4
	for x=-5,5 do
		local px=cpx
		local py=cpy
		for y=-5,5 do
			if (px>=0 and px<8 and py>=0 and py<8) then
				local col = sget(scx+px, scy+py)
				if (col ~= 0) pset(posx+x, posy+y, col)
			end
			px-=sn
			py+=cs
		end
		cpx+=cs
		cpy+=sn
	end
end


Oh, good to see, cheers!


just found out I had this optimized further but didn't update here:

function layeredsprite(sprite, posx, posy, angle, layers)
	local cs,sn = cos(angle),sin(angle)
	local osx,osy = -5*(cs-sn)+4,-5*(sn+cs)+4
	local spx,spy = 8*(sprite%16),8*flr(sprite/16)
	for layer=1, layers do
		local csx,csy = osx,osy
		for x=-5,5 do
			local sx,sy = csx,csy
			for y=-5,5 do
				if (sx>=0 and sx<8 and sy>=0 and sy<8) then
					local col = sget(spx+sx, spy+sy)
					if (col~=0) pset(posx+x, posy+y, col)
				end
				sx-=sn sy+=cs
			end
			csx+=cs csy+=sn
		end
		spx+=8 --layers on same row pls
		posy-=1
	end
end

merged rotsprite() in layeredsprite(), demo runs at 0.24


Is there a way to zoom these sprites, UB ?


not out of the box dw, also this works on single sprites (8x8).
sizes are hardcoded, but should be 'easily' adapted.
if somebody needs help about that, I can have a look, but I won't right now ;)



[Please log in to post a comment]