57 Commits

Author SHA1 Message Date
FatalErr42O
642739f44b documentation updates, readme update 2024-12-21 18:48:58 -08:00
FatalErr42O
9273dfe583 removed individual documentation in favor of a master site 2024-12-20 23:02:08 -08:00
FatalErr42O
4f3bb3ea5e now runs (with some require hax) 2023-08-29 16:01:42 -07:00
shakesoda
ea79f346f6
Merge pull request #75 from idbrii/precond
Include offending type in precondition failure msg
2022-03-31 06:05:54 -07:00
David Briscoe
be10073156 vec2: Fallback to math.atan
Lua 5.3+ use a two-argument version of math.atan instead of math.atan2.
atan2 exists but is deprecated, so if you use cpml in a lua build that
strips deprecated functions vec2.angle_to will fail.
2022-03-27 09:56:44 -07:00
David Briscoe
c26cc1a508 Include offending type in precondition failure msg
Output the type that was incorrect to make it more obvious what the user
is doing wrong without them adding logging.

To avoid string building during lots of vector math, introduced a
private precond module that only builds the strings when an error
occurs. It uses error() to put the resulting error message at the caller
to cpml.

Before:

  lua: ~/cpml/modules/vec2.lua:52: new: Wrong argument type for x (<number> expected)
  lua: ~/cpml/modules/vec2.lua:424: __add: Wrong argument type for right hand operand. (<cpml.vec2> expected)

  example stack traceback:
  	[C]: in function 'assert'
  	~/cpml/modules/vec2.lua:424: in metamethod '__add'
  	test_cpml.lua:32: in main chunk
  	[C]: in ?

After:

  lua: test_cpml.lua:31: new: Wrong argument type for x: string (<number> expected)
  lua: test_cpml.lua:32: __add: Wrong argument type 'string' for right hand operand. (<cpml.vec2> expected)

  example stack traceback:
  	[C]: in function 'error'
  	~/cpml/modules/_private_precond.lua:13: in function 'modules._private_precond.assert'
  	~/cpml/modules/vec2.lua:425: in metamethod '__add'
  	test_cpml.lua:32: in main chunk
  	[C]: in ?

The tracebacks are longer, but the initial error is at the location of
the mistake and the output includes the input type.
2022-03-27 09:56:15 -07:00
shakesoda
eb5eaf6961
Merge pull request #68 from idbrii/nan
vec: Add nan checking functions
2022-03-27 03:47:08 -07:00
David Briscoe
e245f24ca1 vec: Add more comments to clarify operations
Clarify order and behaviour so you can use these functions without
looking up the meaning of vector arithmetic.
2022-03-26 23:34:38 -07:00
David Briscoe
29954aa60e vec: Add nan checking functions
Add:
* vec2.has_nan + test
* vec3.has_nan + test
* quat.has_nan
* mat4.has_nan
* utils.is_nan + test
2022-03-26 23:33:13 -07:00
David Briscoe
39aee9a421 Fix angle_to to produce signed angle
angle_to was producing the angle from +x to the difference between a,b
which is unexpected. Instead, it should produce the smallest absolute
angle between the two vectors and be signed to indicate the direction of
rotation.

By using the old angle_to implementation and modifying equal() to print
out the failures, you can see the numbers that it was producing before
didn't make much sense:

    right:angle_to(down) = 45.0
    right:angle_to(left) = 0.0
    right:angle_to(up)   = -45.0
    down:angle_to(right) = -135.0
    down:angle_to(left)  = -45.0
    down:angle_to(up)    = -90.0
    left:angle_to(down)  = 135.0
    left:angle_to(up)    = -135.0
    up:angle_to(right)   = 135.0
    up:angle_to(down)    = 90.0
    up:angle_to(left)    = 45.0

Now it produces numbers you'd expect:

    right:angle_to(down) = -90.0
    right:angle_to(left) = 180.0
    right:angle_to(up)   = 90.0
    down:angle_to(right) = 90.0
    down:angle_to(left)  = -90.0
    down:angle_to(up)    = 180.0
    left:angle_to(down)  = 90.0
    left:angle_to(up)    = -90.0
    up:angle_to(right)   = -90.0
    up:angle_to(down)    = 180.0
    up:angle_to(left)    = 90.0

See also https://stackoverflow.com/questions/21483999/using-atan2-to-find-angle-between-two-vectors

Also added tests for angle_between.
2021-06-15 11:56:04 -07:00
mcc
336f416f29 Manual merge PR#53 2020-05-03 12:52:17 -04:00
mcc
f7497b9bf7 Manual merge PR#52 2020-05-03 12:50:42 -04:00
mcclure
d4f7cd1280
Merge pull request #50 from mcclure/master
:round(precision) methods for vec2, vec3, bound2, bound3
2020-05-03 12:37:58 -04:00
mcclure
86f0056397
Merge pull request #47 from mcclure/master-test-failures
Fix failing tests
2019-11-30 17:37:03 -05:00
mcc
185b19d766 vec2.to_vec3(z) creates a vec3 with the given z (or 0) 2019-11-30 11:31:22 -05:00
mcc
7452cc0c6c Simple methods for flipping a vector on exactly 1 axis 2019-11-30 11:25:00 -05:00
mcc
8e65db07ce :round(precision) methods for vec2, vec3, bound2, bound3
Uses utils.lua, which requires moving utils.round to a new _private_utils.lua
2019-11-30 00:03:55 -05:00
mcc
a86935a39b Allow ffi.metatype to fail so that "busted" unit tests work
On Github we run unit tests inside "busted". At the start of each test "busted" does some sort of trick (clearing package.loaded)? Which causes "require" to run again for all lua files. This breaks ffi.metatype with error "cannot change a protected metatable" if it is called twice with a single type name, since this is true global state. To work around this this patch wraps ffi.metatype calls in a xpcall() so that failure is silently ignored.
2019-11-29 21:13:30 -05:00
mcc
b80543c242 Make vec2(vec2(1,2)) and vec3(vec3(1,2,3)) consistent between lua and luajit 2019-11-29 14:44:34 -05:00
mcc
b5beacffc7 component_min(a,b) and component_max(a,b) functions for vector classes 2018-01-26 10:06:01 -05:00
Colby Klein
b122e4d726 fix typo 2017-03-29 05:32:16 -07:00
karai17
b3be2a154a unfactor quat 2016-12-14 20:22:22 -04:00
karai17
8df45859e4 unfactor vec2 and vec3 2016-12-14 03:44:34 -04:00
Colby Klein
d19a5addcb Fix failing tests caused by mul->scale 2016-08-21 20:42:44 -07:00
karai17
799e1607bc Updated to work outside of a sanitized environment. 2016-08-14 03:36:36 -03:00
karai17
20f9625ae2 Fixed vec2 and vec3 mul/div/scale 2016-08-14 01:29:24 -03:00
karai17
52eb8af61d More LDoc tidying 2016-07-24 20:57:59 -03:00
karai17
871b86b4ab Updated LDoc data for vec2 and vec3 2016-07-24 20:26:56 -03:00
karai17
5f7a7f29fa Added quaternion tests
* Fixed an issue with is_quat, is_vec3, is_vec2, and is_mat4 methods
* Fixed a minor issue with local variables being declared too late
2016-07-21 20:04:32 -03:00
karai17
78b34590c5 vec2 and vec3 100% test coverage 2016-07-21 13:52:35 -03:00
karai17
2950352ac8 Updated color module, etc. 2016-07-20 22:17:25 -03:00
karai17
2aa0e7fcb6 Cleaned up internal code to be less verbose 2016-07-20 18:24:54 -03:00
karai17
4f9bc17bf4 Refactored mat4
Did some tidying up on quat, vec2, vec3, and utils
2016-07-19 23:55:21 -03:00
karai17
46f8261ea0 Refactored mat4 (in progress)
Cleaned up vec2, vec3, quat
2016-07-18 06:17:42 -03:00
karai17
add0a71c05 Added tests for vec2 and vec3 2016-07-15 22:07:48 -03:00
Bobbyjoness
ffe3d4b083 Fixed redundant requiring of ffi 2016-01-01 21:58:38 -05:00
Landon Manning
c42a3efc07 Added conversion between polar and cartesian coordinates 2015-12-28 19:26:19 -04:00
Landon Manning
437ac9135e Reimplemented quat*vec3, quat/quat, and quat/number
Fixed a few minor things in vec2 and vec3
2015-12-28 10:15:05 -04:00
Matthew Blanchard
8c9214764e Fixed a small bug in vec2. 2015-12-25 22:19:53 -05:00
Matthew Blanchard
85655e65af Refactored quat and added doc comments. Fixed some small typos and doc comments in vec2. 2015-12-25 20:06:31 -05:00
Matthew Blanchard
c60a324010 Fixed some small holdovers from vec3 remaining in vec2. 2015-12-25 18:44:03 -05:00
Matthew Blanchard
c5d02e0683 Updated vec3 doc comments. Brought vec2 in line with vec3. Doc commented vec2. 2015-12-25 18:35:03 -05:00
Colby Klein
82f336a7f4 Fix broken vec2/vec3 clone. 2015-12-20 22:38:36 -08:00
Landon Manning
870a3c5129 Updated vec2 to match new vec3
Fixed code consistency in vec3 as well.
2015-12-20 06:09:09 -04:00
Landon Manning
cda145629d I'm an idiot
the order in which a and b appear actually matters in add/sub.
2015-12-13 06:44:02 -04:00
Landon Manning
c72efbad3c buncha goodies
* Changed add/sub in vec2/vec3 to allow for integer math
* Fixed a string display error in quat
* Other stuff that @shakesoda probably did
2015-12-13 06:16:57 -04:00
Colby Klein
4a4ad08a35 Add vec2.lerp, tests, and fix some minor bugs.
- Added missing vec2 lerp (same as vec3's)
- Added lerp to vec2 and vec3 metatable
- Added tests for vec2 (incomplete)
- Fixed vec2/number being the same as number/vec2
- Fixed vec2 constructor to match vec3's
2015-09-15 07:05:22 -07:00
Colby Klein
78989682cf Add LDoc documentation. Utils done. 2015-09-11 07:59:05 -07:00
Landon Manning
449bf861d4 Added ability to divide a number over a vector.
Example: `local s = 1/scale`
2015-09-09 23:14:33 -03:00
Colby Klein
9458738e11 Switch vec2*vec2 to per-component, add vec2.dot 2015-09-01 15:06:31 -07:00