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

2013/10/28

Collision

Skriener Skriener

2013/10/28

#
Hi there! I'm trying to make a top down maze game. After allot of trouble figuring out collision detection i started searching on this forum. I finally found a scenario displaying exactly what i need: http://www.greenfoot.org/scenarios/524 Problem is, it was made in a previous version and doesn't run in the current updated Greenfoot. I am only a beginner so i cant get it to work either. Can someone please help!
Baenefin Baenefin

2013/10/28

#
You could make an algorithm or are you going to hardcode your level?
MatheMagician MatheMagician

2013/10/28

#
Open up the scenario, and hit "continue" when it tells you that there is an outdated version. Replace every instance of
a.getWidth()
with
a.getImage().getWidth()
and replace
a.getHeight()
with
a.getImage().getHeight()
danpost danpost

2013/10/28

#
What MatheMagician suggested will work. However, that scenario has no collision detection within it (I looked at the code, fixed it and ran it). Maybe my Barriers and Bars scenario may be of some help.
Aermus Aermus

2013/10/28

#
danpost wrote...
What MatheMagician suggested will work. However, that scenario has no collision detection within it (I looked at the code, fixed it and ran it). Maybe my Barriers and Bars scenario may be of some help.
Danpost, your "Barriers and Bars" works even better, thank you. I will be using an img of a topdown person with an walking animation. Will it be possible to use turn() in combination with your code? Thanks in advance :)
Skriener Skriener

2013/10/28

#
Aermus wrote...
danpost wrote...
What MatheMagician suggested will work. However, that scenario has no collision detection within it (I looked at the code, fixed it and ran it). Maybe my Barriers and Bars scenario may be of some help.
Danpost, your "Barriers and Bars" works even better, thank you. I will be using an img of a topdown person with an walking animation. Will it be possible to use turn() in combination with your code? Thanks in advance :)
Uhm.. Hello, that's exactly what i'm looking for. Ive tried to combining my mover code with the Barriers and Bars. Ill let you know when i get it to work. Thanks for the comments everyone!
danpost danpost

2013/10/28

#
Using 'move' and 'turn', I came up with this:
/**
 * Method act:
 * checks for keypresses and moves the robot the appropriate speed until it comes upon a barrier
 */
public void act() 
{
    move();
}

/**
 * Method move:
 * checks for keypresses and turns and moves the robot
 */
private void move()
{
    int dr = 0;
    if (Greenfoot.isKeyDown("left")) dr--;
    if (Greenfoot.isKeyDown("right")) dr++;
    if (dr != 0)
    {
        turn(dr);
        if (getOneIntersectingObject(Barrier.class) != null) turn(-dr);
    }
    int d = 0;
    if (Greenfoot.isKeyDown("up")) d++;
    if (Greenfoot.isKeyDown("down")) d--;
    int x = getX(), y = getY();
    if (d != 0)
    {
        for (int i=1; i<= speed; i++)
        {
            setLocation(x, y);
            move(d*i);
            if (getOneIntersectingObject(Barrier.class) != null)
            {
                setLocation(x, y);
                move(d*(i-1));
                return;
            }
        }
    }
}
You need to login to post a reply.