Skip to content

Instantly share code, notes, and snippets.

@unstoppablecarl
Last active December 14, 2015 00:18
Show Gist options
  • Save unstoppablecarl/4997402 to your computer and use it in GitHub Desktop.
Save unstoppablecarl/4997402 to your computer and use it in GitHub Desktop.
var distance = this.distanceTo(this.target),
d = this.angleTo(this.target),
steerX = Math.cos(d),
steerY = Math.sin(d),
newX = (this.target.pos.x + distance / this.target.vel.x) - this.pos.x,
newY = (this.target.pos.y + distance / this.target.vel.y) - this.pos.y,
// keep speed 10 or less
speed = Math.min(distance, 30) / 6;
this.vel.x += steerX * speed;
this.vel.y += steerY * speed;
// method 2
this.maxVel= {x: 500, y: 500};
this.speed = 20000;
var centerX = this.pos.x + (this.size.x - (this.size.x / 2)),
centerY = this.pos.y + (this.size.y - (this.size.y / 2)),
targetCenterX = this.target.pos.x + this.target.size.x / 2,
targetCenterY = this.target.pos.y + this.target.size.y / 2,
diffX = creepCenterX - pCenterX,
diffY = creepCenterY - pCenterY,
currSpeed = (diffX * diffX) + (diffY * diffY),
cFact = this.speed / currSpeed;
this.vel.x = Math.sqrt(cFact) * diffX;
this.vel.y = Math.sqrt(cFact) * diffY;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment