Follow
ReeceGames
Follow
 1

# Vector math for PICO-8!

Have you ever been looking at your code and been like:
"Hmm, i sure do add two numbers to another set of two numbers a lot," then do i have the solution for you!
At the low low price of like 400 tokens, PICO-8 vector math can be yours!
I originally made this to streamline a platformer I may or may not ever finish, but I realized that other people might want this, flagrant waste of tokens or not.

### Features!

-Constructor function: vec2(x,y) will make a vector, vec2(x) copies x twice
-The four basic functions, plus exponetation
-The concentation operator (..) does dot products
-A normalize function

### The code

There are two versions of the whole program. One handles errors more gracefully, while the other has a smaller token footprint

## Required functions:

The metatable depends heavily on these two functions

• Constructor

This function creates and gives the right metatable to the given two numbers, saving a few tokens every time you or the metatable itself wants to make a new metatable

 ```function vec2(x,y) if (not y) y=x return setmetatable({x=x,y=y},vec2mt) end ```
• Argument preperation

This function returns a list of two vectors, either the two vectors passed, or a vector and then a doubles number. This function means you cannot divide a number by a vector, or subtract a vector from a number.

 ```function vecargs(a,b) if(type(a)=="number")a,b=b,a if type(b)=="number" then return {a,vec2(b)} elseif getmetatable(b)==vec2mt then return {a,b} end end ```

### Fault tolerant metatable

 ```vec2mt={ __index={x=0,y=0}, __newindex=function (tble, k, v) if k=='x' or k=='y' then rawset(tble,k,v) else printh("bad key") end end, __unm = function(tble) return vec2(-tble.x,-tble.y) end, __add = function(a,b) local c=vecargs(a,b) a,b=c[1],c[2] return vec2(a.x+b.x,a.y+b.y) end, __mul = function(a,b) local c=vecargs(a,b) a,b=c[1],c[2] return vec2(a.x*b.x,a.y*b.y) end, __div = function(a,b) local c=vecargs(a,b) a,b=c[1],c[2] return vec2(a.x/b.x,a.y/b.y) end, __sub = function(a,b) local c=vecargs(a,b) a,b=c[1],c[2] return a+(-b) end, __pow = function(a,b) local c=vecargs(a,b) a,b=c[1],c[2] return vec2(a.x^b.x,a.y^b.y) end, __concat = function(a,b) local c=vecargs(a,b) a,b=c[1],c[2] return a.x*b.x+a.y*b.y --15 end, } ```

### Fault intolerant metatable

 ```vec2mt={ __unm = function(tble) return vec2(-tble.x,-tble.y) end, __add = function(a,b) local c=vecargs(a,b) a,b=c[1],c[2] return vec2(a.x+b.x,a.y+b.y) end, __mul = function(a,b) local c=vecargs(a,b) a,b=c[1],c[2] return vec2(a.x*b.x,a.y*b.y) end, __div = function(a,b) local c=vecargs(a,b) a,b=c[1],c[2] return vec2(a.x/b.x,a.y/b.y) end, __sub = function(a,b) local c=vecargs(a,b) a,b=c[1],c[2] return a+(-b) end, __pow = function(a,b) local c=vecargs(a,b) a,b=c[1],c[2] return vec2(a.x^b.x,a.y^b.y) end, __concat = function(a,b) local c=vecargs(a,b) a,b=c[1],c[2] return a.x*b.x+a.y*b.y --15 end, }```

### Normalize function

 ```function norm(vec) local power=vec^2 return vec/sqrt(power.x+power.y) end ```

P#73745 2020-03-08 17:37

Cart #rg_tweettweet2-0 | 2019-05-20 | Code ▽ | No License

My entry for the second tweettweetjam
Play the game here.

P#64606 2019-05-20 03:36

# look its karel the robot

Cart #karel_the_robot-0 | 2019-05-01 | Code ▽ | No License

wow karel is an intro-duction to programming that ueses phive whole instructions.
karel can move(), which makes it go forewarde
karel can turnleft() whchic should be obvious
karel may dropchip() thath puts a chip on the ground?
karel can takechip() and that picks up any chips that are on the same tile as it
karel is capable of checkchip() whitch returns the chipcode of the chip its on

okay if you want human redable instruction go to this link

P#64137 2019-05-02 03:55

 2

Cart #rg_snowytweet-0 | 2018-12-23 | Code ▽ | License: CC4-BY-NC-SA
2

I was getting a little bored of my main project, so i took a detour and made a tweetcart!
All it has are 2 little houses on a snowy hill, but it looks pretty nice in my opinion.

P#60279 2018-12-23 02:37

# Release!

Cart #phagocytosis_reecegames-0 | 2018-12-02 | Code ▽ | No License

It's done! I might still add on to it, but it's a finished game now!

## How to play

This is you:
 [16x16]

Move with the directions, and start digesting with [O]
Moving and digesting take up energy, and if you run out of energy, you take massive damage to the health meter

# Enemies

Viruses
 [8x8]

These simply drift across the screen, and deal damage if they go off of it.
Touch them while digesting to defeat them

Green Bacteria
 [8x8]

These spawn where you see this symbol:
 [16x16]

They slowly drift away from you.

Yellow Bacteria
 [8x8]

They have a short healthbar that you have to deplete, but are otherwise just like viruses.

Allergens
 [8x8]

Drift across the screen and hurt you if you try to digest them

# Powerups

Multiplier

Red and white pill (antibiotic)
Prevents both kinds of bacteria from spawning

Yellow bottle (allergy pen)
Allergens will not hurt you

Beige thing (platelet)
Halves spawn rate of all enemies

Green arrows (abstract green arrows)
Makes you accelerate faster

## Beta 2

Cart #59291 | 2018-11-23 | Code ▽ | No License

--Changes
-Title Screen!
-Better Music
-Difficulty Curve
-New enemy
-Bacteria
-Can run away from you

## Beta 1

Cart #58901 | 2018-11-09 | Code ▽ | No License

This is my first playable game, very simple right now.
Gameplay
Move with the arrows. Viruses spawn on occasion, if they reach the other side of the screen, the human you're in takes damage. Use (O) to enter phagocytosis. If you touch a virus in phagocytosis mode, you will kill it.
But be careful! Both moving and phagocytosis deplete energy. If you run out of energy, the human rapidly loses health. If the human runs out of health, you lose! Eat food to gain back energy.

P#58902 2018-11-09 17:22 ( Edited 2018-12-02 23:16)