我正在为课程做一个编程项目。我们正在编制打砖块游戏,该游戏包括一个球、一个挡板、四个边缘和砖块。当球与不同物体相撞时,球会弹开,并且每个物体在碰撞时执行不同的操作。以下是我的代码,当前它无法正常工作。我试图使用对象的位置(其中心点)构建有界框,并使用每个边缘的值(顶部、底部、左侧、右侧)来计算是否发生碰撞。我想到可能有两种类型的碰撞,一种是角落相撞,另一种是一个物体撞击另一个物体的中间某个地方。请查看我的代码并在需要时提供帮助。我不知道是否有更好的方法来实现我的想法,但目前我的代码无法正常工作,并且几乎总是返回true。
以下是代码的一部分,它检查每个能够与其他可以碰撞的对象相撞的对象。它还移动每个对象并打开游戏的时钟。
/**
* 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);
}
}