Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript collision detection

function checkCollisions(x1, y1, w1, h1, x2, y2, w2, h2){
	if (x1 + w1 >= x2 && x1 + w1 <= x2 + w2 && y1 + h1 >= y2 && y1 + h1 <= y2 + h2) {
		return true;
	} else if (x1 >= x2 && x1 <= x2 + w2 && y1 >= y2 && y1 <= y2 + h2) {
		return true;
	} else {
		return false;
	}
}
Comment

collision javascript

let a = document.querySelector('.Div1')

let b = document.querySelector('.Div2')




setInterval(function(){

  let d1P = a.getBoundingClientRect()

  let d2P = b.getBoundingClientRect()

 let TopLeftCornerBoxA = d1P.x
 let TopRieghtCornerBoxA = d1P.x+d1P.width
 let TopLeftCornerBoxB = d2P.x
 let TopRieghtCornerBoxB = d2P.x+d2P.width


 let TopLeftCornerBoxHeightA = d1P.y
 let BottonLeftCornerBoxHeightA = d1P.y+d1P.height
 let TopLeftCornerBoxHeightB = d2P.y
 let BottonLeftCornerBoxHeightB = d2P.y+d2P.height


  // console.log('///////////////')
  // console.log(TopLeftCornerBoxA)
  // console.log(TopRieghtCornerBoxA)
  // console.log(TopLeftCornerBoxB)
  // console.log(TopRieghtCornerBoxB)

  // console.log('///////////////')
  // console.log(TopLeftCornerBoxHeightA)
  // console.log(BottonLeftCornerBoxHeightA)
  // console.log(TopLeftCornerBoxHeightB)
  // console.log(BottonLeftCornerBoxHeightB)
  // console.log('///////////////')
  
  if(

    TopRieghtCornerBoxA>=TopLeftCornerBoxB && TopLeftCornerBoxA<=TopRieghtCornerBoxB
    BottonLeftCornerBoxHeightA>=TopLeftCornerBoxHeightB && TopLeftCornerBoxHeightA<=BottonLeftCornerBoxHeightB


  ){
    a.style.backgroundColor = "#cf8"
    b.style.backgroundColor = "#cf8"
  }else{
    a.style.backgroundColor = "#red"
    b.style.backgroundColor = "#blue"
  }


},1)
Comment

PREVIOUS NEXT
Code Example
Javascript :: Symbol Methods javascript 
Javascript :: Delete a user in ExpressJS 
Javascript :: rc-notification react 
Javascript :: last index of string in javascript 
Javascript :: key value pair array in javascript 
Javascript :: slice() in javascript 
Javascript :: leafletjs code 
Javascript :: how to use cordova screen shot 
Javascript :: get window url from a browser extension 
Javascript :: area selection on image using javascript 
Javascript :: javascript Display Time Every 3 Second 
Javascript :: vuejs enter phone with country flag 
Javascript :: Shallow copy Objects using Object.prototype.assign method 
Javascript :: change bg-color all class 
Javascript :: java.lang.IllegalArgumentException: Can only download HTTP/HTTPS 
Javascript :: textinput onpress react native 
Javascript :: angular pipe paramerte 
Javascript :: node js simple server 
Javascript :: arrow functions javascript 
Javascript :: fluent validation email address regex 
Javascript :: chrome version 
Javascript :: jquery has class 
Javascript :: good way to check object properties in js 
Javascript :: copy array of object in js 
Javascript :: convert c# to javascript online 
Javascript :: Removing borderline of input in react 
Javascript :: import and export data in mongodb 
Javascript :: check if array does not contain string js 
Javascript :: string sort javascript 
Javascript :: alpine js x-on click not working 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =