Let's go through the process of creating a wireframe sphere that can be rotated using the arrow keys and has a glitch effect that changes the sphere's color. We'll address the 3D projection and controls more closely to fit within Pico-8's capabilities. Here's how you can implement this in a more refined and tested manner:
-- Constants
local screen_w, screen_h = 128, 128
local half_screen_w, half_screen_h = screen_w / 2, screen_h / 2
local sphere_radius = 30
local num_points = 16
local color_normal = 11
local color_glitch1 = 12
local color_glitch2 = 13
-- Rotation angles
local angle_x, angle_y = 0, 0
-- Glitch effect toggle
local glitch = false
-- Initialize 3D points
local points3d = {}
function init_sphere()
for i = 0, num_points do
points3d[i] = {}
local theta = i * 2 * 3.1415 / num_points
for j = 0, num_points do
local phi = j * 3.1415 / num_points
local x = sphere_radius * cos(theta) * sin(phi)
local y = sphere_radius * sin(theta) * sin(phi)
local z = sphere_radius * cos(phi)
points3d[i][j] = {x, y, z}
end
end
end
function rotate_3d(px, py, pz)
-- Y rotation
local x_z = px * cos(angle_y) - pz * sin(angle_y)
local z_y = px * sin(angle_y) + pz * cos(angle_y)
-- X rotation
local y_x = py * cos(angle_x) - z_y * sin(angle_x)
local z_x = py * sin(angle_x) + z_y * cos(angle_x)
return x_z, y_x, z_x
end
function project_2d(px, py, pz)
local scale = 1 / (pz / 64 + 1)
local x2d = px * scale + half_screen_w
local y2d = py * scale + half_screen_h
return x2d, y2d
end
function _init()
init_sphere()
end
function _update()
-- Update rotation based on button input
if btn(0) then angle_y -= 0.05 end -- left
if btn(1) then angle_y += 0.05 end -- right
if btn(2) then angle_x -= 0.05 end -- up
if btn(3) then angle_x += 0.05 end -- down
-- Toggle glitch effect on button press (e.g., button "O")
if btnp(4) then
glitch = not glitch
end
end
function _draw()
cls(0)
-- Draw wireframe sphere
for i = 0, num_points do
for j = 0, num_points do
local p1 = points3d[i][j]
local p2 = points3d[(i + 1) % num_points][j]
local x1, y1, z1 = unpack(p1)
local x2, y2, z2 = unpack(p2)
x1, y1, z1 = rotate_3d(x1, y1, z1)
x2, y2, z2 = rotate_3d(x2, y2, z2)
local s1x, s1y = project_2d(x1, y1, z1)
local s2x, s2y = project_2d(x2, y2, z2)
-- Select color based on glitch state
local line_color = glitch and (rnd() < 0.5 and color_glitch1 or color_glitch2) or color_normal
line(s1x, s1y, s2x, s2y, line_color)
end
end
end
Overview
- Initialization: The sphere is initialized with a set of 3D points using spherical coordinates.
- Rotation: Points are rotated around the X and Y axes based on input.
- Projection: The 3D points are projected onto the 2D screen using a simple perspective projection.
- Control: Arrow keys rotate the sphere, and the "O" button toggles a glitch effect.
- Glitch Effect: Alternates between two colors when the glitch effect is enabled.
To run this code, paste it into a new Pico-8 cartridge. You can change the button mappings as needed to fit the Pico-8 controls you prefer. This script provides a more interactive and visually accurate wireframe sphere within the constraints of Pico-8.