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

2015/12/15

gravity help

1
2
davemib123 davemib123

2015/12/15

#
Hi, I've attempted to code some gravity / jump code and it sort of works if everything is on one level:
private void checkGravity()
    {
        int groundLevel = 520; 
        boolean onGround = (getY() == groundLevel);

        if (!onGround)
        {
            ySpeed++;
            setLocation(getX(), getY() + ySpeed);
            if (getY() >= groundLevel)
            {
                setLocation(getX(), groundLevel);
            }
        }
        else
        {
            if (Greenfoot.isKeyDown("up"))
            {
                ySpeed = -18;
                setLocation(getX(), getY() + ySpeed);
            }
        }
    }
How do I edit the groundLevel variable to accept offsets for different classes i.e. pipe, stepping tile, block? My scenario is located here: http://www.greenfoot.org/scenarios/15453 Thanks in advance
danpost danpost

2015/12/15

#
Maybe looking at my Jump and Run Demo w/Moving Platform scenario might be of some help (run the scenario and click on the 'Who' button to see how I programmed its movement.
davemib123 davemib123

2015/12/16

#
danpost that scenario is great. just some queries relating to this part:
 int dy = (int)Math.signum(ySpeed); // direction of movement
        Actor obstacle = getOneIntersectingObject(null);
        while (obstacle != null)
        {
            setLocation(getX(), getY()-dy);
            obstacle = getOneIntersectingObject(null);
            ySpeed = 0;
            if (dy > 0) onGround = true;
        }
this part checks all obstacles, if I want to add only specific objects I believe it is something similar to this but cant work out how to alter the while loop:
int dy = (int)Math.signum(ySpeed); // direction of movement
        Actor obstacle = getOneIntersectingObject(SteppingTile.class);
        if (obstacle == null) obstacle = getOneIntersectingObject(Block.class);
        if (obstacle == null) obstacle = getOneIntersectingObject(Brick.class); 
        if (obstacle == null) obstacle = getOneIntersectingObject(Pipe.class);
        if (obstacle == null) obstacle = getOneIntersectingObject(Question.class);
        while (obstacle != null)
        {
            setLocation(getX(), getY()-dy);
            obstacle = getOneIntersectingObject(SteppingTile.class);
            obstacle = getOneIntersectingObject(Block.class);
            obstacle = getOneIntersectingObject(Brick.class);
            obstacle = getOneIntersectingObject(Pipe.class);
            obstacle = getOneIntersectingObject(Question.class);
            ySpeed = 0;
            if (dy > 0) onGround = true;
        }
danpost danpost

2015/12/17

#
I think you need to reset 'obstacle' back to 'null' before reacquiring an intersecting object and again check for 'null' within the 'while' loop.
davemib123 davemib123

2015/12/17

#
danpost wrote...
I think you need to reset 'obstacle' back to 'null' before reacquiring an intersecting object
Not really sure on that one.
danpost wrote...
again check for 'null' within the 'while' loop.
do you mean adding this into the while loop:
obstacle = getOneIntersectingObject(null);
danpost danpost

2015/12/17

#
davemib123 wrote...
danpost wrote...
I think you need to reset 'obstacle' back to 'null' before reacquiring an intersecting object
Not really sure on that one.
I mean:
obstacle = null;
davemib123 wrote...
danpost wrote...
again check for 'null' within the 'while' loop.
do you mean adding this into the while loop
No. I mean this for each subsequent assignment of 'obstacle':
if (obstacle == null)
just like you did before the 'while' loop.
davemib123 davemib123

2015/12/17

#
like this:
int dy = (int)Math.signum(ySpeed); // direction of movement
        Actor obstacle = getOneIntersectingObject(SteppingTile.class); obstacle = null;
        if (obstacle == null) obstacle = getOneIntersectingObject(Block.class); obstacle = null;
        if (obstacle == null) obstacle = getOneIntersectingObject(Brick.class);  obstacle = null;
        if (obstacle == null) obstacle = getOneIntersectingObject(Pipe.class); obstacle = null;
        if (obstacle == null) obstacle = getOneIntersectingObject(Question.class); obstacle = null;
        while (obstacle != null)
        {
            setLocation(getX(), getY()-dy);
            obstacle = getOneIntersectingObject(SteppingTile.class);
            obstacle = getOneIntersectingObject(Block.class);
            obstacle = getOneIntersectingObject(Brick.class);
            obstacle = getOneIntersectingObject(Pipe.class);
            obstacle = getOneIntersectingObject(Question.class);
            ySpeed = 0;
            if (dy > 0) onGround = true;
        }
?
fejfo fejfo

2015/12/17

#
the problem is that only the Question objects will be solid because it will return null so you need to check if it null and then trie to change it like this :
while (obstacle != null)
        {
            setLocation(getX(), getY()-dy);
            obstacle = getOneIntersectingObject(SteppingTile.class);
            if(obstacle == null) obstacle = getOneIntersectingObject(Block.class);
            if(obstacle == null) obstacle = getOneIntersectingObject(Brick.class);
            if(obstacle == null) obstacle = getOneIntersectingObject(Pipe.class);
            if(obstacle == null) obstacle = getOneIntersectingObject(Question.class);
            ySpeed = 0;
            if (dy > 0) onGround = true;
        }
danpost danpost

2015/12/17

#
I guess it could be simplified with using a 'for' loop:
int dy = (int)Math.signum(ySpeed); // direction of movement
Class[] classes = new Class[] { SteppingTile.class, Block.class, Brick.class, Pipe.class, Question.class };
for (int n=0; n<classes.length, n++)
{
    while (getOneIntersectingObject(classes[n]) != null)
    {
        setLocation(getX(), getY()-dy);
        ySpeed = 0;
        if (dy > 0) onGround = true;
    }
}
davemib123 davemib123

2015/12/17

#
fejfo and danpost thanks for the solutions. They work great.
davemib123 davemib123

2015/12/27

#
in relation to the above I'm trying to change the image to a jump one. this is what I have:
 if (shared.facingRight){
                setImage (RMjump);
            }
            else {
                setImage (LMjump);
            }
I'm not really sure where I should include within the moveVertically method:
    /**
     * Method moveVertically: controls vertical movement of actor (including jumping)
     */
    private void moveVertically()
    {
        // get values needed for method

        int dy = 0;
        int worldHeight = getWorld().getHeight();
        boolean onGround = false; // for can-jump state

        // fall
        speedY = speedY + gravity;
        setLocation(getX(), getY() + speedY);

        // check for and move back off of any obstacles
        if (speedY < 0) dy = -1;
        if (speedY > 0) dy = 1;

        Actor obstacle = getOneIntersectingObject(SteppingTile.class);
        if (obstacle == null) obstacle = getOneIntersectingObject(Block.class); 
        if (obstacle == null) obstacle = getOneIntersectingObject(Brick.class); 
        if (obstacle == null) obstacle = getOneIntersectingObject(Pipe.class); 
        if (obstacle == null) obstacle = getOneIntersectingObject(Question.class); 
        while (obstacle != null)
        {
            setLocation(getX(), getY() - dy);
            obstacle = getOneIntersectingObject(SteppingTile.class);
            if(obstacle == null) obstacle = getOneIntersectingObject(Block.class);
            if(obstacle == null) obstacle = getOneIntersectingObject(Brick.class);
            if(obstacle == null) obstacle = getOneIntersectingObject(Pipe.class);
            if(obstacle == null) obstacle = getOneIntersectingObject(Question.class);
            speedY = 0;
            if (dy > 0) onGround = true;
        }

        // check jump
        if (onGround && Greenfoot.isKeyDown("up")) 
        {  
            speedY = speedY - jump_force;     
        }
        
        // check for world edge (bottom)
        if (getY() > worldHeight + 50)
        {
            Level L = (Level)getWorld();
            Counter lifeCounter = L.getLifeCounter();
            lifeCounter.subtract(1);
            isDead = true;
            dead();
        }
    }
I've tried in the jump section, that didn't work. So not really sure.
danpost danpost

2015/12/27

#
That should be done at the point where the value of 'onGround' is determined. Add an 'else' part to the 'if' at line 34 and include the code in the 'else' block.
davemib123 davemib123

2015/12/28

#
so if I've read it correctly you mean like this:
 if (dy > 0)
            {
                onGround = true;
            }
            else{
                if (shared.facingRight){
                    setImage (RMjump);
                }
                else {
                    setImage (LMjump);
                }
            }
danpost danpost

2015/12/28

#
davemib123 wrote...
so if I've read it correctly you mean like this: < Code Omitted >
Did you try it? Does it work? (it does look like what I meant). After looking over it again, I have come across a problem with it, however. Maybe your code should be placed where 'onGround' is initially set to 'false' and the image should be reset to on ground image in the 'if' part (taking the 'else' part back out).
davemib123 davemib123

2015/12/28

#
i've tried your suggestions and I cant seem to get the jump images working. would you mind having a look at the code within the scenario: http://www.greenfoot.org/scenarios/15762
There are more replies on the next page.
1
2