The overlap of elements is what can be referred to as collision detection. This is a standard part of most games, when moving page elements to be able to detect the position and calculate if they are overlapping.
This exercise is designed to test and learn more about how to check for overlap of elements that move on the page. It will demonstrate how to set up movement of page elements, and output the values needed to calculate the collision between two page elements.
Set up movement of page elements and track the position exercise :
- Create a function that allows for the creation of page elements as we can then dynamically add elements to the page as needed. Give the function a name of adder() to get the parent element that the new element will be appended to. Create the element using the string value of the element tag. Add the inner HTML content for the element and also add a class using the string value.
- Create 4 page elements, 2 boxes that will be moved, and 2 areas to output the box coordinates and dimensions.
- Create a global game object that can track the animation frame, contains the x,y (horizontal,vertical) position of each box, and that contains the h,w (height and width) of each box. These values then can be used to update the position.
- Set up an object named keyz to track which keyboard keys will be used for element movement. Add 8 key code names as property names and set the values to false. This will be updated once the key is pressed.
- Add event listeners on the document that listen for keydown and keyup events. If the keys from the keyz object pressed match the object property name, set the value to true on press down and false on up. This can now be used for the element movement.
- Create and Launch the mover() function which will handle page element movement on key presses.
- Within the mover() function add conditions to check which keys are true, update the game object x,y for each box accordingly. Also apply a condition to check to ensure that before the movement is allowed that the element is within the boundaries of the main container object element.
- Update the style values of properties left and top for each box with the new game x,y values for each.
- Output the x,y,h,w of each element to the page as they move; these values should change with the new x,y coordinates of the element. These will be used to calculate the collision of the elements.
Collision detection Checker of DOM page elements Part 2
To check if any two elements are overlapping you need both element, x,y height and width values. These can then be used to calculate the corners to see overlap on the horizontal axis and overlap on the vertical axis. If both horizontal and vertical are overlapping then there is a collision occurring between the two elements on the page.
Using getBoundingClientRect() will return the dimension of an element. This method returns an object of values which properties include, bottom, height, left, right, top, width, x, y
These values for the element can then be used in the collision formula to check for overlap.
Moving exercise to check for element overlap:
- create move output areas to show the current stats of the element and the collision values. Update the style of the element if an overlap occurs.
- Create a new element in the center to use within the collision check function.
- Create a new function that requires two parameters, one for each element to check. Get the boundaries of the element.
- Create a function col() which can check the boundaries and return a boolean value if the two elements are overlapping. Use this formula to check for overlap of the elements between the various boxes on the screen.
- Update element styling and output info styling depending on the result of the collision detection.