make a design of a 3d smiley face that spins around in pico8
- User
make a design of a 3d smiley face that spins around in pico8
- Assistant
- User
make the yellow dots zig zag when you press keys
- Assistant
make a design of a 3d smiley face that spins around in pico8
make the yellow dots zig zag when you press keys
Creating a 3D spinning smiley face in PICO-8 involves using PICO-8's limited graphics capabilities along with some basic 3D math. Here's a simple outline and code example to get you started.
Setup PICO-8 Environment: Initialize your PICO-8 environment by setting up the basic screen configuration and variables needed for the 3D projection and rotation.
3D Projection: Define functions to convert 3D coordinates into 2D screen space. This is typically done using simple orthogonal or perspective projection formulas.
Smiley Face Model: Define the coordinates for a simple smiley face. You can use circles and lines to represent the face and its features.
Rotation Logic: Implement rotation matrices to spin the smiley face around the desired axis — usually the Y-axis for a spinning effect.
Drawing Loop: Continuously update the smiley's position and redraw it based on its rotated coordinates.
Here's a basic example of how you might code this:
-- PICO-8 3D Spinning Smiley Face
function _init()
smiley = {
{x = 0, y = 1, z = 0}, -- top of the head
{x = 1, y = 0, z = 0}, -- right side
{x = 0, y = -1, z = 0}, -- bottom of the head
{x = -1, y = 0, z = 0} -- left side
-- Add more points for eyes and mouth as needed
}
angle = 0
end
function _update()
angle += 0.01
end
function rotate_point_around_y(point, angle)
local cos_angle = cos(angle)
local sin_angle = sin(angle)
local x = point.x * cos_angle - point.z * sin_angle
local z = point.z * cos_angle + point.x * sin_angle
return {x=x, y=point.y, z=z}
end
function project_point(point)
local perspective_divisor = 4
local projected_x = (point.x / (point.z + perspective_divisor)) * 64 + 64
local projected_y = (point.y / (point.z + perspective_divisor)) * 64 + 64
return {x=projected_x, y=projected_y}
end
function _draw()
cls()
for i, point in pairs(smiley) do
local rotated_point = rotate_point_around_y(point, angle)
local projected_point = project_point(rotated_point)
circfill(projected_point.x, projected_point.y, 3, 10)
end
-- Draw additional elements for eyes and mouth here
end
3D Points: The smiley array holds points that represent parts of the smiley face. You can expand this to include more points for details like eyes and mouth.
Rotation: The rotate_point_around_y function rotates a point around the Y-axis using basic 3D rotation matrix math.
Projection: The project_point function maps 3D coordinates into 2D screen space. This simple perspective projection gives a depth effect.
Coordinates: The drawing loop updates the smiley face's appearance by rotating its points and projecting them onto the screen.
Expand the smiley face details by adding more points and customizing the drawing logic to create eyes and a mouth. Adjust variables and math as needed to suit your visual preferences and PICO-8's capabilities.
To make the yellow dots of the smiley face zig-zag when you press keys in PICO-8, you'll need to implement input handling and modify the drawing logic. We will use the arrow keys to adjust the zig-zag pattern dynamically.
Here's how you can accomplish this:
Below is the updated PICO-8 code with zig-zag logic added:
-- PICO-8 3D Spinning and Zig-Zagging Smiley Face
function _init()
-- Define points: simple head, more points can be added
smiley = {
{x = 0, y = 1, z = 0}, -- top of the head
{x = 1, y = 0, z = 0}, -- right side
{x = 0, y = -1, z = 0}, -- bottom of the head
{x = -1, y = 0, z = 0} -- left side
}
angle = 0
zigzag_offset = 0
end
function _update()
angle += 0.01
-- Adjust zig-zag offset based on arrow key input
if (btn(0)) then zigzag_offset -= 0.05 end -- left
if (btn(1)) then zigzag_offset += 0.05 end -- right
if (btn(2)) then zigzag_offset -= 0.05 end -- up
if (btn(3)) then zigzag_offset += 0.05 end -- down
zigzag_offset = max(-1, min(1, zigzag_offset)) -- clamp the offset to avoid excessive effect
end
function rotate_point_around_y(point, angle)
local cos_angle = cos(angle)
local sin_angle = sin(angle)
local x = point.x * cos_angle - point.z * sin_angle
local z = point.z * cos_angle + point.x * sin_angle
return {x=x, y=point.y, z=z}
end
function project_point(point)
local perspective_divisor = 4
local projected_x = (point.x / (point.z + perspective_divisor)) * 64 + 64
local projected_y = (point.y / (point.z + perspective_divisor)) * 64 + 64
-- Apply zig-zag effect
projected_x += sin(projected_y / 8 + zigzag_offset) * 5
projected_y += sin(projected_x / 8 + zigzag_offset) * 5
return {x=projected_x, y=projected_y}
end
function _draw()
cls()
for i, point in pairs(smiley) do
local rotated_point = rotate_point_around_y(point, angle)
local projected_point = project_point(rotated_point)
circfill(projected_point.x, projected_point.y, 3, 10)
end
-- Draw additional elements for eyes and mouth here
end
Zig-Zag Offset: The zigzag_offset variable is adjusted based on arrow key input. This controls the sin function's phase shift, creating a zig-zag effect.
Input Handling: Use the btn() function to detect when arrow keys are pressed, adjusting the zigzag_offset accordingly.
Clamping: The zigzag_offset is clamped between -1 and 1 to prevent excessive zig-zag motion, which can distort the visuals too much.
Sinusoidal Adjustment: The zig-zag effect is achieved by modifying the projected X and Y positions using a sine wave. The periodic nature of sine allows for a smooth zig-zag pattern.
With this code, pressing the arrow keys will dynamically adjust the zig-zag pattern visible on the PICO-8 screen. You can tweak the parameters, such as the multiplication factor inside the sine function or the offsets, to change the intensity or frequency of the zig-zag effect.