Skip to content

Instantly share code, notes, and snippets.

@eymiha
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eymiha/771b42c090965a882fe9 to your computer and use it in GitHub Desktop.
Save eymiha/771b42c090965a882fe9 to your computer and use it in GitHub Desktop.
validation for bounding box collision detection
Optimised, can detect a non collision in one compare, minimally
collision.html
--------------
<head>
<title>intersect</title>
</head>
<body>
<h1>Box intersection</h1>
<p>
corner 1: front, top, right
<br/>
corner 2: back, bottom, left
</p>
{{> boxA}}
{{> boxB}}
{{> check}}
</body>
<template name="boxA">
<p>
Box A
<br/>
corner 1 20, 20, 20
<br/>
corner 2 10, 10, 10
</p>
</template>
<template name="boxB">
<p>
Box B
<br/>
corner 1 <input class='value' id="front" value='30'>, <input class='value' id="top" value='30'>, <input class='value' id="right" value='30'>
<br/>
corner 2 <input class='value' id="back" value='25'>, <input class='value' id="bottom" value='25'>, <input class='value' id="left" value='25'>
</p>
</template>
<template name="check">
<p id="result"></p>
</template>
collision.coffee
----------------
if Meteor.isClient
Template.boxB.events
"blur .value": -> Template.check.calculate()
Template.check.calculate = ()->
a_front = 20
a_back = 10
a_left = 10
a_right = 20
a_top = 20
a_bottom = 10
b_front = $("#front").val()
b_back = $("#back").val()
b_left = $("#left").val()
b_right = $("#right").val()
b_top = $("#top").val()
b_bottom = $("#bottom").val()
fb = a_front > b_back
bf = a_back < b_front
rl = a_right > b_left
lr = a_left < b_right
tb = a_top > b_bottom
bt = a_bottom < b_top
intersects = fb && bf && rl && lr && tb && bt
$("#result").text "intersects #{intersects}"
Template.check.rendered = ->
Template.check.calculate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment