Skip to content

Instantly share code, notes, and snippets.

@andrewdolce
andrewdolce / thrusterutilsai.js
Last active December 14, 2015 06:19
lookahead
// The "optimal" velocity is the velocity needed to bring us to
// the target angle in a single physics step.
var optimalVelocity = targetAngle / dt;
// The "optimal" torque is the amount of torque needed this
// frame to achieve the "optimal" velocity.
var optimalTorque = ( optimalVelocity - angularVelocity ) / ( dt * inverseInertia );
var desiredTorqueSign = nonZeroSign( desiredTorque );
if ( desiredTorqueSign === nonZeroSign(optimalTorque) ) {
// We're traveling in the same direction we want to go, so do we
// need to brake?
var brakeDist = 0;
var wantsBrake = false;
if ( result.ccw === angularVelocity > 0 ) {
var brakeTime = -angularVelocity / ( brakeTorque * inverseInertia );
brakeDist = brakeTime * angularVelocity * 0.5;
wantsBrake = Math.abs(brakeDist) - Math.abs(targetAngle) > -0.01;
}
@andrewdolce
andrewdolce / thrusteraiutils.js
Last active December 14, 2015 06:08
freeze condition
if ( Math.abs(targetAngle) < 0.01 ) {
var freeze = false;
var freezeAccel;
if ( angularVelocity > 0 ) {
freezeAccel = maxCW * inverseInertia;
freeze = angularVelocity + freezeAccel * dt < 0;
} else {
freezeAccel = maxCCW * inverseInertia;
freeze = angularVelocity + freezeAccel * dt > 0;
}
@andrewdolce
andrewdolce / thrusteraiutils.js
Last active December 14, 2015 05:59
choose turn direction
// Target angle is closest via CW.
var brakeTorque, desiredTorque;
if ( targetAngle < 0 ) {
if ( targetAngle < -Math.PI ) {
console.warn( 'targetAngle < -π: targetAngle =', targetAngle );
}
desiredTorque = maxCW;
brakeTorque = maxCCW;
result.ccw = false;
@andrewdolce
andrewdolce / mouselookthrusterai.js
Last active December 14, 2015 05:59
target angle calculation
// Calculate normalized target vector to heading.
var globalHeading = input.faceHeading;
Vec2.setFromValues(
targetDir,
Math.cos( globalHeading ),
Math.sin( globalHeading )
);
// Calculate angle between forward vector and target vector.
Mat2.multVec2( xf.rotation, LOCAL_FORWARD, forward );
@andrewdolce
andrewdolce / mouselookthrusterai.js
Last active December 14, 2015 05:59
calculateThrusterValues() function
MLTS.prototype.calculateThrusterValues = function() {
this.maxForward = 0;
this.maxBackward = 0;
this.maxRight = 0;
this.maxLeft = 0;
this.maxCCW = 0;
this.maxCW = 0;
this.model.thrusters.forEach(function( thruster ) {
if ( thruster.isAttached ) {
@andrewdolce
andrewdolce / physics.js
Created September 19, 2012 12:48
Physics API
// This is a JS component interface.
// It acts as the window to the physics layer. It also emits collision events
// that have been processed / generated by the CollisionEventDispatcher.
function Physics () {
this.physicsId = null;
// would store serializable physics properties like:
// linear velocity
// angular velocity
// anything else needed for backing up physics state
};
@andrewdolce
andrewdolce / jitter.sh
Created August 1, 2012 07:40
ipfw jitter test script
#!/bin/bash
MIN_LAG=25
MAX_LAG=50
(( LAG_RANGE = MAX_LAG - MIN_LAG ))
TIME=600 # total test time (in seconds)
INTERVAL=0.1 # time in between changes in latency (in seconds)
STEPS=6000 # TIME / INTERVAL
@andrewdolce
andrewdolce / ipfw_post_7
Created August 1, 2012 07:37
ipfw less naive loopback
$ sudo ipfw add 100 pipe 1 ip from 127.0.0.1 to 127.0.0.1 in
$ sudo ipfw add 100 allow ip from 127.0.0.1 to 127.0.0.1 out
@andrewdolce
andrewdolce / ipfw_post_6
Created August 1, 2012 07:36
ipfw naive loopback
$ sudo ipfw pipe 1 config delay 100ms
$ sudo ipfw add 100 pipe 1 ip from 127.0.0.1 to 127.0.0.1