2017-01-28 43 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”这个词?平行宇宙中可能存在不同的物理学;) –