[ad_1]
I have a game I’m making in Javascript that will involve combining items by dragging and dropping them onto each other. Think Alchemy or similar.
So far, so good, but I hit a snag while trying to figure out how I can detect collisions between the two entities! I have a (really) basic bounding box test –
function isColliding(a, b) {
if (a.x + a.width < b.x || a.x > b.x + b.width || a.y + a.height < b.y || a.y > b.y + b.height) {
return false;
} else {
return true;
}
}
The trouble I’m having is that in practice, this is fine for detecting the mouse colliding with an object to pick it up, but I’m stuck on how I can, after I’ve grabbed an object, test to see if that object I have grabbed is colliding with any of the multiple other objects that exist in the window. I tried doing –
Core.objectCollision = function() {
if (Core.Mouse.isHolding != null) {
for (i in Core.Objects) {
if (isColliding(Core.Mouse.isHolding, Core.Objects[i])) {
console.log("Colliding with world object!");
}
}
}
}
This function is inside my update loop, but of course the object I’m holding is within my Core.Objects array too, so technically collides with itself.
I really think I’m missing something obvious, but a lot of my searches for help have been returning how to simply detect collision between two already known items, however I need it so any item I grab can be tested against any other item in the view.
Please also note I’m trying to avoid libraries for the time being, I’d rather learn the functionality myself.
Thanks for any help!
[ad_2]