I am doing a software project for a class. We program a game that consists of a ball, an oar, four edges and bricks. the ball bounces from different objects, and each object performs various operations on itself when a collision is detected. below is my code that is currently not working. I am trying to use the location of the object (its center point) to build a limited frame and the values of each edge (top, bottom, left, right) to calculate whether the boxes hit. I thought that there could be two types of collisions, where the corners hit, and where one object hits somewhere in the middle of another object. Please review my code and help if you can. I don’t know if there is a better way to do what I'm trying to do, but what is currently not working for me and returns true almost all the time.
This is a piece of code that checks every object that might collide with any other object that might collide. It also moves every object and captures the game clock.
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();
if(gObj0 instanceof ICollider){
ICollider curObj = (ICollider) gObj0;
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();
}
This is the part of the code that is used to determine if an object has collided with another object, as well as the corresponding action that needs to be taken, if any. This section of code refers to a specific spherical object, so an action preformed when it hits a brick is a change to ySpeed.
public boolean collidesWith(ICollider otherObj) {
GameObject gObj = (GameObject) otherObj;
int r1 = (int) (getX() + getWidth()/2);
int l1 = (int) (getX() - getWidth()/2);
int t1 = (int) (getY() + getHeight()/2);
int b1 = (int) (getY() - getHeight()/2);
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);
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;
}
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);
}
}
source
share