[ad_1]
I’ve a recreation I am making in Javascript that can contain combining objects by dragging and dropping them onto one another. Suppose Alchemy or comparable.
Thus far, so good, however I hit a snag whereas making an attempt to determine how I can detect collisions between the 2 entities! I’ve a (actually) fundamental bounding field check –
perform isColliding(a, b) {
if (a.x + a.width < b.x || a.x > b.x + b.width || a.y + a.peak < b.y || a.y > b.y + b.peak) {
return false;
} else {
return true;
}
}
The difficulty I am having is that in observe, that is high-quality for detecting the mouse colliding with an object to choose it up, however I am caught on how I can, after I’ve grabbed an object, check to see if that object I’ve grabbed is colliding with any of the a number of different objects that exist within the window. I attempted doing –
Core.objectCollision = perform() {
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 perform is inside my replace loop, however in fact the article I am holding is inside my Core.Objects array too, so technically collides with itself.
I actually suppose I am lacking one thing apparent, however numerous my searches for assist have been returning merely detect collision between two already recognized objects, nevertheless I would like it so any merchandise I seize might be examined towards another merchandise within the view.
Please additionally observe I am making an attempt to keep away from libraries in the interim, I might fairly be taught the performance myself.
Thanks for any assist!
[ad_2]