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

2012/12/16

my character falls through the ground

KierTulp KierTulp

2012/12/16

#
hello! I have made a platforming game but when my character drops from heigh places, he falls through or into the ground. I've made a code to prevent it, but it still seems to fall through from time to time, this only happens when he builds up a lot of downwards speed.
public void checkFall()
    {
        if (onGround()){
            vSpeed = 0;
        }
        else { fall();
        }
    }
    
    public boolean onGround()
    {
        Actor Under = getOneObjectAtOffset (0, getImage().getHeight() / 2, zwarteBalk.class);
        return Under != null;
        
    }
thats the code i use to stop him from falling. Does anyone know a way of stopping him from falling through the floor anyway? thanks!
SPower SPower

2012/12/16

#
change onGround to:
Actor under = getOneIntersectingObject(zwarteBalk.class);
return (under != null);
And as some advice, don't start your variable names with an Uppercase, but with a lowercase. Java won't complain when you don't do so, but you confuse others.
KierTulp KierTulp

2012/12/16

#
thanks for the tip, but he still falls through the floor with your code. it is not the problem that he always falls through the floor, the problem is that he falls through when he drops fast.
marzukr marzukr

2012/12/16

#
Maybe you should reduce vSpeed so it doesn't fall as fast.
SPower SPower

2012/12/16

#
After this:
vSpeed = 0;
add this:
Actor a = getOneIntersectingObject(zwarteBalk.class);
setLocation(getX(), a.getY() -a.getImage().getWidth() /2 -getImage().getWidth() /2);
KierTulp KierTulp

2012/12/16

#
when I put in the code you send me my player just keeps bouncing and bouncing, so I dont think that that will work. and to marzukr
private int vSpeed = 0;
    private int acceleration = 1;
    
and
public void fall()
    {
        setLocation ( getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration; 
    }
I have tried doing that before, I believe I have it at the slowest that can be as of right now. i tried making it a 0,5 but he doesn't allow that, and when making it a double, it says that double can't be counted with int and all that jazz, so I dont think that that will work.
marzukr marzukr

2012/12/16

#
Hmm...
danpost danpost

2012/12/16

#
One thing you could do is: in each act cycle, when it is falling, move downward in smaller steps (no bigger than the height of any object that could stop its fall) check for obstacles after each smaller step. This could be done in pixel-sized steps as follows:
public void fall()
{
    vSpeed+=1; // adding vertical acceleration (gravity)
    int step=0;
    while(step<vSpeed && getOneObjectAtOffset(0, getImage().getHeight()/2, null)==null)
    {
        setLocation(getX(), getY()+1);
        step++;
    }
    if(step<vSpeed) vSpeed=0;
}
TisLars TisLars

2012/12/19

#
@danpost This way the player you are controlling isn't able to jump again after the fall() method finished.
danpost danpost

2012/12/19

#
My mind was in fall mode when I wrote that, sorry. Try the following, which allows for negative values as well. An added 'step-back' was added to prevent sinking into a ground surface.
public void fall()
{
    vSpeed+=1; // add gravity
    int dir=(int)Math.signum(vSpeed); // determine direction
    for(int step=0; step!=vSpeed; step+=dir) // for each pixel-step
    {
        setLocation(getX(), getY()+dir); // move 
        if(getOneIntersectingObject(null)!=null) // check intersection
        {
            setLocation(getX(), getY()-dir); // resistance (step-back)
            vSpeed=0; // stopped
            break; // forces exit out of 'for' loop
        }
    }
}
You need to login to post a reply.