Skip to content

Instantly share code, notes, and snippets.

@Powersaurus
Last active April 25, 2020 15:14
Show Gist options
  • Select an option

  • Save Powersaurus/a2f14141efef62c32fb9bf8f1f889f6f to your computer and use it in GitHub Desktop.

Select an option

Save Powersaurus/a2f14141efef62c32fb9bf8f1f889f6f to your computer and use it in GitHub Desktop.
PICO-8 tline() perspective demo
pico-8 cartridge // http://www.pico-8.com
version 21
__lua__
-- basic perspective tline demo by @powersaurus
-- player rotation
a=0
-- player screen pos
px=64
py=64
mx=0
my=0
-- toggle map/map-rotated-with-tline
rotated=true
function _update()
if btn(⬆️) then
mx+=cos(a)/8
my+=sin(a)/8
end
if btn(⬇️) then
mx-=cos(a)/8
my-=sin(a)/8
end
if btn(⬅️) then
a+=3/360
elseif btn(➡️) then
a-=3/360
end
if btnp(❎) then
rotated=not rotated
end
if(a<0)a+=1.0
if(a>=1.0)a-=1.0
end
function _draw()
cls(6)
if rotated then
draw_rotated()
else
draw_map_view()
end
print("⬅️ ➡️ rotate view",2,2,7)
print("❎ toggle rotated view",2,10,7)
end
function draw_rotated()
-- loop map outside of 8x8
poke(0x5f38, 8)
poke(0x5f39, 8)
-- player direction
local sty=sin(a)
local stx=cos(a)
-- camera direction
local msy=cos(a)
local msx=-sin(a)
-- not bothering with
-- 64..71 - too noisy
for y=72,128 do
local dist=(128/(2*y-128+1))
--printh(">"..msx.." "..msy)
-- divide by smaller number to zoom in
local dby4=dist/2
local d16=dby4/64
tline(
0,y,127,y,
mx+(-msx+stx)*dby4,
my+(-msy+sty)*dby4,
d16*msx,d16*msy
-- after every pixel *drawn*
-- not texel!!!!
)
end
--printh("===")
-- point in direction of players view
--line(px,128,px,120,7)
end
function draw_map_view()
map(0,0,24,24,10,10)
draw_player()
end
function draw_player()
local stx=-sin(a)
local sty=cos(a)
local psx=-sin(a+90/360)
local psy=cos(a+90/360)
local mmx=mx*8+24
local mmy=my*8+24
-- player location
circfill(mmx,mmy,1,7)
-- player view direction
line(mmx,mmy,mmx+stx*8,mmy+sty*8,7)
end
__gfx__
5ee55eeeeeeeee55ddbbbbbd23b3b33b9922999900000000000000005ee55eeeeeeeee5500000000000000000000000000000000000000000000000000000000
eeeeee55eeeeeeeeddbbdbbb53525235992299220000000000000000eeeeee55eeeeeeee00000000000000000000000000000000000000000000000000000000
eeeee555eeeeeeeebbbbbbbb533b3335999999220000000000000000eeeee555eeeeeeee00000000000000000000000000000000000000000000000000000000
eeee55555ee555eedbbbbbdd43353b21999229990000000000000000eeee55555ee555ee00000000000000000000000000000000000000000000000000000000
eeeee555eee555eedbbddbdd33b22532229229920000000000000000eeeee555eee555ee00000000000000000000000000000000000000000000000000000000
55eeeeeeeeee5eeebbbddbbbb353353329999922000000000000000055eeeeeeeeee5eee00000000000000000000000000000000000000000000000000000000
55eeeeeeeeeeeeeeddbddbbb5213414399999999000000000000000055eeeeeeeeeeeeee00000000000000000000000000000000000000000000000000000000
eeeeeeeeeeeeeeeeddbbbbbd13233533992299990000000000000000eeeeeeeeeeeeeeee00000000000000000000000000000000000000000000000000000000
eeeee55eee55eeee0000000011ddddd1888888220000000000000000eeeee55eee55eeee00000000000000000000000000000000000000000000000000000000
eeeee55ee555eeee00000000ddd111dd822888220000000000000000eeeee55ee555eeee00000000000000000000000000000000000000000000000000000000
e55eeeeee5555eee000000001dd111d1888888880000000000000000e55eeeeee5555eee00000000000000000000000000000000000000000000000000000000
e555eeeee5555eee000000001d111dd1288822820000000000000000e555eeeee5555eee00000000000000000000000000000000000000000000000000000000
e555eeee5555eeee000000001dd11dd1288888820000000000000000e555eeee5555eeee00000000000000000000000000000000000000000000000000000000
eeeeeeeee5555eee00000000d1dddddd822228880000000000000000eeeeeeeee5555eee00000000000000000000000000000000000000000000000000000000
5eeeeeeeeeeeee5500000000111d1d118222288800000000000000005eeeeeeeeeeeee5500000000000000000000000000000000000000000000000000000000
5ee55eeeeeeeee5500000000111d1d118222282200000000000000005ee55eeeeeeeee5500000000000000000000000000000000000000000000000000000000
__map__
070207140704080313130000000d0d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
031817181718180313130000000d0d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
070114040703080313130000000d0d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
041102030418180313130000000d0d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
020814081408030313130000000d0d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
171817181718031313130000000d0d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
030303030303031313130000000d0d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
1313131313131313131300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
1313131313131313131300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0404040404040404040400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment