Skip to content

Instantly share code, notes, and snippets.

@curlup
Last active July 21, 2020 20:04
Show Gist options
  • Save curlup/f2e0b8f356847928945a12beb50078f5 to your computer and use it in GitHub Desktop.
Save curlup/f2e0b8f356847928945a12beb50078f5 to your computer and use it in GitHub Desktop.
no-collision dev fork
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<script type="text/javascript" src="http://mbostock.github.io/d3/talk/20111018/d3/d3.js"></script>
<script type="text/javascript" src="http://mbostock.github.io/d3/talk/20111018/d3/d3.geom.js"></script>
<script type="text/javascript" src="http://mbostock.github.io/d3/talk/20111018/d3/d3.layout.js"></script>
<style type="text/css">
circle {
stroke: #000;
stroke-opacity: .5;
}
.hint {
color: #222;
font-size: 18px;
}
.header{
font-size: 2em;
position: absolute;
}
.header{
font-size: 5em;
}
.footer{
font-size: 2em;
position: absolute;
top: 660px;
left: 760px;
}
.hidden {
display: none;
}
</style>
<title>Collision</title>
</head>
<body>
<div id="body">
<div class="header">
<span style="color:red">No</span> collision
<div class="hint warn">This is a dev version - please go to <a href="http://bl.ocks.org/curlup/raw/7279535/">the current one.</a></div>
<div id="clickcontinue" class="hidden">Click to continue or restart</div>
</div>
<div class="footer">
your current score is: <span id="score">0</span>
<br>
your current record is: <span id="maxscore">0</span><span id="share">.</span>
<div class="hint">reload the page to reset the record</div>
</div>
</div>
<script type="text/javascript">
var w = 1000,
h = 901;
var bigN = 200;
var paused = false;
var nodes = d3.range(2+bigN).map(function() { return {radius: Math.random() * 12 + 4}; });
var infected = 0;
var infColor = "green", defColor = "black", oneColor = "red";
var friction = 0.925, gravity = 0.09;
var rootCharge = initRootCharge = -2000;
var chargeFactor = -250;
var force = d3.layout.force()
.friction(friction)
.gravity(gravity)
.charge(function(d, i) { return d == root ? rootCharge : (i<=infected ? chargeFactor*Math.sqrt(d.radius) : 0 ); })
.nodes(nodes)
.size([w, h]);
var root = nodes[0];
root.radius = 10;
root.fixed = true;
var theOne = nodes[nodes.length-1];
theOne.radius = 10;
force.start();
var svg = d3.select("#body").append("svg:svg")
.attr("width", w)
.attr("height", h);
var score = 0;
var text = d3.select("#score");
var share = d3.select("#share");
var clickcontinue = function() { d3.select("#clickcontinue").classed("hidden", !paused); };
var maxscore = 0;
var maxtext = d3.select("#maxscore");
var twitter_widget = d3.select("#twitter-widget-0");
var needColor = function(d, i) { return (d==theOne) ? oneColor : (i<=infected ? infColor : defColor); };
svg.selectAll("circle")
.data(nodes)
.enter().append("svg:circle")
.attr("r", function(d) { return d.radius ; })
.style("fill", needColor);
var score_inc = true;
var prev_maxscore = 0;
force.on("tick", function(e) {
var q = d3.geom.quadtree(nodes),
i = 0,
n = nodes.length;
score_inc = true;
while (++i < n) {
q.visit(collide(nodes[i]));
}
maxscore = (maxscore>score?maxscore:score) | 0;
if (score_inc)
{
score+=0.02;
infected = (score) | 0;
} else {
if (score > 1) {
paused = true;
clickcontinue();
svg.selectAll("circle")
.style("fill", oneColor);
}
score = 0;
if (prev_maxscore != maxscore && maxscore>2){
share.html("");
share.append("iframe")
.attr("class", "twitter-share-button twitter-tweet-button twitter-count-horizontal")
.attr("src", "https://platform.twitter.com/widgets/tweet_button.1390956745.html#_=1391369095545&amp;count=horizontal&amp;id=twitter-widget-0&amp;lang=en&amp;original_referer=file%3A%2F%2F%2Fhome%2Ftru%2FDesktop%2Fcollision.html&amp;related=tru_pablo&amp;size=m&amp;text=I%20scored%20"+maxscore+"%20in%20Collision%20game&amp;url=file%3A%2F%2F%2Fhome%2Ftru%2FDesktop%2Fcollision.html")
.attr("id", "twitter-widget-0")
.attr("scrolling", "no")
.attr("frameborder", "0")
.attr("allowtransparency", "true")
.attr("title", "Twitter Tweet Button")
.attr("data-twttr-rendered", "true")
.attr("style", "width: 107px; height: 20px;");
}
};
rootCharge = initRootCharge/Math.log10(10+2*infected/3)
text.text((score*10|0)/10);
maxtext.text(maxscore);
prev_maxscore = maxscore;
console.log(rootCharge);
svg.selectAll("circle")
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
.style("fill", needColor);
});
svg.on("mousemove", function() {
force.stop()
var p1 = d3.svg.mouse(this);
root.px = p1[0];
root.py = p1[1];
if (!paused) {
force.start();
force.resume();
}
});
svg.on("click", function() {
paused = !paused;
clickcontinue();
if (!paused) {
force.start();
force.resume();
}
});
function collide(node) {
var r = node.radius + 16,
nx1 = node.x - r,
nx2 = node.x + r,
ny1 = node.y - r,
ny2 = node.y + r;
return function(quad, x1, y1, x2, y2) {
if (quad.point && (quad.point !== node)) {
var x = node.x - quad.point.x,
y = node.y - quad.point.y,
l = Math.sqrt(x * x + y * y),
r = node.radius + quad.point.radius;
if (l < r) {
l = (l - r) / l * .5;
node.x -= x *= l;
node.y -= y *= l;
quad.point.x += x;
quad.point.y += y;
if (node == theOne || quad.point == theOne)
{
score_inc = false;
}
}
}
return x1 > nx2
|| x2 < nx1
|| y1 > ny2
|| y2 < ny1;
};
}
d3.select(self.frameElement).style("height", h + "px");
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment