Home Game Development javascript – Collision detection between draggable object and world objects?

javascript – Collision detection between draggable object and world objects?

0
javascript – Collision detection between draggable object and world objects?

[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]

Previous article Have a Nice Death Review (PS5)
Next article Xbox’s Big 2024 Exclusive Hellblade 2 Looks Intense AF
Hello there! My name is YoleeTeam, and I am thrilled to welcome you to AmazonianGames.com. As the premier destination for all things related to Amazon Games' universe, we are dedicated to providing you with the most exciting and immersive gaming experiences out there. From captivating visuals to exhilarating gameplay, our website is packed with comprehensive insights, updates, and reviews to keep you ahead of the game. Whether you're a seasoned gamer or new to the scene, I am here to guide you through this virtual frontier. If you have any questions or suggestions, feel free to reach out to me at john@yoleesolutions.com. Embark on your Amazon gaming journey today with AmazonianGames.com and let the adventure begin!