Skip to content

Instantly share code, notes, and snippets.

@ShimShamSam
ShimShamSam / single-pass-tree.js
Created August 22, 2017 16:32
Single-pass tree structure
function createTree(nodes) {
const children = {};
const tree = [];
for(const node of nodes) {
node.children = children[node.id] || (children[node.id] = []);
(node.parent_id ? children[node.parent_id] || (children[node.parent_id] = []) : tree).push(node);
}
return tree;
@ShimShamSam
ShimShamSam / stopSidewaysVelocity.js
Last active April 24, 2024 03:08
Phaser Physics - Stop sideways velocity
/**
* Negate sideways velocity on an object being acted upon by a Phaser physics engine.
* The primary use for this is to simulate vehicle movement by negating "drift" when the vehicle turns.
* @param {Phaser.Sprite} sprite The sprite whose sideways velocity you want to negate
*/
function stopSidewaysVelocity(sprite) {
// Recycle the same object to conserve memory
if(!stopSidewaysVelocity.sideways) {
stopSidewaysVelocity.sideways = {};
}