Skip to content

Commit

Permalink
episode 38. plus it looks like a lot of line endings got changed.
Browse files Browse the repository at this point in the history
  • Loading branch information
bit101 committed Mar 18, 2015
1 parent f6b8211 commit d319f52
Show file tree
Hide file tree
Showing 218 changed files with 12,586 additions and 11,844 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
CodingMath
==========

This repo will house any applicable files from the Coding Math videos.

Site: http://codingmath.com

YouTube: http://www.youtube.com/user/codingmath

Support: http://www.patreon.com/codingmath
CodingMath
==========

This repo will house any applicable files from the Coding Math videos.

Site: http://codingmath.com

YouTube: http://www.youtube.com/user/codingmath

Support: http://www.patreon.com/codingmath
44 changes: 22 additions & 22 deletions application1/index.html
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript" src="vector.js"></script>
<script type="text/javascript" src="particle.js"></script>
<script type="text/javascript" src="utils.js"></script>
<script type="text/javascript" src="main.js"></script>
<style type="text/css">
html, body {
margin: 0px;
}

canvas {
display: block;
}

</style>
</head>
<body>
<canvas id="canvas"></canvas>
</body>
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript" src="vector.js"></script>
<script type="text/javascript" src="particle.js"></script>
<script type="text/javascript" src="utils.js"></script>
<script type="text/javascript" src="main.js"></script>
<style type="text/css">
html, body {
margin: 0px;
}

canvas {
display: block;
}

</style>
</head>
<body>
<canvas id="canvas"></canvas>
</body>
</html>
100 changes: 50 additions & 50 deletions application1/main.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
window.onload = function() {
var canvas = document.getElementById("canvas"),
context = canvas.getContext("2d"),
width = canvas.width = window.innerWidth,
height = canvas.height = window.innerHeight,
gun = {
x: 100,
y: height,
angle: -Math.PI / 4
};

draw();

document.body.addEventListener("mousedown", onMouseDown);

function onMouseDown(event) {
document.body.addEventListener("mousemove", onMouseMove);
document.body.addEventListener("mouseup", onMouseUp);
aimGun(event.clientX, event.clientY);
}

function onMouseMove(event) {
aimGun(event.clientX, event.clientY);
}

function onMouseUp(event) {
document.body.removeEventListener("mousemove", onMouseMove);
document.body.removeEventListener("mouseup", onMouseUp);
aimGun(event.clientX, event.clientY);
}

function aimGun(mouseX, mouseY) {
gun.angle = utils.clamp(Math.atan2(mouseY - gun.y, mouseX - gun.x), -Math.PI / 2, -0.3);
draw();
}

function draw() {
context.clearRect(0, 0, width, height);

context.beginPath();
context.arc(gun.x, gun.y, 24, 0, Math.PI * 2, false);
context.fill();

context.save();
context.translate(gun.x, gun.y);
context.rotate(gun.angle);
context.fillRect(0, -8, 40, 16);
context.restore();
}

window.onload = function() {
var canvas = document.getElementById("canvas"),
context = canvas.getContext("2d"),
width = canvas.width = window.innerWidth,
height = canvas.height = window.innerHeight,
gun = {
x: 100,
y: height,
angle: -Math.PI / 4
};

draw();

document.body.addEventListener("mousedown", onMouseDown);

function onMouseDown(event) {
document.body.addEventListener("mousemove", onMouseMove);
document.body.addEventListener("mouseup", onMouseUp);
aimGun(event.clientX, event.clientY);
}

function onMouseMove(event) {
aimGun(event.clientX, event.clientY);
}

function onMouseUp(event) {
document.body.removeEventListener("mousemove", onMouseMove);
document.body.removeEventListener("mouseup", onMouseUp);
aimGun(event.clientX, event.clientY);
}

function aimGun(mouseX, mouseY) {
gun.angle = utils.clamp(Math.atan2(mouseY - gun.y, mouseX - gun.x), -Math.PI / 2, -0.3);
draw();
}

function draw() {
context.clearRect(0, 0, width, height);

context.beginPath();
context.arc(gun.x, gun.y, 24, 0, Math.PI * 2, false);
context.fill();

context.save();
context.translate(gun.x, gun.y);
context.rotate(gun.angle);
context.fillRect(0, -8, 40, 16);
context.restore();
}

};
94 changes: 47 additions & 47 deletions application1/particle.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
var particle = {
position: null,
velocity: null,
mass: 1,
radius: 0,
bounce: -1,
friction: 1,

create: function(x, y, speed, direction, grav) {
var obj = Object.create(this);
obj.position = vector.create(x, y);
obj.velocity = vector.create(0, 0);
obj.velocity.setLength(speed);
obj.velocity.setAngle(direction);
obj.gravity = vector.create(0, grav || 0);
return obj;
},

accelerate: function(accel) {
this.velocity.addTo(accel);
},

update: function() {
this.velocity.multiplyBy(this.friction);
this.velocity.addTo(this.gravity);
this.position.addTo(this.velocity);
},

angleTo: function(p2) {
return Math.atan2(p2.position.getY() - this.position.getY(), p2.position.getX() - this.position.getX());
},

distanceTo: function(p2) {
var dx = p2.position.getX() - this.position.getX(),
dy = p2.position.getY() - this.position.getY();

return Math.sqrt(dx * dx + dy * dy);
},

gravitateTo: function(p2) {
var grav = vector.create(0, 0),
dist = this.distanceTo(p2);

grav.setLength(p2.mass / (dist * dist));
grav.setAngle(this.angleTo(p2));
this.velocity.addTo(grav);
}
var particle = {
position: null,
velocity: null,
mass: 1,
radius: 0,
bounce: -1,
friction: 1,

create: function(x, y, speed, direction, grav) {
var obj = Object.create(this);
obj.position = vector.create(x, y);
obj.velocity = vector.create(0, 0);
obj.velocity.setLength(speed);
obj.velocity.setAngle(direction);
obj.gravity = vector.create(0, grav || 0);
return obj;
},

accelerate: function(accel) {
this.velocity.addTo(accel);
},

update: function() {
this.velocity.multiplyBy(this.friction);
this.velocity.addTo(this.gravity);
this.position.addTo(this.velocity);
},

angleTo: function(p2) {
return Math.atan2(p2.position.getY() - this.position.getY(), p2.position.getX() - this.position.getX());
},

distanceTo: function(p2) {
var dx = p2.position.getX() - this.position.getX(),
dy = p2.position.getY() - this.position.getY();

return Math.sqrt(dx * dx + dy * dy);
},

gravitateTo: function(p2) {
var grav = vector.create(0, 0),
dist = this.distanceTo(p2);

grav.setLength(p2.mass / (dist * dist));
grav.setAngle(this.angleTo(p2));
this.velocity.addTo(grav);
}
};
32 changes: 16 additions & 16 deletions application1/utils.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
var utils = {
norm: function(value, min, max) {
return (value - min) / (max - min);
},

lerp: function(norm, min, max) {
return (max - min) * norm + min;
},

map: function(value, sourceMin, sourceMax, destMin, destMax) {
return lerp(norm(value, sourceMin, sourceMax), destMin, destMax);
},

clamp: function(value, min, max) {
return Math.min(Math.max(value, min), max);
}
var utils = {
norm: function(value, min, max) {
return (value - min) / (max - min);
},

lerp: function(norm, min, max) {
return (max - min) * norm + min;
},

map: function(value, sourceMin, sourceMax, destMin, destMax) {
return lerp(norm(value, sourceMin, sourceMax), destMin, destMax);
},

clamp: function(value, min, max) {
return Math.min(Math.max(value, min), max);
}
}
Loading

0 comments on commit d319f52

Please sign in to comment.