1.0.0 • Published 6 years ago

game_functions v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
6 years ago

Heya folks. This is my first NPM lib so bear with me. This is a simple library of functions I usually include with my games, so I figured it might be easier to just deply and publish with NPM rather than laboriously copy and paste everything.

All functions in here focus on 2D game development and most involve vectors and their use.

if you want to use any of these in 3D, you'll need to re-add the z-dimension to the resulting vector.

All functions work with objects, that is to say {x:0,y:0} and the like. I don't beleive in the array format of vectors, but because I love people, I included a function to convert between array and objects. It also works exclusively with radians, but has convenient radian to degree convesion functions.

With that being said, here are the functions.

Functions

Lengthdir(length, direction) turns a polar coordinate, distance and a direction, (direction in radians) to a cartesian coordinate. This is extremely useful for game development for lots of reasons. Suffice to say if you're making games you probably want this.

Angle_difference(angle1, angle2) gives you the shortest angular direction from angle1 to angle2, from -pi to +pi.

lerp_distance(p1, p2, dis) gives a point which is dis units from p1, in the direction of p2 from p1.

lerp(p1, p2, percent) gives a point which is percent (value 0-1) of the way from p1 to p2.

dtr(degrees) converts degrees to radians. Very useful if you don't want to do calculations yourself; I use it to add random values.

rtd(radians) converts radians to degrees. Useless except to give human-readable feedback about a given situation.

point_distance(point1, point2) gives standard euclidean distance from point1 to point2.

manhattan(point1, point2) cheapened distance function. Don't try to compare it to numerical values. I mainly use it for Astar pathfinding.

compare_dis(point1, point2, dis) Cheapened distance function used to compare a numerical value to the distance of two points. Cheaper because it avoids the sqrt function. Use only when speed and optimization are very important.

lerp_1d(x1, x2, percent) returns an x value percent (value 0-1) of the way from x1 to x2. so lerp(1,3,.5) would return 2.

unitify(vector) returns a normalized unit vector of the given vector.

rotate_x(vector, direction) when complete, this will do the matrix multiplications to rotate a vector in the X dimension. Useful for 3D or 2d-to-3d transformations.

ar_to_obj(array) transforms the hated array format of vectors to the amazing and perfect json/object format of vectors

random_about(radius) generates a 1D random value from -val/2 to val/2, with mean 0.