一个用于旋转矩形的2d碰撞检测的javascript函数?

时间:2012-12-18 03:27:43

标签: javascript collision-detection

我正在使用javascript制作一个游戏,我找不到任何类似于一个函数的东西,它会占用2个边界框对象,如果相交则返回true。我发现的所有例子对于我的小游戏来说似乎太复杂了,而且大多数是特定于语言的:/

//my box object
function bounding_box (x, y, width, height, rotation) {
  var box = {};
  box.x = x;
  box.y = y;
  box.w = width
  box.h = height;
  box.r = rotation; //degrees from origin - all objects in the game have the same rotation origin
  return box;
}

function boxes_collide (a, b) {
  //if collision return true
  //else return false

  //my box collision function at the moment
  //doesn't work with rotation
  return !(
      ((a.y + a.h) < (b.y)) ||
      (a.y > (b.z + b.h)) ||
      ((a.x + a.w) < b.x) ||
      (a.x > (b.x + b.h))
  );
}

//create boxes
var boxa = bounding_box(0, 0, 5, 3, 45);
var boxb = bounding_box(1, 3, 4, 2, 90);

//test for collision
if (boxes_collide (boxa, boxb)) {
  alert('collision');
}

我在这个小问题上被困了几个小时,任何帮助都将不胜感激! :)

1 个答案:

答案 0 :(得分:0)

参数b没有名为z的属性。