2021-03-26 20:58:21 +00:00
|
|
|
<img src="docs/banner.png">
|
|
|
|
|
2020-03-06 01:44:26 +00:00
|
|
|
# VMath
|
|
|
|
|
2020-11-29 22:55:52 +00:00
|
|
|
`nimble install vmath`
|
|
|
|
|
2021-03-26 20:58:21 +00:00
|
|
|
Your single stop for vector math routines for 2d and 3d graphics.
|
|
|
|
|
2021-05-29 18:24:48 +00:00
|
|
|
* Pure nim no dependencies.
|
|
|
|
* Tries to be similar to GLSL Shader Language but adds extra stuff.
|
|
|
|
* Extensively Benchmarked.
|
|
|
|
* Docs: https://nimdocs.com/treeform/vmath
|
2021-03-26 20:58:21 +00:00
|
|
|
|
2021-05-29 18:24:48 +00:00
|
|
|
## Has vector functions for GLSL tyes:
|
2021-03-26 20:58:21 +00:00
|
|
|
|
|
|
|
Type | Constructor | Description
|
|
|
|
------|-------------| ---------------------------------------------------
|
|
|
|
BVec# | bvec# | a vector of booleans
|
|
|
|
IVec# | ivec# | a vector of signed integers
|
|
|
|
UVec# | uvec# | a vector of unsigned integers
|
|
|
|
Vec# | vec# | a vector of single-precision floating-point numbers
|
|
|
|
DVec# | dvec# | a vector of double-precision floating-point numbers
|
|
|
|
|
2021-05-29 18:24:48 +00:00
|
|
|
You can use these constructors to make them:
|
2021-03-26 20:58:21 +00:00
|
|
|
|
|
|
|
NIM | GLSL | 2 | 3 | 4 | 9 | 16 | 4 |
|
|
|
|
--------|--------|-------|-------|-------|-------|-------|-------|
|
|
|
|
bool | bool | BVec2 | BVec3 | BVec4 | | | |
|
|
|
|
int32 | int | IVec2 | IVec3 | IVec4 | | | |
|
|
|
|
uint32 | uint | UVec2 | UVec3 | UVec4 | | | |
|
|
|
|
float32 | float | Vec2 | Vec3 | Vec4 | Mat3 | Mat4 | Quat |
|
|
|
|
float64 | double | DVec2 | DVec3 | DVec4 | DMat3 | DMat4 | DQuat |
|
|
|
|
|
2021-05-29 18:24:48 +00:00
|
|
|
## 2D & 3D Matrix math
|
2021-03-26 20:58:21 +00:00
|
|
|
|
2021-05-29 18:24:48 +00:00
|
|
|
You can combine and create matrices 2d and 3d matrices by passing 2d or 3d vectors to matrix functions:
|
2019-10-17 14:42:23 +00:00
|
|
|
|
|
|
|
```nim
|
2021-05-29 18:24:48 +00:00
|
|
|
let mat2d = translate(vec2(10, 20)) * rotate(45.toRadians) * scale(vec2(2))
|
|
|
|
let mat3d = translate(vec3(10, 20, 0)) * rotateZ(45.toRadians) * scale(vec3(2))
|
2019-10-17 14:42:23 +00:00
|
|
|
```
|
|
|
|
|
2021-05-29 18:24:48 +00:00
|
|
|
## Almost equal operator
|
2019-10-17 14:42:23 +00:00
|
|
|
|
2021-05-29 18:24:48 +00:00
|
|
|
You can check if float point numbers are close, very useful for tests:
|
2019-10-17 14:42:23 +00:00
|
|
|
```nim
|
2021-05-29 18:24:48 +00:00
|
|
|
1.0 ~= 1.0
|
|
|
|
0.0 ~= 0.0
|
|
|
|
-1.0 ~= -1.0
|
|
|
|
not(0.1 ~= 0.2)
|
|
|
|
not(0.01 ~= 0.02)
|
|
|
|
not(0.001 ~= 0.002)
|
|
|
|
not(0.0001 ~= 0.0002)
|
|
|
|
not(0.00001 ~= 0.00002)
|
2019-10-17 14:42:23 +00:00
|
|
|
```
|
|
|
|
|
2021-05-29 18:24:48 +00:00
|
|
|
## Number functions
|
2019-10-17 14:42:23 +00:00
|
|
|
|
2021-05-29 18:24:48 +00:00
|
|
|
* `between` - Returns true if value is between min and max or equal to them.
|
|
|
|
* `sign` - Returns the sign of a number, -1 or 1.
|
|
|
|
* `quantize` - Makes v be multiple of n. Rounding to integer quantize by 1.0.
|
|
|
|
* `fractional` - Returns fractional part of a number. 3.14 -> 0.14
|
|
|
|
* `lerp` - Interpolates value between a and b.
|
2019-10-17 14:42:23 +00:00
|
|
|
|
2021-05-29 18:24:48 +00:00
|
|
|
## Angle functions
|
2019-10-17 14:42:23 +00:00
|
|
|
|
2021-05-29 18:24:48 +00:00
|
|
|
* `fixAngle` - Make angle be from -PI to PI radians.
|
|
|
|
* `angleBetween` - Angle between angle a and angle b.
|
|
|
|
* `turnAngle` - Move from angle a to angle b with step of v.
|
2019-10-17 14:42:23 +00:00
|
|
|
|
2021-05-29 18:24:48 +00:00
|
|
|
# 0.x.x to 1.0.0 vmath breaking changes:
|
2019-10-17 14:42:23 +00:00
|
|
|
|
2021-05-29 18:24:48 +00:00
|
|
|
* `vec3(v)` no longer works please use `vec3(v.x, v.y, 0)` instead.
|
|
|
|
* `vec3(v, 0)` no longer works please use `vec3(v.x, v.y, 0)` instead.
|
|
|
|
* `2 * v` no longer works due to more vec types please use `v * 2` instead.
|
|
|
|
* `m[15]` no longer works because matrices are now m[x, y].
|
|
|
|
* Concept of 3x3 rotation 3d matrix was removed.
|
|
|
|
* `angleBetween` got renamed to `angle(a, b)`
|
|
|
|
* `scaleMat` got renamed to `scale(v)`
|
|
|
|
* `rotationMat3` got renamed to `rotate(x)`
|