Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <canvas id="canvas"></canvas>
              
            
!

CSS

              
                body {
  background: black;
  margin: 0;
}

canvas {
  background: white;
  box-shadow: 1px 1px 1px rgba(0, 0, 0, .2);
}

              
            
!

JS

              
                var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
var anim;
canvas.width  = 600;
canvas.height = 400;

ships = [];
ships.push(new Ship(50, 100, 10, "blue")); // ships[0] is the MOVER
ships.push(new Ship(300, 200, 10, "red")); // ships[1] is static and shoots at [0]

function init(){
	ships[0].setMoveTo(150, 250);
	//ships[0].vx = ships[0].move.vx
	if (ships[1].setTarget(ships[0])){
		anim = setInterval(function(){
				draw()
			}, 30
		);
	} else console.log("no solution");
}

function Ship(x, y, s, color){
	this.x = x;
	this.y = y;
	this.s = s;
	this.color = color;
	this.moveSpeed = 3 // my move speed
	this.intercept; // placeholder for the intercept vector
	this.interceptSpeed = 2// projectile speed
	this.getTargetData = function(){
		return {x: this.x, y: this.y};
	}
	
	this.setMoveTo = function(x, y){
		this.move = new Vector({x: this.x, y: this.y}, {x: x, y: y}, this.moveSpeed);
	}
	
	this.setTarget = function(target){ // get the position of the intercept
		//intercept(shooter, target, projSpeed){
		var inter = getIntercept(this, target, target.move, this.interceptSpeed);
		if (inter){
			this.intercept = new Vector(this, inter, this.interceptSpeed);
			return true;
		} else this.intercept = false; return false;
	}

	this.draw = function(){ // draw myself (for the shooter)
		context.beginPath();
		context.arc(this.x, this.y, 10, 0, 2*Math.PI, false);
		context.closePath();
		context.fillStyle = this.color;
		context.fill();
	}
	
	this.drawIntercept = function(){// draw a projectile, my this.intercept
		if (!this.intercept.isDone()){
			this.intercept.now++;
		}
		context.beginPath();
		context.arc(
					this.x + (this.intercept.vx * this.intercept.now),
					this.y + (this.intercept.vy * this.intercept.now),
					5, 0, 2*Math.PI, false
				);
		context.closePath();
		context.fillStyle = "black";
		context.fill();
	}
	
	this.drawMovement = function(){ // draw myself as the mover
		if (!this.move.isDone()){
			this.move.now++;
		}
		context.beginPath();
		context.arc(
					this.x + (this.move.vx * this.move.now),
					this.y + (this.move.vy * this.move.now ),
					10, 0, 2*Math.PI, false
				);
		context.closePath();
		context.fillStyle = this.color;
		context.fill();
	}
}

function Vector(a, b, s){
	this.a = a; // origin
	this.b = b; // target
	this.s = s;
	this.x; 
	this.y;
	this.now = 0; // current t
	this.end; // max t aka magnitude
	this.vx // velo x
	this.vy // velo y
	
	this.setup = function(){
		this.x = b.x - a.x;
		this.y = b.y - a.y;
		
		var x = Math.pow(this.x, 2);
		var y = Math.pow(this.y, 2);
		var m = x + y; 
		
		this.m = Math.sqrt(m) / this.s
		this.vx = this.x/this.m;
		this.vy = this.y/this.m;
		this.end = this.m;
	};
	
	this.isDone = function(){
		if (this.now >= this.end){
			return true;
		}
	}
	this.setup();
}

function getDist(a, b){
	return Math.sqrt((b.x-a.x)*(b.x-a.x) + (b.y-a.y)*(b.y-a.y));
}

function draw() {	
  context.clearRect(0, 0, canvas.width, canvas.height);
	ships[0].drawMovement();
	ships[1].draw();
	ships[1].drawIntercept();
	//console.log("ding")
	
	if (ships[0].move.isDone() && ships[1].intercept.isDone()){
		clearTimeout(anim)
	}
}

function getIntercept(src, dst, dstV, v){
	var tx = dst.x - src.x;
	var	ty = dst.y - src.y;
	var	tvx = dstV.vx;
	var	tvy = dstV.vy;

	// Get quadratic equation components
	var a = tvx*tvx + tvy*tvy - v*v;
	var b = 2 * (tvx * tx + tvy * ty);
	var c = tx*tx + ty*ty;    

	// Solve quadratic
	var ts = quad(a, b, c); // See quad(), below

	// Find smallest positive solution
	var sol = null;
	if (ts) {
		var t0 = ts[0], t1 = ts[1];
		var t = Math.min(t0, t1);
		if (t < 0) t = Math.max(t0, t1);    
		if (t > 0) {
			sol = {
				x: dst.x + tvx * t,
				y: dst.y + tvy * t
			};
		}
	}
	return sol;
}

function quad(a,b,c) {
	var sol = null;
	if (Math.abs(a) < 1e-6) {
		if (Math.abs(b) < 1e-6) {
			sol = Math.abs(c) < 1e-6 ? [0,0] : null;
		} else {
			sol = [-c/b, -c/b];
		}
	} else {
		var disc = b*b - 4*a*c;
		if (disc >= 0) {
			disc = Math.sqrt(disc);
			a = 2*a;
			sol = [(-b-disc)/a, (-b+disc)/a];
		}
	}
	return sol;
}



init();




//  var dist = Math.sqrt(this.x * this.x + this.y * this.y);
    

              
            
!
999px

Console