我一直在使用数组allShapes。该数组由具有相同种类属性的对象组成,例如每个对象都有一个.x值和.y值。

每个形状都有一个“半径”,因此可以轻松完成检测。对于每个对象,半径都完全相同。它等于10。

如何有效地检查一个形状是否与另一个形状发生碰撞的功能?提前致谢!

var allShapes = [{70,30},{40,90},{287,245}];
// allShapes[0].x = 70
// For each object, there is a .x and .y value

最佳答案

var allShapes = [{70,30},{40,90},{287,245}];

for(let i = 0; i < allShapes.length - 1; i++) {
  for(let j = i + 1; j < allShapes.length; j++) {
    //check if allShapes[i] and allShapes[j] are colliding
  }
}


检测细目:

- allShapes[i] = {70,30}
  - allShapes[j] = {40,90} => check collision
  - allShapes[j] = {287,245} => check collision
- allShapes[i] = {40,90}
  - allShapes[j] = {287,245} => check collision

09-07 11:29