function filter(p) { /* Clamp velocity to maxVelocity units/second. Zeroing each component of acceleration keeps us from slamming.*/ var maxVelocity = 5; function sign(val) { if (val > 0) { return 1; } else if (val < 0) { return -1; } else { return 0; } } if (p.velocity) { /* Random near-zero value used as "zero" to prevent two sequential updates from being exactly the same (which would cause them to be ignored) */ if (Math.abs(p.velocity.x) > maxVelocity) { p.velocity.x = sign(p.velocity.x) * maxVelocity; p.acceleration.x = 0; } if (Math.abs(p.velocity.y) > maxVelocity) { p.velocity.y = sign(p.velocity.y) * maxVelocity; p.acceleration.y = 0; } if (Math.abs(p.velocity.z) > maxVelocity) { p.velocity.z = sign(p.velocity.z) * maxVelocity; p.acceleration.z = 0; } } return p; }