Ask coding questions

← Back to all posts
Can Someone Help Me Detect Div Collision?
h
JWZ6 (711)

I'm working on this dodging game. Can someone please help me with detecting if you are dead/hit?

div collision

thanks, JWZ6

Answered by ch1ck3n (2341) [earned 5 cycles]
View Answer
Comments
hotnewtop
ch1ck3n (2341)

just check if the top and left properties of the two elements are in range that they are colliding

so in your case if pos_x and pos_y are same enough as pos_x_2 and pos_y_2 that means its collided

ch1ck3n (2341)

@JWZ6 ping me when its finished i wanna see the finished product :P

JWZ6 (711)

@ch1ck3n hol up can i invite u

ch1ck3n (2341)

@JWZ6

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>Arrow Key Test</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  </head>
  <body>
    <div id="circle"><span>Move Me!</span></div>
    <div id="boom"></div>
    <img src="" width="100px" id="run">
    <script src="script.js"></script>
  </body>
</html>