Skip to content

Instantly share code, notes, and snippets.

@williammalo williammalo/README.md forked from 140bytes/LICENSE.txt
Last active Mar 8, 2018

Embed
What would you like to do?
BoxCollisionCheck

Function to check if 2 objects collide. Very useful for games.

arguments:

a: object 1 x

b: object 1 y

c: object 1 width

d: object 1 height

e: object 2 x

f: object 2 y

g: object 2 width

h: object 2 height

function(a,b,c,d,e,f,g,h){return a<e+g&&a+c>e&&b<f+h&&b+d>f}
function(a,b,c,d,e,f,g,h){return a<e+g&&a+c>e&&b<f+h&&b+d>f}
{
"name": "BoxCollisionCheck",
"description": "function to check if 2 objects collide",
"keywords": [
"collision",
"box",
"game"
]
}
<!DOCTYPE html>
<title>Foo</title>
<div>Expected value: <b>undefined</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
// write a small example that shows off the API for your example
// and tests it in one fell swoop.
var myFunction = function(){ /* the code here should be identical to the entry. */ }
document.getElementById( "ret" ).innerHTML = myFunction()
</script>
@maettig

This comment has been minimized.

Copy link

commented Apr 3, 2012

Not much to say here. Maybe it's possible to replace all && with &. It will return a number instead of a boolean but this doesn't matter in most cases. Another idea is, I'm using objects like { x: 32, y: 64, w: 16, h: 16 } in such games. Would be nice to have a function that accepts two of them.

@williammalo

This comment has been minimized.

Copy link
Owner Author

commented Apr 3, 2012

@maettig quote:"Another idea is, I'm using objects like { x: 32, y: 64, w: 16, h: 16 } in such games. Would be nice to have a function that accepts two of them."
Here it is! :)
function(a,b){return a.x<b.x+b.w&a.x+a.w>b.x&a.y<b.y+b.h&a.y+a.h>b.y}

@maettig

This comment has been minimized.

Copy link

commented Apr 3, 2012

Yea, easy. ;-) What about using the remaining bytes to support mirrored objects (where width or height may be negative)?

@williammalo

This comment has been minimized.

Copy link
Owner Author

commented Apr 3, 2012

@maettig
Would you per chance know a better way of converting to absolute than this?:
(x<0?-x:x)

@maettig

This comment has been minimized.

Copy link

commented Apr 3, 2012

This is the shortest replacement for Math.abs, as far as I know. Shouldn't be a problem. You have plenty of space left. :-)

@atk

This comment has been minimized.

Copy link

commented Apr 5, 2012

I'd like a function to compare two DOM nodes (inside the same offsetParent) instead of those vector boxes:

function(a,b){var h='offset',x=h+'Left',y=h+'Top',w=h+'Width';return a[x]<b[x]+b[w]&a[x]+a[w]>b[x]&a[y]<b[y]+b[h+='Height']&a[y]+a[h]>b[y]}

@williammalo

This comment has been minimized.

Copy link
Owner Author

commented Apr 5, 2012

@atk sweet!

@atk

This comment has been minimized.

Copy link

commented Apr 5, 2012

Thank you, @williammalo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.