2017-01-28 37 views
-3

好吧,所以我想構建一個方法來檢測兩個對象是否發生碰撞。他們的hitboxes存儲在數組中。像這樣[topLeftX,topLeftY,bottomRightX,bottomRightY]爲這兩個對象中的每一個。我無法弄清楚如果聲明使用這兩個數組來檢測這個。檢測與Java中的數組碰撞

public class Physics { 
    public static boolean isColliding(int ob1Hitbox[], int ob2Hitbox[]) { 

    } 
} 

如果發生碰撞,該方法必須返回true。

+2

歡迎堆棧溢出!看起來你正在尋求作業幫助。雖然我們本身沒有任何問題,但請觀察這些[應做和不應該](http://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions/338845#338845),並相應地編輯您的問題。 –

+0

這裏'對象'的含義是什麼? – Null

+0

@Null矩形 –

回答

1

你可以爲了使用Rectangle#intersects已經計算爲你所做的:

import java.awt.Rectangle; 

public class Physics { 
    public static boolean isColliding(int[] ob1Hitbox, int[] ob2Hitbox) { 
     return toRectangle(ob1Hitbox).intersects(toRectangle(ob2Hitbox)); 
    } 

    private static Rectangle toRectangle(int[] hitbox) { 
     int x = hitbox[0]; 
     int y = hitbox[1]; 
     int width = hitbox[2] - x; 
     int height = y - hitbox[3]; 
     return new Rectangle(x, y, width, height); 
    } 
} 
+0

爲什麼你使用「static」這個詞?平行宇宙中可能存在不同的物理學;) –