CPML - Cirno's Perfect Math Library

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
shakesoda
Citizen
Posts: 78
Joined: Thu Sep 25, 2014 11:57 am
Location: Seattle, WA
Contact:

CPML - Cirno's Perfect Math Library

Post by shakesoda »

It's as easy as 1-2-9!

This post is really late, since it's been around for a couple years already... but better than never!

We needed a math library for LÖVE3D, which covers everything needed for our games, so here it is.

Features:
  • Various geometric intersections (ray-[triangle,sphere,aabb,plane], point-[aabb,triangle,frustum], aabb-[sphere,aabb,obb,frustum], etc)
  • Robust vector modules (vec2, vec3 - originally based on hump's)
  • Quaternions
  • Various useful math & mesh utilities
  • 4x4 matrices
  • Octrees (may be later split into the sister library, CPCL
  • Various color-related functions
  • Uses LuaJIT FFI internally if available for performance
  • Does not depend on love
  • Simplex noise (useful outside of love; inside love simply falls back to love.math.noise)
Repo: https://github.com/excessive/cpml
Docs: https://excessive.github.io/cpml (note: may be incomplete)

Basic Usage:

Code: Select all

local cpml = require "cpml"

-- basic vector manipulation...
local a = cpml.vec3(1, 2, 3)
local b = cpml.vec3(2, 3, 4)
local c = a * b
print(c) -- (+2.000,+6.000,+12.000)

-- you can also access functions using cpml.class.function(a, b)...
print(cpml.vec3.dot(a, b)) -- 20

-- transform a vec3 point into camera space with a matrix...
local camera = cpml.mat4():look_at(cpml.vec3(0, -5, 1.5), cpml.vec3(0, 0, 0))
local pos = m1 * c

-- and much more! check the docs to see what all we've got.
Note: we recently finished a major refactor, so a few things may still be broken.
Last edited by shakesoda on Fri Dec 16, 2016 12:40 am, edited 2 times in total.
excessive ❤ moé (LÖVE3D, CPML, ...). holo on IRC.
User avatar
Karai17
Party member
Posts: 930
Joined: Sun Sep 02, 2012 10:46 pm

Re: CPML - Cirno's Perfect Math Library

Post by Karai17 »

Finally a forum post for our excessively used cpml! We use this SO MUCH in basically every project we do, big or small, and it works pretty great.
STI - An awesome Tiled library
LÖVE3D - A 3D library for LÖVE 0.10+

Dev Blog | GitHub | excessive ❤ moé
User avatar
EntranceJew
Prole
Posts: 31
Joined: Fri Apr 03, 2015 10:02 pm
Location: Saint Petersburg, Florida
Contact:

Re: CPML - Cirno's Perfect Math Library

Post by EntranceJew »

OOOOIIIEEE, HOW DID THIS MATH LIBRARY GET SO FAST? IT'S ALMOST LIKE YOU GOT TINY WIZARDS IN THERE.
sandsmas: A LÖVE Editor
My Libraries: Imgur, Palettes, Music Macros, Timer, Hooks
User avatar
shakesoda
Citizen
Posts: 78
Joined: Thu Sep 25, 2014 11:57 am
Location: Seattle, WA
Contact:

Re: CPML - Cirno's Perfect Math Library

Post by shakesoda »

EntranceJew wrote:OOOOIIIEEE, HOW DID THIS MATH LIBRARY GET SO FAST? IT'S ALMOST LIKE YOU GOT TINY WIZARDS IN THERE.
excellent observation! it's actually got fairies.
excessive ❤ moé (LÖVE3D, CPML, ...). holo on IRC.
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: CPML - Cirno's Perfect Math Library

Post by ivan »

Hi Shakesoda,
Not sure if you're still maintaining this lib, but I have a small suggestion for your "octree" class,
How about keeping a table of references (sort of like reverse lookup):
handles[obj] = node
You just need to update this table whenever an object is inserted/removed.
This would make removal of object from the tree much faster as you don't have to search the tree each time.
User avatar
shakesoda
Citizen
Posts: 78
Joined: Thu Sep 25, 2014 11:57 am
Location: Seattle, WA
Contact:

Re: CPML - Cirno's Perfect Math Library

Post by shakesoda »

ivan: we don't have a lookup because of memory concerns, with LD36 (our heaviest use of the octree) we were running into memory limits...

also, updated CPML to v1.2.9, which reverts the out-variable API changes to be friendlier to use again. Everything else from the old refactor branch was kept. The out variables did help performance, but the usage suffered enough that it was just plain frustrating to use, even many months and two LD's later.
excessive ❤ moé (LÖVE3D, CPML, ...). holo on IRC.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: CPML - Cirno's Perfect Math Library

Post by raidho36 »

Many of your functions return new objects. Consider creating functions that modify objects in place, and use object generating functions as wrappers for those. This way you get functions that don't produce garbage, plus functions that are "fire and forget" style of use.

Code: Select all

local function vector_inplace ( ref, x, y, z )
  ref.x, ref.y, ref.z = x, y, z
end

local function vector ( x, y, z )
   return vector_inplace ( { }, z, y, z )
end
User avatar
shakesoda
Citizen
Posts: 78
Joined: Thu Sep 25, 2014 11:57 am
Location: Seattle, WA
Contact:

Re: CPML - Cirno's Perfect Math Library

Post by shakesoda »

raidho36 wrote:Many of your functions return new objects. Consider creating functions that modify objects in place, and use object generating functions as wrappers for those. This way you get functions that don't produce garbage, plus functions that are "fire and forget" style of use.

Code: Select all

local function vector_inplace ( ref, x, y, z )
  ref.x, ref.y, ref.z = x, y, z
end

local function vector ( x, y, z )
   return vector_inplace ( { }, z, y, z )
end
this is planned for next release, we just wanted the API to be pleasant to use again first.

https://github.com/excessive/cpml/issues/20
excessive ❤ moé (LÖVE3D, CPML, ...). holo on IRC.
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests