npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

simple-game-math

v1.0.2

Published

Simple Math Functions For Game Development

Downloads

8

Readme

Simple Game Math API Reference

A node package for simple math functions and types for game development.

Vector2

A module for Vector2 types and functions.

Interface: Vector2.IVector2

An interface for vectors.

Vector2.IVector2.x : number

X component of vector.

Vector2.IVector2.y : number

Y component of vector.

import { Vector2 } from 'simple-game-math'
const newVector: Vector2.IVector2 = {
  x: 0,
  y: 10
}
import { Vector2 } from 'simple-game-math'

class Vector extends IVector2 {
  constructor(public x: number, public y: number) {}
}

Vector2.distance(a: IVector2, b: IVector2): number

Returns the distance between 2 vectors points.

import { Vector2 } from 'simple-game-math'
const dist =  Vector2.distance({x: 0, y: 0}, {x: 10, y: 0})

console.log(dist) // output: 10

Vector2.dot(a: IVector2, b: IVector2): number

Returns the dot product between 2 vectors.

import { Vector2 } from 'simple-game-math'

console.log(Vector2.dot({x: 1, y: 1}, {x: 10, y: 10})) // output: {x: 10, y: 10}

Vector2.mag(v: IVector2): number

Returns the magnitude of a vector.

import { Vector2 } from 'simple-game-math'

console.log(Vector2.mag({x: 5, y: 10})) //output: 11.180339887498949

Vector2.normalize(v: IVector2): IVector2

Returns a vector with a magnitude of one in the same direction as the input vector.

import { Vector2 } from 'simple-game-math'

console.log(Vector2.normalize({x: 5, y: 10})) // output: {x: 0.44721359549996, y: 0.89442719099992}

Vector2.subtract(b: IVector2, a: IVector2): IVector2

Returns a vector where the components are equal to x = b.x - a.x and y = b.y - a.y.

import { Vector2 } from 'simple-game-math'

console.log(Vector2.subtract({x: 10, y: 15}, {x: 5, y: 5})) //output: {x: 5, y: 10}

Math

A module for providing basic math functions.

Math.clamp(v: number, min: number, max: number): number

Returns the a V clamped between min and max.

import { Math } from 'simple-game-math'

console.log(Math.clamp(20, 0, 10)) //output: 10

Math.moveTowards(current: number, towards: number, maxStep: number): number

Returns a number that is between current and towards but will not exceed towards.

import { Math } from 'simple-game-math'

console.log(Math.moveTowards(0, 10, 5)) // output: 5
console.log(Math.moveTowards(0, 10, 20)) // output: 10
console.log(Math.moveTowards(0, 10, -10)) // output: 0

Math.lerp(start: number, end: number, step: number): number

Linearly interpolates between two numbers

import { Math } from 'simple-game-math'

console.log(Math.lerp(0, 10, 0.5)) // output: 5
console.log(Math.lerp(0, 10, 0.25)) // output: 2.5
console.log(Math.lerp(0, 10, 0.75)) // output: 7.5
console.log(Math.lerp(0, 10, 1)) // output: 10