gpt4 book ai didi

javascript - 使用 JQuery 检测两个旋转的 div 是否发生碰撞

转载 作者:太空狗 更新时间:2023-10-29 15:14:01 26 4
gpt4 key购买 nike

我想检测两个旋转的 div 是否发生碰撞。如果它们不旋转,我知道该怎么做,但我不知道当它们实际旋转时该怎么做。

我已经尝试了一些碰撞插件,例如 jQuery Collision ( http://sourceforge.net/projects/jquerycollision/ ),但是当 div 旋转时它们不起作用。

我使用 CSS 的变换属性旋转了 div。

最佳答案

它不是 100% 准确,但它适用于大多数情况,我使用了来自 here 的算法:

function isUndefined(a) {
return a === undefined;
}

/**
* Helper function to determine whether there is an intersection between the two polygons described
* by the lists of vertices. Uses the Separating Axis Theorem
*
* @param a an array of connected points [{x:, y:}, {x:, y:},...] that form a closed polygon
* @param b an array of connected points [{x:, y:}, {x:, y:},...] that form a closed polygon
* @return true if there is any intersection between the 2 polygons, false otherwise
*/
function doPolygonsIntersect (a, b) {
var polygons = [a, b];
var minA, maxA, projected, i, i1, j, minB, maxB;

for (i = 0; i < polygons.length; i++) {

// for each polygon, look at each edge of the polygon, and determine if it separates
// the two shapes
var polygon = polygons[i];
for (i1 = 0; i1 < polygon.length; i1++) {

// grab 2 vertices to create an edge
var i2 = (i1 + 1) % polygon.length;
var p1 = polygon[i1];
var p2 = polygon[i2];

// find the line perpendicular to this edge
var normal = { x: p2.y - p1.y, y: p1.x - p2.x };

minA = maxA = undefined;
// for each vertex in the first shape, project it onto the line perpendicular to the edge
// and keep track of the min and max of these values
for (j = 0; j < a.length; j++) {
projected = normal.x * a[j].x + normal.y * a[j].y;
if (isUndefined(minA) || projected < minA) {
minA = projected;
}
if (isUndefined(maxA) || projected > maxA) {
maxA = projected;
}
}

// for each vertex in the second shape, project it onto the line perpendicular to the edge
// and keep track of the min and max of these values
minB = maxB = undefined;
for (j = 0; j < b.length; j++) {
projected = normal.x * b[j].x + normal.y * b[j].y;
if (isUndefined(minB) || projected < minB) {
minB = projected;
}
if (isUndefined(maxB) || projected > maxB) {
maxB = projected;
}
}

// if there is no overlap between the projects, the edge we are looking at separates the two
// polygons, and we know there is no overlap
if (maxA < minB || maxB < minA) {
console.log("polygons don't intersect!");
return false;
}
}
}
return true;
};

$('#r').click(function() {
var rota = Math.floor( Math.random() * 100 ),
rotb = Math.floor( Math.random() * 100 ),
pointsa = new Array(4),
pointsb = new Array(4);

$('#a').css('transform', 'rotateZ(' + rota + 'deg)');
$('#b').css('transform', 'rotateZ(' + rotb + 'deg)');

$('#a div').each(function(i) {
pointsa[i] = {x: parseInt($(this).offset().left), y: parseInt($(this).offset().top)};
});

$('#b div').each(function(i) {
pointsb[i] = {x: parseInt($(this).offset().left), y: parseInt($(this).offset().top)};
});


$('#s').val("intersection: " + doPolygonsIntersect(pointsb, pointsa).toString());
});

working fiddle

我猜这不是最好的方法,我的脚本所做的基本上是在 0 到 100 度之间随机旋转,获取每个 Angular 的 x 和 y 位置(使用 div,你可以用数学来做到这一点,也(我不能 :D))并使用这些坐标运行算法。

关于javascript - 使用 JQuery 检测两个旋转的 div 是否发生碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24970416/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com