This site requires JavaScript, please enable it in your browser!
Greenfoot back
Dab1001
Dab1001 wrote ...

2014/8/14

Collision Detection

Dab1001 Dab1001

2014/8/14

#
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:
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;
        }
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:
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)
    {

    }
}
Help is much appreciated, as I can't continue 'til my actors aren't falling off the world! :P
Super_Hippo Super_Hippo

2014/8/14

#
    public boolean amITouchingObject()
    {
        if (getOneIntersectingObject(Box.class) != null || getOneIntersectingObject(Box.class) != null || getOneIntersectingObject(Floor.class) != null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
But I am not sure if this will change anything. By the way, instead of your step by step, you could change line 44 and following to (and similar with the other ones).
while (amITouchingObject())  
{  
    setLocation(getX() - 1, getY()); 
    touchedEdgeYet = true;
}
I am not sure if this is what you wanted:
for(int i=0; i==((int) (velocity / 5) + 1); i++){
Maybe you should change it to the following?
for(int i=0; i<((int) (velocity / 5) + 1); i++){
Probably the casting to int is also not needed. But with your code, I think it will never get into this for loop unless 'velocity/5+1' is between 0 and 1. So change this '==' to '<' and tell me whether or not anything have changed. ;)
Dab1001 Dab1001

2014/8/14

#
Super_Hippo wrote...
    public boolean amITouchingObject()
    {
        if (getOneIntersectingObject(Box.class) != null || getOneIntersectingObject(Box.class) != null || getOneIntersectingObject(Floor.class) != null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
But I am not sure if this will change anything. By the way, instead of your step by step, you could change line 44 and following to (and similar with the other ones).
while (amITouchingObject())  
{  
    setLocation(getX() - 1, getY()); 
    touchedEdgeYet = true;
}
I am not sure if this is what you wanted:
for(int i=0; i==((int) (velocity / 5) + 1); i++){
Maybe you should change it to the following?
for(int i=0; i<((int) (velocity / 5) + 1); i++){
Probably the casting to int is also not needed. But with your code, I think it will never get into this for loop unless 'velocity/5+1' is between 0 and 1. So change this '==' to '<' and tell me whether or not anything have changed. ;)
It worked, thanks! :) It did create another problem though; now whenever it hits the box, it instantly goes to the far left of the box and continued falling :/ EDIT: Wait, no, that was just me being forgetful when copy/pasting, and forgetting to make it change the Y when direction was 'y'. Thanks for your help :)
You need to login to post a reply.