0

Sorry for the title, it seems kind of weird but it's hard to explain. I'll be using JavaScript syntax for this as it's the language I am most familiar with.

For example, let's say I have an array called Boxes that houses 4 objects:

Boxes = [
{x: 1, y: 1, width: 100, height: 10},
{x: 1, y: 1, width: 130, height: 10},
{x: 1, y: 1, width: 150, height: 10},
{x: 1, y: 1, width: 120, height: 10}
]

I can use:

for(var box of Boxes){
    console.log(box)
}

To loop through them. However, this is where my mind goes blank. How exactly do I create a loop that will check the box against themselves?

For example, if these objects were echo'd out in a game, they'd overlap. I would need to loop through them, check them against themselves to find out their new positions, right? Or am I overthinking this?

  • simple if statements ? – WilomGfx Jul 20 '17 at 03:12
  • Possible duplicate of [Object comparison in JavaScript](https://stackoverflow.com/questions/1068834/object-comparison-in-javascript) – r3wt Jul 20 '17 at 03:13
  • 1
    You mean collision detection? Try https://stackoverflow.com/questions/2440377/ – david25272 Jul 20 '17 at 03:18
  • two things before i go: 1. filter an array for duplicates. the algorithm you choose is dependent on your needs. some algorithms may middle out, some may just do a double pass etc. just google filtering arrays for duplicates. the simplest algorithm is probably a single pass on the array, and then a second pass, testing the current item against the rest of the array (minus the item at the same index). if you array is small, you can do it the easy way. if not, opt for a better algorithm. 2. Comparing objects in js https://stackoverflow.com/questions/1068834/object-comparison-in-javascript – r3wt Jul 20 '17 at 03:19
  • Thank you guys. I understand the collision detection code, but I'm still confused on how to check each box to the one adjacent (themselves). To make sure no collisions (or overlapping) boxes are there. If I use just my regular loop I posted, I can't check for overlaps within the objects, because it's far more than 4 permutations right? – John Miller Jul 20 '17 at 03:24

0 Answers0