使用JavaScript DOM进行圆形之间的碰撞检测

huangapple go评论76阅读模式
英文:

Collision detection between circles using javascript DOM

问题

我正在使用HTML、CSS和JS编写一个非画布游戏,用于检测两个圆之间的碰撞,我使用了两个圆的斜边和半径。

作为 x 变量,我使用了左侧的CSS属性,而对于 y,我使用了 top 的CSS属性(我使用了 % 而不是 px,因为它应该是响应式的)。不知何故,它不起作用,我似乎无法找出问题所在。

food.style.top = random2 + "%";
food.style.left = random + "%";

// 使用毕达哥拉斯定理进行碰撞检测的函数,如果两个圆之间的距离小于它们的半径之和,它们就会发生碰撞
setInterval(function() {
  var playerX = parseInt(window.getComputedStyle(player).getPropertyValue("left"));
  var foodX = parseInt(window.getComputedStyle(food).getPropertyValue("left"));
  var playerY = parseInt(window.getComputedStyle(player).getPropertyValue("top"));
  var foodY = parseInt(window.getComputedStyle(food).getPropertyValue("top"));
  var playerRadius = parseInt(window.getComputedStyle(player).getPropertyValue("width"));
  var foodRadius = parseInt(window.getComputedStyle(food).getPropertyValue("width"));

  var adjacent = Math.abs(playerY - foodY);
  var opposite = Math.abs(playerX - foodX);

  var hypotenuse = Math.sqrt(Math.pow(adjacent, 2) + Math.pow(opposite, 2));
  var sumRadius = playerRadius / 2 + foodRadius / 2;

  if (hypotenuse < sumRadius) {
    food.style.top = random + "%";
    food.style.left = random + "%";
    score = score + 50;
  }
}, 10);
* {
  padding: 0;
  margin: 0;
}

body {
  background-color: antiquewhite;
}

#player {
  width: 10%;
  height: 20%;
  background-color: red;
  position: fixed;
  top: 35%;
  left: 35%;
  border-radius: 50%;
  z-index: 5;
}

#food {
  width: 5%;
  height: 10%;
  background-color: #ffd100;
  position: absolute;
  border-radius: 50%;
  top: 0%;
  left: 0%;
  z-index: -1;
}
<div id="player">
  <!--  <img src="png-clipart-smiley-desktop-happiness-face-smiley-miscellaneous-face-thumbnail.png" id="image"> -->
</div>
<div id="food"></div>

我尝试了一些CSS属性的调整,因为我认为使用 % 可能是问题的原因。

英文:

I am programming a non-canvas game using HTML, CSS, and JS; for the collision detection between two circles I used the hypotenuse and the radii of the two circles.

As x variables I used the left CSS attribute, and for the y I used the top CSS attribute (I used % instead of px because it is supposed to be responsive). Somehow it doesn't work, and I cant seem to figure out what the problem is.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

food.style.top = random2 + &quot;%&quot;;
food.style.left = random + &quot;%&quot;;

// function for collision detection using pythagorean Theorem, if the distance between6 two circles is smaller than
// the sum of their radi than they are collidin
setInterval(function() {
  var playerX = parseInt(window.getComputedStyle(player).getPropertyValue(&quot;left&quot;));
  var foodX = parseInt(window.getComputedStyle(food).getPropertyValue(&quot;left&quot;));
  var playerY = parseInt(window.getComputedStyle(player).getPropertyValue(&quot;top&quot;));
  var foodY = parseInt(window.getComputedStyle(food).getPropertyValue(&quot;top&quot;));
  var playerRadius = parseInt(window.getComputedStyle(player).getPropertyValue(&quot;width&quot;));
  var foodRadius = parseInt(window.getComputedStyle(food).getPropertyValue(&quot;width&quot;));

  var adjacent = Math.abs(playerY - foodY);
  var opposite = Math.abs(playerX - foodX);

  var hypotenuse = Math.sqrt(Math.pow(adjacent, 2) + Math.pow(opposite, 2));
  var sumRadius = playerRadius / 2 + foodRadius / 2;

  if (hypotenuse &lt; sumRadius) {
    food.style.top = random + &quot;%&quot;;
    food.style.left = random + &quot;%&quot;;
    score = score + 50;
  }
}, 10);

<!-- language: lang-css -->

* {
  padding: 0;
  margin: 0;
}

body {
  background-color: antiquewhite;
}

#player {
  width: 10%;
  height: 20%;
  background-color: red;
  position: fixed;
  top: 35%;
  left: 35%;
  border-radius: 50%;
  z-index: 5;
}

#food {
  width: 5%;
  height: 10%;
  background-color: #ffd100;
  position: absolute;
  border-radius: 50%;
  top: 0%;
  left: 0%;
  z-index: -1;
}

<!-- language: lang-html -->

&lt;div id=&quot;player&quot;&gt;
  &lt;!--  &lt;img src=&quot;png-clipart-smiley-desktop-happiness-face-smiley-miscellaneous-face-thumbnail.png&quot; id=&quot;image&quot;&gt; --&gt;
&lt;/div&gt;
&lt;div id=&quot;food&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

I tried playing around with the css attributes since i think the use of the % might be the issue.

答案1

得分: 1

而不是使用computedStyle,您可以使用getBoundingClientRect来查找DOM元素的位置。

我还建议使用transform来定位元素,而不是使用lefttop。您可以使用Math.hypot来直接获取距离。

const circle1 = document.querySelector("#circle1").getBoundingClientRect();
const circle2 = document.querySelector("#circle2").getBoundingClientRect();

const dx = circle1.x - circle2.x;
const dy = circle1.y - circle2.y;
const distance = Math.hypot(dx, dy);

if (distance < 100) {
    console.log("circles are touching");
}
#circle1, #circle2 {
  width: 100px;
  height: 100px;
  position: absolute;
  border-radius: 100%;
  background-color: blue;
}

#circle1 {
  transform: translate(10px, 20px);
}

#circle2 {
  transform: translate(30px, 50px);
}

您可以在以下链接中了解更多信息:Element.getBoundingClientRect

英文:

Instead of computedStyle you can use getBoundingClientRect to find the position of a DOM element.

I also advise to use transform instead of left and top to position an element. You can use Math.hypot to get the distance directly.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const circle1 = document.querySelector(&quot;#circle1&quot;).getBoundingClientRect()
const circle2 = document.querySelector(&quot;#circle2&quot;).getBoundingClientRect()

const dx = circle1.x - circle2.x;
const dy = circle1.y - circle2.y;
const distance = Math.hypot(dx,dy);

if(distance &lt; 100) {
	console.log(&quot;circles are touching&quot;)
}

<!-- language: lang-css -->

#circle1, #circle2 {
  width:100px;
  height:100px;
  position:absolute;
  border-radius:100%;
  background-color:blue;
}

#circle1 {
  transform:translate(10px, 20px);
}
#circle2 {
  transform:translate(30px, 50px);
}

<!-- language: lang-html -->

&lt;div id=&quot;circle1&quot;&gt;

&lt;/div&gt;
&lt;div id=&quot;circle2&quot;&gt;

&lt;/div&gt;

<!-- end snippet -->

https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect

huangapple
  • 本文由 发表于 2023年6月1日 05:03:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76377250.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定