我正在为班级做一个编程项目。我们正在编写游戏爆发,它由一个球、一个桨、四个边缘和砖块组成。球从不同的物体上反弹,当检测到碰撞时,每个物体都会对自己执行不同的操作。下面是我目前不起作用的代码。我正在尝试使用对象的位置(其中心点)来构造一个有界框和每个边缘的值(上、下、左、右)来计算框是否命中。我在脑海中想到,可能存在两种类型的碰撞,一种是拐角碰撞,另一种是一个物体撞到另一个物体中间的某个地方。如果可以,请查看我的代码并提供帮助。我不知道是否有更好的方法来做我想做的事情,但我目前没有
这是代码的一部分,用于检查能够与所有其他可能发生碰撞的对象发生碰撞的每个对象。它还会移动每个对象并计时游戏的时钟。
/**
* Tell the GameWorld that the "game clock" has ticked. A clock tick in the GameWorld has the
* following effects: (1) all movable objects are told to update their positions according to there
* current heading and speed, (2) the "elapsed game time" is incremented by one and (3) all Items are
* checked for a collision.
*/
public void tickClock() {
gameClock++;
Iterator theElements0 = listOfGameObjects.getIterator();
while (theElements0.hasNext()){
GameObject gObj = (GameObject) theElements0.getNext();
if ( gObj instanceof IMovable){
IMovable mObj = (IMovable)gObj;
mObj.move(gameClock);
}
}
Iterator theElements1 = listOfGameObjects.getIterator();
while (theElements1.hasNext()){
GameObject gObj0 = theElements1.getNext();//get a collidable object.
if(gObj0 instanceof ICollider){
ICollider curObj = (ICollider) gObj0;
//check if this object collides with any OTHER object.
Iterator theElements2 = listOfGameObjects.getIterator();
while(theElements2.hasNext()){
GameObject gObj1 = theElements2.getNext();
if(gObj1 != curObj && gObj1 instanceof ICollider) {
ICollider otherObj = (ICollider) gObj1;
if (curObj.collidesWith(otherObj)){
curObj.handleCollision(otherObj);
}
}
}
}
}
setChanged();
notifyObservers();
}
这是代码的一部分,用于确定一个对象是否与另一个对象发生碰撞,以及如果发生碰撞则要采取的适当措施。这段代码专门来自球对象,所以当它撞到砖块时执行的动作是 ySpeed 反转。
public boolean collidesWith(ICollider otherObj) {
GameObject gObj = (GameObject) otherObj;
//this collider
int r1 = (int) (getX() + getWidth()/2);
int l1 = (int) (getX() - getWidth()/2);
int t1 = (int) (getY() + getHeight()/2);
int b1 = (int) (getY() - getHeight()/2);
//the other collider
int r2 = (int) (gObj.getX() + gObj.getWidth()/2);
int l2 = (int) (gObj.getX() - gObj.getWidth()/2);
int t2 = (int) (gObj.getY() + gObj.getHeight()/2);
int b2 = (int) (gObj.getY() - gObj.getHeight()/2);
//corner collision check
if(r1>l2 && t2>b1 && t1>t2 && b1>b2){
System.out.println("Corner Collision check 1");
return true;
}
if(r2>l1 && t2>b1 && t1>t2 && b1>b2){
System.out.println("Corner Collision check 2");
return true;
}
if(r2>l1 && t1>b2 && t2>t1 && b2>b1){
System.out.println("Corner Collision check 3");
return true;
}
if(r1>l2 && t1>b2 && t2>t1 && b2>b1){
System.out.println("Corner Collision check 4");
return true;
}
//middle collision check
if(l1>l2 && r1<r2 && t1<t2 && b1<b2){
System.out.println("middle collision check 1");
return true;
}
if(l1>l2 && r1<r2 && t1>t2 && b1>b2){
System.out.println("middle Collision check 2");
return true;
}
if(l1<l2 && r1<r2 && t1<t2 && b1>b2){
System.out.println("middle Collision check 3");
return true;
}
if(l1>l2 && r1>r2 && t1<t2 && b1>b2){
return true;
}
return false;
}
public void handleCollision(ICollider otherObject) {
if(otherObject instanceof Brick){
System.out.println("Brick Hit");
ySpeed = -(ySpeed);
}
}