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

2013/8/10

Checking about below

Kartoffelbrot Kartoffelbrot

2013/8/10

#
For my Jump and Run I need a method that checks, wether an object is below the actor or not. It looks like this:
public boolean objectBelow(Class clss)
    {
        Actor a1 = getOneObjectAtOffset(0, (getImage().getHeight()/2), clss);
        Actor a2 = getOneObjectAtOffset(0, (getImage().getHeight()/2)+1, clss);
        Actor a3 = getOneObjectAtOffset(0, (getImage().getHeight()/2)+2, clss);
        Actor a4 = getOneObjectAtOffset(0, (getImage().getHeight()/2)+3, clss);
        Actor a5 = getOneObjectAtOffset(0, (getImage().getHeight()/2)+4, clss);
        if(a1!=null||a2!=null||a3!=null||a4!=null||a5!=null){
            return true;
        }
        return false;
    }
But it is ugly. If the Player falls with one pixel per act, he will stop falling four pixels above the bottom and if he is faster than 5 pixels per act, he falls through the my bootom objects. I know why it works this way, but I don't know how to make this method better. Any ideas?
Kartoffelbrot Kartoffelbrot

2013/8/10

#
Didn't wrote the title correctly so it isn't correct and looks bad, but now it's too late to change.
danpost danpost

2013/8/10

#
It would be hard to tell how to fix the stopping/falling through the object without the movement code. The only code you supplied was the below object detection code. However, you are correct about the ugliness of the method you supplied. I suggest using the 'getOneIntersectingObject' call for collision detection. Use it once after horizontal movement and once after vertical movement (falling code); the sign of the speed in each direction can be used to determine if any intersecting object is (a) above or below (falling) or (b) left or right (moving) of this actor.
Gevater_Tod4711 Gevater_Tod4711

2013/8/11

#
I think the best way to resolve this problem is to let the actor move into the ground and afterwards move him up again. Try it like this:
public void fall() {
    //move down;
    if (getOneObjectAtOffset(0, getImage().getHeight()/2 + 1, Ground.class) != null) {
        while(getOneObjectAtOffset(0, getImage().getHeight()/2, Ground.class) != null) {
            setLocation(getX(), getY()-1);
        }
    }
}
So your actor will fall down into the ground and move out the ground within the same act because of the while loop.
Kartoffelbrot Kartoffelbrot

2013/8/11

#
Thank you both.
Kartoffelbrot Kartoffelbrot

2013/8/12

#
This is my code now and it works very well:
public void gravitation()
    {
        if(!objectBelow(Bottom.class)){
            setLocation(getX(), getY() + fallgeschwindigkeit);
            fallgeschwindigkeit+=g;
        }
        if(objectBelow(Bottom.class)){
            fallgeschwindigkeit=0;
            while(objectBelow(Bottom.class))
                setLocation(getX(), getY() - 1);
            if(getOneObjectAtOffset(0,getY()+getImage().getHeight()/2,Bottom.class)==null)
                setLocation(getX(), getY() + 1);
        }
    }
public boolean objectBelow(Class clss)
    {
        Actor a = getOneIntersectingObject(clss);
        if(a!=null)
            if(a.getY()>getY())
                return true;
        return false;
    }
You need to login to post a reply.