Skip to content

Instantly share code, notes, and snippets.

@unstoppablecarl
Last active December 13, 2015 22:08
Show Gist options
  • Save unstoppablecarl/4981906 to your computer and use it in GitHub Desktop.
Save unstoppablecarl/4981906 to your computer and use it in GitHub Desktop.
// also tried this slightly different way behaves different but the hard turn problem remains
var mass = 1,
maxSpeed = 1000,
maxSteer = 200,
distance = this.distanceTo(this.target),
d = this.angleTo(this.target),
steerX = Math.cos(d) * this.speed - this.vel.x,
steerY = Math.sin(d) * this.speed - this.vel.y;
steerX = Math.min(steerX, maxSteer);
steerY = Math.min(steerY, maxSteer);
this.vel.x = this.vel.x + steerX;
this.vel.y = this.vel.y + steerY;
{
speed: 2000,
turningSpeed: .02,
angularDistance: function(a, b) {
var pi2 = Math.PI*2;
var d = (b - a) % pi2;
if (d > Math.PI) d -= pi2;
if (d < -Math.PI) d += pi2;
return d;
},
update: function(){
var targetAngle = this.angleTo(this.target); // get angle in radians
var d = this.angularDistance(this.angle, this.targetAngle);
if (d > 0) {
d = Math.min(d, this.turningSpeed);
} else {
d = Math.max(d, -this.turningSpeed);
}
this.angle += d;
var xf = Math.cos(this.angle)
var yf = Math.sin(this.angle)
this.vel.x = xf * this.speed;
this.vel.y = yf * this.speed;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment