ΞΜΜΜ̈ˆˆ¬ͺͺͺ¬ˆˆˆ¬ΜΜΜ¬ΜΐΜ¬ΜΐΜ¬ΜΐΜΜΜμΜξͺͺΐΐξΜ¬ΐΜΐ¬ΐΐΐ¬ΐΐΐ¬ΐΐΞΜΜΜ̈ˆˆ¬ͺͺͺ¬ˆˆˆ¬ΜΜΜ¬ΜΜΜ¬ΜΐΜ¬ΜΐΜΜΜμΜξͺͺΐΐξΜ¬ΐΜΜ¬ΐΐΐ¬ΐΐΐ¬ΐΐ¬ΜΜΜ¬ͺͺͺ¬ͺ¬ͺ¬ͺΚΜ¬ͺͺͺŒˆˆˆΜΐΜΞΜΜΞΜ¬Μͺͺΐμ¬ͺΐξͺͺΐξͺͺΐΜξΐμξΜΜξξ¬ΜΜΜ¬ͺͺͺ¬ͺ¬ͺ¬ͺΚΜ¬ͺͺͺŒˆˆˆΜΐΜΞΜΜΞΜ¬Μͺͺΐμ¬ͺΐξͺͺΐξͺͺΐΜξΐμξΜΜξξ >ΐ?333333333333333333333333333333333333333333333333!"33"33""333333333333333333!"333333333333333333333333333333333333333333!333!333333"33333""3!33""3!"33!33""3333""333""333333333333333333333333333333333""333!33"3333"3333!333333333333333333333333333333333333333333333333333333!333!33333333""3!333!!333!3333!33333!333!333333333333333333333333333333333333333!"!3!!"3!!""""!"!!""!""!"""!""!"!""!"!33!!33333333333333333333"""3333333333333333!"33333333333333333333333333333333333333333""3333""!33""3333""333""33333333333333333333333333""333333333333333333333333333333333333333333!333!333!3333!33333!333!33333333333333333333333333!"!""!"!!"""!""!"!""!"!ΑΜΜΜΜΜΜΜΜΜΜΜΜΜΑΜΜΜΜΜΜΜΜΜΜΜΑΜΜΜΜΜΜΜΜΑΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜ ΐΜΜΜΜΜΜΜΜΜΜΑΜΜ̌ˆˆΜΜΜΜΜ ΜΜΜΜΜΜΜΜΜ̈ˆˆˆˆˆΐΜΜΜΜΜΜΜΜΜΜΐ̈ˆˆˆˆˆˆˆˆΘΜΜΜΜΜΑΜ  ͺŠˆˆˆˆˆˆˆˆˆˆΐΜΜΜΑΜ  ͺͺͺͺͺˆˆˆˆˆˆˆˆˆˆΐΜΜΜΜΜͺͺͺͺͺͺͺͺͺˆˆˆˆˆˆˆˆˆˆΜΜΜΜΜͺͺͺͺͺͺͺͺͺͺͺͺŠˆˆˆˆˆˆΘΜΑΜ  ͺͺͺͺͺͺͺͺͺͺͺͺͺͺͺŠˆˆˆΘΜΑΜ ͺͺͺͺͺͺͺΚΜͺͺͺͺͺͺͺͺͺŠˆΜΜΜΜͺͺͺͺͺͺͺΚΜΜΜΜͺͺͺͺͺͺͺͺΜΜΜΜ ͺͺͺͺͺͺͺΜΜΜΜΜ¬ͺͺͺͺͺͺΚΜΑΜΜΜ  ͺͺͺͺͺΜΜΚΜΜΜΜͺͺͺͺͺͺͺΚΜΑΜΜΜͺͺͺͺͺΚΜ¬ͺͺͺͺΜͺͺͺͺͺͺͺΜΜΜΜΜΜͺͺͺͺͺΚΜͺͺͺͺͺΚΜ¬ͺͺͺͺͺΜΜΜΜΜΜ  ͺͺͺͺͺͺͺͺͺͺͺͺΚΜͺͺͺͺͺΚΜΑΜΜΜ  ͺͺͺͺͺͺͺͺͺͺͺͺΜΜͺͺͺͺͺΚΜΑΜΜΜͺͺΜ¬ͺͺͺͺͺͺͺͺͺͺ¬ͺͺͺͺͺΜΜΜΜΐΜͺΚΜΜΜΜΜ¬ͺͺͺͺͺͺͺͺͺͺͺͺΜΜΜΐΜ  ͺΚΜΜΜΜΜΜΜΜͺͺͺͺͺͺͺͺͺΚΜΑΜ ΐΜ ͺͺΜΜΜΜΜΜΜΜΜΜΜ¬ͺͺͺͺͺͺΚΜΑΜ ΜΜͺͺΜΜΜ ΜΜΜΜΜΜΜΜ¬ͺͺͺͺΜΜΜΜΜΜ ͺΚΜΜΜΜΜΜΜΜΜΜΜΜΜ¬ͺͺΜΜ ΐΜ  ͺΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜͺΚΜΑΜ ΐΜ ͺͺΜΜΜ ΐΜΜΜΜΜΜΜΜ¬ͺΚΜΑΜΜΜͺΚΜΜΜ ΐΜΜΜΜΜΐΜΜΜ¬ͺΜΜΜΜΜ  ͺΚΜΜΜΜΜΜΜΜ ΐΜΜΜͺͺΜΜ ΐΜ  ͺΜΜΜΜΜΜΜΜΜ ΐΜΜΜͺΚΜΑΜΜΜΜΜͺͺΜΜΜ ΐΜΜΜΜΜΜΜΜ¬ͺΚΜΑΜΜΜΜΜͺΚΜΜΜ ΐΜΜΜΜΜΜΜΜ¬ͺΜΜΜΜΜΜ  ͺΚΜΜΜΜΜΜΜΜ ΐΜΜΜͺͺΜΑΜ ͺͺΜΜΜΜΜΜΜΜΜΜΜ ΐΜΜΜͺΚΜΑΜͺͺΜΜΜΜΜΜΜΜΜΜΜΜΜΜ¬ͺΚΜΜΜͺŠˆˆΘΜΜΜΜΜΜΜΜΜΜΜ¬ͺΜΜΑΜ  ͺŠˆˆˆˆˆΜΜΜΜΜΜΜΜΜΜΜͺͺΜΑΜ  ͺŠˆˆˆˆˆˆˆΘΜΜΜΜΜΜΜΜͺΚΜΜΜͺͺͺͺˆˆˆˆˆˆˆˆˆΜΜΜΜΜ¬ͺΚΜΜΜͺͺͺͺͺͺŠˆˆˆˆˆˆˆˆΘΜΜ¬ͺΜΜΑΜΜ̌¨ͺͺͺͺͺͺͺͺŠˆˆˆˆˆˆˆˆͺͺΜΑΜΜ̌ˆˆˆͺͺͺͺͺͺͺͺͺˆˆˆˆˆˆͺΚΜΑΜ̈ˆˆˆˆˆ¨ͺͺͺͺͺͺͺͺŠˆˆ¨ͺΚΜΜ̈ˆˆˆˆˆˆˆˆͺͺͺͺͺͺͺͺͺ¨ͺΜΜΜΜΜ̌ˆˆˆˆˆˆˆˆˆͺͺͺͺͺͺͺͺΜΑΜΜΜΜΜΜ̌ˆˆˆˆˆˆˆˆ¨ͺͺͺͺΚΜΜΜΜΜΜΜΜΜ̈ˆˆˆˆˆˆˆˆͺͺΚΜΑΜΜΜΜΜΜΜΜ̈ˆˆˆˆˆΘΜΜΑΜΜΜΜΜΜΜ̌ˆˆˆΜΜΜΜΜΜΜΜΜΜΜ̌ΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΑΜ$-- title: Sprite Rotation -- author: Skeptim with ChatGPT -- desc: Sprite rotation along x,y,z axes. -- site: https://tic80.com/dev?id=6947 -- license: GNU General Public License v3.0 (GPL) -- version: 2.0 -- script: lua -- input: gamepad -- Initialize variables local x, y, z = 120, 68, 0 -- Position local angx, angy, angz = 0, 0, 0 -- Rotation angles -- UV coordinates for each corner local u1, v1 = 8, 0 -- Corner 1, top left local u2, v2 = 24, 0 -- Corner 2, top right local u3, v3 = 24, 16 -- Corner 3, bottom right local u4, v4 = 8, 16 -- Corner 4, bottom left -- Rotation center: can be the center of the sprite or not local u0=(u1+u3)/2 -- U coordinate local v0=(v1+v3)/2 -- V coordinate -- Scale factor local scale = 4 local function sprite_rotation(x, y, z, angx, angy, angz, u1, v1, u2, v2, u3, v3, u4, v4, scale, chromakey, u0, v0) local sin = math.sin local cos = math.cos local z_offset=300 -- to avoid negative z values -- Calculate rotation matrices local sinX, cosX = sin(angx), cos(angx) local sinY, cosY = sin(angy), cos(angy) local sinZ, cosZ = sin(angz), cos(angz) -- Center of the sprite if not u0 or not v0 then u0=(u1+u3)/2 v0=(v1+v3)/2 end -- Define the vertices of the first triangle local vertices1 = { { x = (u1-u0) * scale, y = (v1-v0) * scale, z = 0 }, { x = (u2-u0) * scale, y = (v2-v0) * scale, z = 0 }, { x = (u4-u0) * scale, y = (v4-v0) * scale, z = 0 } } -- Apply rotation matrices to the vertices of the first triangle for _, vertex in ipairs(vertices1) do local x, y, z = vertex.x, vertex.y, vertex.z -- Apply rotation around x-axis local newY = y * cosX - z * sinX local newZ = y * sinX + z * cosX y, z = newY, newZ -- Apply rotation around y-axis local newX = x * cosY + z * sinY newZ = -x * sinY + z * cosY x, z = newX, newZ -- Apply rotation around z-axis newX = x * cosZ - y * sinZ newY = x * sinZ + y * cosZ x, y = newX, newY vertex.x, vertex.y, vertex.z = x, y, z+z_offset end -- Define the vertices of the first triangle local vertices2 = { { x = (u2-u0) * scale, y = (v2-v0) * scale, z = 0 }, { x = (u4-u0) * scale, y = (v4-v0) * scale, z = 0 }, { x = (u3-u0) * scale, y = (v3-v0) * scale, z = 0 } } -- Apply rotation matrices to the vertices of the second triangle for _, vertex in ipairs(vertices2) do local x, y, z = vertex.x, vertex.y, vertex.z -- Apply rotation around x-axis local newY = y * cosX - z * sinX local newZ = y * sinX + z * cosX y, z = newY, newZ -- Apply rotation around y-axis local newX = x * cosY + z * sinY newZ = -x * sinY + z * cosY x, z = newX, newZ -- Apply rotation around z-axis newX = x * cosZ - y * sinZ newY = x * sinZ + y * cosZ x, y = newX, newY vertex.x, vertex.y, vertex.z = x, y, z+z_offset end -- Draw the rotated triangles to form a sprite using ttri local v1_1, v2_1, v3_1 = vertices1[1], vertices1[2], vertices1[3] local v1_2, v2_2, v3_2 = vertices2[1], vertices2[2], vertices2[3] -- Draw first triangle ttri( v1_1.x + x, v1_1.y + y, v2_1.x + x, v2_1.y + y, v3_1.x + x, v3_1.y + y, u1, v1, u2, v2, u4, v4, 0, chromakey, v1_1.z, v2_1.z, v3_1.z ) -- Draw second triangle ttri( v1_2.x + x, v1_2.y + y, v2_2.x + x, v2_2.y + y, v3_2.x + x, v3_2.y + y, u2, v2, u4, v4, u3, v3, 0, chromakey, v1_2.z, v2_2.z, v3_2.z ) end function TIC() cls(1) print("X, Y, Z Rotation",11,11,2,false,2) print("Function",11,31,2,false,2) print("X, Y, Z Rotation",10,10,3,false,2) print("Function",10,30,3,false,2) sprite_rotation(x, y, z, angx, angy, angz, u1, v1, u2, v2, u3, v3, u4, v4, scale, 14, u0, v0) -- Update rotation angles based on input if btn(0) then angx = angx + 0.1 end if btn(1) then angy = angy + 0.1 end if btn(2) then angz = angz + 0.1 end if btn(3) then u0 = u0 + 0.2 end end