Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- collision detection from Mary Rose Cook
- http://maryrosecook.com/
- colliding() returns true if two passed bodies are colliding. The approach is to test for five situations. If any are true, the bodies are definitely not colliding. If none of them are true, the bodies are colliding.
- b1 is the same body as b2.
- Right of b1 is to the left of the left of b2.
- Bottom of b1 is above the top of b2.
- Left of b1 is to the right of the right of b2.
- Top of b1 is below the bottom of b2.
- */
- var colliding = function(b1, b2) {
- return !(
- b1 === b2 ||
- b1.center.x + b1.size.x / 2 < b2.center.x - b2.size.x / 2 ||
- b1.center.y + b1.size.y / 2 < b2.center.y - b2.size.y / 2 ||
- b1.center.x - b1.size.x / 2 > b2.center.x + b2.size.x / 2 ||
- b1.center.y - b1.size.y / 2 > b2.center.y + b2.size.y / 2
- );
- };
Advertisement
Add Comment
Please, Sign In to add comment