Skip to content

Instantly share code, notes, and snippets.

@alannakelly
Created May 3, 2021 16:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alannakelly/53b160359934e5bdbc4076fda74dfd36 to your computer and use it in GitHub Desktop.
Save alannakelly/53b160359934e5bdbc4076fda74dfd36 to your computer and use it in GitHub Desktop.
Demo of how a 3rd person camera works in PICO-8
pico-8 cartridge // http://www.pico-8.com
version 29
__lua__
--------------------------------
-- 3rd person camera demo --
-- --
-- (c) 2021 alanna kelly --
-- --
-- --
--this work is licensed under --
--the terms of the mit --
--license. --
--for a copy, see --
--https://choosealicense.com/l--
--icenses/mit/ --
-- --
--https://alannakelly.ie --
--------------------------------
-- player
player={
x=64,
y=64,
h=0.25
}
-- camera
cam={
rho=30, -- distance to player
theta=.75
}
function _update()
-- arrows move player
if (btn(2)) forward()
if (btn(3)) backward()
if (btn(0)) player.h += 0.01
if (btn(1)) player.h -= 0.01
-- use [s] and [f] to orbit camera
if (btn(0,1)) cam.theta -= .01
if (btn(1,1)) cam.theta += .01
-- use [e] and [d] to change camera distance
if (btn(2,1)) cam.rho -= .5
if (btn(3,1)) cam.rho += .5
end
function forward()
player.x += cos(player.h)
player.y += sin(player.h)
end
function backward()
player.x -= cos(player.h)
player.y -= sin(player.h)
end
function update_cam()
end
function _draw()
cls()
print("3rd person camera demo")
print("⬆️⬇️⬅️➡️ moves player")
print("edsf moves camera")
-- draw player
local px = player.x
local py = player.y
-- draw player
spr(1,px-4,py-4)
-- draw player heading
line(px,py,px+cos(player.h)*10,py+sin(player.h)*10,11)
-- the look at vector
local cvx = cos(cam.theta)
local cvy = sin(cam.theta)
-- polar to world coordinates
local cx = px + (cam.rho * cvx)
local cy = py + (cam.rho * cvy)
-- draw camera
spr(2,cx-4,cy-4)
-- draw camera look at vector
line(cx,cy,cx-cvx*10,cy-cvy*10)
end
__gfx__
00000000000770000007700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000700000011110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700007770000011110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000070707000011110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000000700000011110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700007070000011110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000007070000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000077077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment