I'm making a physics based Surgeon Simulator-style game, but for some reason, my collision detection isn't working. I've tracked the problem to its root; a method in a helper class I'm using. Said method is this:
This method is called multiple times during a lot of other methods, and so I can't continue until it is fixed.
If it is of any relevance, the following are my classes:
World
>GameBackdrop
Actor
>Objects
>Box
>Bottle
>Floor
>Arm
>Little
>Ring
>Middle
>Index
>Thumb
And the full code of Bottle and Objects (the only programmed classes) is this:
Help is much appreciated, as I can't continue 'til my actors aren't falling off the world! :P
public boolean amITouchingObject() { if (getIntersectingObjects(Box.class) != null) { return true; } else if (getIntersectingObjects(Bottle.class) != null) { return true; } else if (getIntersectingObjects(Floor.class) != null) { return true; } { return false; }
import greenfoot.*; public class Bottle extends Objects { private boolean pickedUp = false; private int xVel = 0; private int yVel = 0; private int oldX; private int oldY; private int counter = 0; public void act() { if (pickedUp) { if(Greenfoot.isKeyDown("A") == false && Greenfoot.isKeyDown("W") == false && Greenfoot.isKeyDown("E") == false && Greenfoot.isKeyDown("R") == false && Greenfoot.isKeyDown("space") == false) { xVel = getX() - oldX; yVel = getY() - oldY; pickedUp = false; counter = 0; } setLocation(((GameBackdrop)getWorld()).arm.getX() + 40, ((GameBackdrop)getWorld()).arm.getY() - 250); } else { moveVelocity("Sound - Glass Shatter.wav", "Bottle Debris.png", xVel, yVel); changeXVelocity(xVel); yVel++; counter = 0; } pickedUp = checkIfPickedUp(); counter++; if (counter == 3) { oldX = getX(); oldY = getY(); counter = 0; } } }
import greenfoot.*; public class Objects extends Actor { public void moveVelocity(String breakSound, String breakParticle, int xSpeed, int ySpeed) { setLocation(getX() + xSpeed, getY()); if (senseEdge(xSpeed, 'x') && xSpeed > 5) { destroyed(xSpeed, ySpeed); } setLocation(getX(), getY() + ySpeed); if (senseEdge(ySpeed, 'y') && ySpeed > 5) { destroyed(xSpeed, ySpeed); } } public int changeXVelocity(int xSpeed) { if (xSpeed != 0) { if (xSpeed > 0) { xSpeed--; } else { xSpeed++; } } return xSpeed; } public boolean senseEdge(int velocity, char direction) { boolean touchedEdgeYet = false; for(int i=0; i==((int) (velocity / 5) + 1); i++){ if (direction == 'x') { if (velocity != 0) { if (velocity > 0) { if (amITouchingObject()) { setLocation(getX() - 1, getY()); touchedEdgeYet = true; if (amITouchingObject()) { setLocation(getX() - 1, getY()); if (amITouchingObject()) { setLocation(getX() - 1, getY()); if (amITouchingObject()) { setLocation(getX() - 1, getY()); if (amITouchingObject()) { setLocation(getX() - 1, getY()); } } } } } } else { if (amITouchingObject()) { setLocation(getX() + 1, getY()); touchedEdgeYet = true; if (amITouchingObject()) { setLocation(getX() + 1, getY()); if (amITouchingObject()) { setLocation(getX() + 1, getY()); if (amITouchingObject()) { setLocation(getX() + 1, getY()); if (amITouchingObject()) { setLocation(getX() + 1, getY()); } } } } } } } } else { if (velocity != 0) { if (velocity > 0) { if (amITouchingObject()) { setLocation(getX(), getY() + 1); touchedEdgeYet = true; if (amITouchingObject()) { setLocation(getX(), getY() + 1); if (amITouchingObject()) { setLocation(getX(), getY() + 1); if (amITouchingObject()) { setLocation(getX(), getY() + 1); if (amITouchingObject()) { setLocation(getX(), getY() + 1); } } } } } } else { if (amITouchingObject()) { setLocation(getX(), getY() + 1); touchedEdgeYet = true; if (amITouchingObject()) { setLocation(getX(), getY() + 1); if (amITouchingObject()) { setLocation(getX(), getY() + 1); if (amITouchingObject()) { setLocation(getX(), getY() + 1); if (amITouchingObject()) { setLocation(getX(), getY() + 1); } } } } } } } } } return touchedEdgeYet; } public boolean amITouchingObject() { if (getIntersectingObjects(Box.class) != null) { return true; } else if (getIntersectingObjects(Bottle.class) != null) { return true; } else if (getIntersectingObjects(Floor.class) != null) { return true; } else { return false; } } public boolean checkIfPickedUp() { if (getObjectsAtOffset(0, 0, Little.class) != null && Greenfoot.isKeyDown("A")) { return true; } else if (getObjectsAtOffset(0, 0, Ring.class) != null && Greenfoot.isKeyDown("W")) { return true; } else if(getObjectsAtOffset(0, 0, Middle.class) != null && Greenfoot.isKeyDown("E")) { return true; } else if(getObjectsAtOffset(0, 0, Index.class) != null && Greenfoot.isKeyDown("R")) { return true; } else if(getObjectsAtOffset(0, 0, Thumb.class) != null && Greenfoot.isKeyDown("space")) { return true; } else { return false; } } public void destroyed(int xVel, int yVel) { } }