Skip to content

Instantly share code, notes, and snippets.

@scambier
Last active July 22, 2022 15:16
Show Gist options
  • Select an option

  • Save scambier/5b0a0ac36cb6c885f67183b9669da3c4 to your computer and use it in GitHub Desktop.

Select an option

Save scambier/5b0a0ac36cb6c885f67183b9669da3c4 to your computer and use it in GitHub Desktop.
--[[
The idea of this tweetcart was to get the "water ripple" effect with a grid of points.
Each point is applied a sinusoidal effect,
depending on its distance from the center, and the elapsed time.
The points go up then down following that sinusoidal,
and a color gradient is applied to get a nice pseudo-3d effect.
You can copy-paste this whole code inside PICO-8 and just run it.
Play with the values to see what changes!
]]
-- Smooth framerate!
_set_fps(60)
-- Blue gradient palette.
-- This command overwrites the default palette, but leaves black as index 0.
pal({7,12,-4,-3,-11,-15,1},1)
-- Set camera position to -64,-64
-- so the origin 0,0 is at the center of the screen
camera(-64,-64)
-- Start of loop
-- This is a goto ::<label>::, where <label> in this case is just an underscore
::_::
-- Clear screen
cls()
i=0
for y=-44,54,4 do
-- The i counter, with "+(i%4)" here,
-- is used to place points in an alternating pattern.
-- Definitely not necessary, but looks better.
i+=2
for x=-64+(i%4),64,4 do
-- a and b are the vertical and horizontal distance between the point and origin,
-- normalized to the range 0-1.
-- a is divided by 80 (instead of 64) to give a more rounded shape
a=x/80
b=y/64
-- Pythagoras, c²=a²+b²
-- We don't sqrt() here, because we don't need the exact distance - saves chars and processing time!
dist=a*a+b*b
-- z is the vertical offset that we apply to each point.
-- The basic idea is to (at least) call sin() with the elapsed time t() and the distance.
-- This way, all points at the same distance from the center will get the same offset,
-- forming concentric circles.
-- From there, we play with the values to get the desired effect.
z = cos(-sin(t()*.15 - dist/6)) * 10
-- Draw the point
pset(
-- The horizontal position of a point never changes.
x,
-- The z*1.2 is to smooth some light ripples between the big waves.
y + z*1.2,
-- The color is also calculated from this offset.
-- Since z varies (roughly) between -9 and 9, (z+10)/18 normalizes it to 0-1.
-- This value is then *6+1 to get a color index between 1 and 7, corresponding to the palette.
((z+10)/18)*6+1
)
end
end
-- Draw the screen
flip()
-- Back at the start of the loop
goto _
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment