簡體   English   中英

一個用於旋轉矩形的2d碰撞檢測的javascript函數?

[英]a javascript function for 2d collision detection with rotated rectangles?

我正在用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');
}

我已經在這個小問題上被困了幾個小時,任何幫助都將不勝感激! :)

參數b沒有名為z的屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM