2012-07-13 84 views
2

我一直在尋找一些方法來在我的2D模擬中實現四叉樹,以便使碰撞檢測更快,但我發現這個概念很難掌握。 這個模擬器的效果很好,因爲它現在只是一旦我通過了160-180個粒子,它會變得非常緩慢,因爲碰撞檢測完全不必要地經過所有的粒子,而且只是簡單的愚蠢。 現在它只是一羣或多個圓圈在屏幕周圍相互碰撞,我可以通過點擊並拖動鼠標以新的速度和位置產生新的。在Quadtrees上需要幫助java

EDIT1:

嗯,我想我是能夠立即創建樹,你可以在圖片中看到

我的四叉樹圖像:http://i.stack.imgur.com/c4WNz.jpg

所以,現在的問題是怎麼做我使它對我的碰撞檢測有用...

我每次檢查時都必須從零開始創建整棵樹嗎?

我即將做的,而我等待着什麼,希望這是正確的以某種方式:P

1_Check如果有在每個節點一球,離開這個節點是否有不關。 2_保持檢查交點直到我點擊葉級並將這些相交的球添加到葉節點。 3_Collide球葉節點之前,我繼續前進?

這裏是我的QuadTreeNode類:

public class QuadTreeNode { 
    private QuadTreeNode parent; 
    private QuadTreeNode[] children;  
    private int id; 
    private double x; 
    private double y; 
    private double width; 
    private double height; 

public QuadTreeNode(double x, double y, double width, double height, int id, QuadTreeNode parent){ 
    this.x = x; 
    this.y = y; 
    this.width = width; 
    this.height = height; 
    this.children = new QuadTreeNode[4]; 
    this.id = id; 
    this.parent = parent; 
    //System.out.println("<x:>"+x+"<y:>"+y+"<w:>"+width+"<h:>"+height);  
    if (this.width>=1000/12 && this!=null){ 
     nodes+=1; 
     this.children[0] = new QuadTreeNode(x, y, width/2, height/2, id+1, this); 
     this.children[1] = new QuadTreeNode(x + width/2, y, width/2, height/2, id+2, this); 
     this.children[2] = new QuadTreeNode(x, y + height/2, width/2, height/2, id+3, this); 
     this.children[3] = new QuadTreeNode(x + width/2, y + height/2, width/2, height/2, id+4, this); 
    } 
} 
+0

問題是什麼? – toto2 2012-07-13 23:21:31

回答

1

我覺得這個概念相當難以把握

理念:

  • 因爲細胞的遞歸分區

    • 該單元格包含固定計數的基元。
    • 發生最大分區計數。
  • 從整個場景的邊界框開始。

  • 遞歸:如果單元格中的元素太多,則將它們分開。
  • 如果房間爲空(自適應分區),則不分區
  • 精細分區,其中幾何爲。

提示:遍歷是相對昂貴,因爲許多向上和向下運動在hirarchy。

  • 將原語保存在內部節點或葉子中。

穿越: - 測試與根的軸對齊包圍盒(第一:整個場景)

提示:先打不能最接近。

簡而言之,這就是四叉樹或八叉樹(3D)的原理。我在這裏有一個幻燈片,它描述了圖像,但我不想全部上傳。我認爲這不是你的問題的完整答案,但可能會有所幫助。