英文:
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 + "%";
food.style.left = random + "%";
// 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("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);
<!-- 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 -->
<div id="player">
<!-- <img src="png-clipart-smiley-desktop-happiness-face-smiley-miscellaneous-face-thumbnail.png" id="image"> -->
</div>
<div id="food"></div>
<!-- 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
来定位元素,而不是使用left
和top
。您可以使用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("#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")
}
<!-- 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 -->
<div id="circle1">
</div>
<div id="circle2">
</div>
<!-- end snippet -->
https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论