Browse Source

tools and updates needed for a steering controller

develop
Rob Colbert 2 years ago
parent
commit
319b8b64b2
  1. 9
      README.md
  2. 3
      lib/nice-input.js
  3. 47
      lib/nice-math.js
  4. 62
      lib/nice-vector-2d.js

9
README.md

@ -81,4 +81,11 @@ NiceTween works on dynamic `from` and `to` objects. If either `from` or `to` are
## NiceLog
`NiceLog` is an override of console.log that generates formatted output and can perform other functions. `NiceGame` creates a `NiceLog` for it's own use, your application can create one or more for it's own use, and they can each have their own configuration. `NiceLog` is the reason you see color-highlighted and context-aware messages printed to the browser console while a `NiceGame` is running.
`NiceLog` is an override of console.log that generates formatted output and can perform other functions. `NiceGame` creates a `NiceLog` for it's own use, your application can create one or more for it's own use, and they can each have their own configuration. `NiceLog` is the reason you see color-highlighted and context-aware messages printed to the browser console while a `NiceGame` is running.
# Optional Nice Things
The following are not required, but can make your day nicer.
## Fira Code (font)
Visit https://github.com/tonsky/FiraCode/wiki/VS-Code-Instructions to learn about fonts, font ligatures, why they can make reading code better, and how to install and configure the font on your system for use with your favorite editor.

3
lib/nice-input.js

@ -267,6 +267,9 @@ export class NiceInput {
this.log.info('onGamepadConnected', 'gamepad connected', { gamepad });
this.gamepads.push(gamepad);
this.addGamepadButton('system:start', 9);
this.addGamepadButton('system:back', 8);
const ngsdkEvent = new CustomEvent('ngsdk:gamepad-connected', { detail: { gamepad: event.gamepad } });
document.dispatchEvent(ngsdkEvent);
}

47
lib/nice-math.js

@ -4,13 +4,60 @@
'use strict';
const TWO_PI = Math.PI * 2.0;
export default class NiceMath {
/**
* Generate a random floating point number within a minimum and maximum value.
* @param {Number} min The minimum value to generate
* @param {Number} max The maximum value to generate
* @returns A random number between min and max (inclusive)
*/
static randomRange (min, max) {
return (Math.random() * (max - min)) + min;
}
/**
* Generate a randome whole number (integer) within a minimum and maximum
* range.
* @param {Number} min The minimum value to generate
* @param {Number} max The maximum value to generate
* @returns A random whole number (integer) between min and max (inclusive).
*/
static randomRangeInt (min, max) {
return Math.round(Math.random() * (max - min)) + min;
}
/**
* Normalizes an angle (in radians) to a value between 0 and Math.PI*2.
* @param {Number} angle The angle (in radians) to be normalized
* @returns The normalized angle
*/
static normalizeAngle (angle) {
while (angle < 0) {
angle += TWO_PI;
}
while (angle > TWO_PI) {
angle -= TWO_PI;
}
return angle;
}
static isAngleNear (angle1, angle2, maxDistance) {
if (typeof angle1 !== 'number') {
throw new Error('angle1 is invalid');
}
if (typeof angle2 !== 'number') {
throw new Error('angle2 is invalid');
}
if (typeof maxDistance !== 'number' || maxDistance < 0.0) {
throw new Error('maxDistance is invalid');
}
angle1 = NiceMath.normalizeAngle(angle1);
angle2 = NiceMath.normalizeAngle(angle2);
return Math.abs(angle2 - angle1) <= maxDistance;
}
}

62
lib/nice-vector-2d.js

@ -22,6 +22,18 @@ export default class NiceVector2d {
return new NiceVector2d(this.x, this.y);
}
length ( ) {
return Math.sqrt(this.dot(this));
}
normalize ( ) {
const length = this.length();
if (length === 0) {
return new NiceVector2d(0.0, 0.0);
}
return new NiceVector2d(this.x / length, this.y / length);
}
equals (vector) {
return (this.x === vector.x) && (this.y === vector.y);
}
@ -29,14 +41,62 @@ export default class NiceVector2d {
add (vector) {
this.x += vector.x;
this.y += vector.y;
return this;
}
subtract (vector) {
this.x -= vector.x;
this.y -= vector.y;
return this;
}
dotProduct (vector) {
multiply (value) {
return new NiceVector2d(this.x * value, this.y * value);
}
dot (vector) {
return (this.x * vector.x) + (this.y * vector.y);
}
slope ( ) {
let angle = Math.atan2(this.y, this.x);
if (angle < 0) {
angle += 2 * Math.PI;
}
return angle;
}
cross (v) {
return (this.x * v.y) - (this.y * v.x);
}
angleTo (target) {
let v = target.clone().subtract(this).normalize();
let angle = Math.atan2(v.y, v.x);
if (angle < 0) {
angle += Math.PI * 2;
}
if (angle > (Math.PI * 2)) {
angle -= Math.PI * 2;
}
return angle;
}
rotate (angle) {
return new NiceVector2d(
this.x * Math.cos(angle) - this.y * Math.sin(angle),
this.x * Math.sin(angle) + this.y * Math.cos(angle)
);
}
distanceTo (target) {
const deltaX = target.x - this.x;
const deltaY = target.y - this.y;
return Math.sqrt((deltaX * deltaX) + (deltaY * deltaY));
}
isNear (target, maxDistance) {
return this.distanceTo(target) <= maxDistance;
}
}
Loading…
Cancel
Save