0

I require assistance on hit detection in javascript. I have tried many scripts but they all do not work.
My current code:

function collision($div1, $div2) {
 // nothing, this is where my collision detection needs to be
}

My divs:

<style>
/* CSS */
/* character */
#character {
  position:absolute;
}

/* masses */
#bluemass {
  position:absolute;
}
</style>
<center>
<div class="bluemass" id="bluemass">
    <img src='./pic/bluemass.png' height='35' width='35' />
</div> </center>
<div class="character" id="character">

<img src="./pic/char.png" alt="info" height="90" width="90" />

<br>

<? 

   $v = $_GET["username"]; 

   echo "<div style ='font:21px/21px Courier,tahoma,sans-serif;'>$v</div>";

?>



<?

   echo "<div style ='font:21px/21px Courier,tahoma,sans-serif;'>$mass</div>";

?>
</div>

How would I detect if the div character collides/overlaps with the div bluemass? Thanks in advance, this is my testing site.

Symbios
  • 17
  • 2
  • 6
  • 2
    so have you tried at least something? how do get the reference of your elements in your collision function? what is `$div1` and `$div2`? – malifa Jun 24 '16 at 20:29
  • I don't see how that PHP code (or the divs it creates) relates to your question - if it is not relevant please [edit] your question to remove it. – nnnnnn Jun 24 '16 at 20:31
  • This Stackoverflow question can help you : http://stackoverflow.com/questions/4230029/jquery-javascript-collision-detection – Shai Aharoni Jun 24 '16 at 20:37

1 Answers1

0

You can start with something like:

function collision($div1, $div2) {

 //
 var rect1={
  "top":$div1.offset().top,
  "left":$div1.offset().left,
  "width":$div1.offset().width(),
  "height":$div1.offset().height()
 };
 var rect2={
  "top":$div2.offset().top,
  "left":$div2.offset().left,
  "width":$div2.offset().width(),
  "height":$div2.offset().height()
 };

 // Standard rect-overlap checking code here

}

I wouldn't call this a complete solution because I know you need to additionally account for certain other things, like possibly page margins/padding/etc, but hopefully this will get you started.

omghai_8782
  • 468
  • 4
  • 11
  • Even if the question would be worth answering at this point, how is this supposed to help him? If i would be John Snow and know nothing, it would just raise more questions. – malifa Jun 24 '16 at 20:37