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

2015/3/19

Ball with gravitation

Tree Tree

2015/3/19

#
Hello, I´m currently creating a volley game and I want to create a ball which is bouncing from one to the other player. The problem is that the ball shound move with the kick from the players and fly with the Gravitation but I´ve no idea if and how it works. I hope anyone can help me...
danpost danpost

2015/3/20

#
You just need fields to track the current speeds both in the horizontal direction as well as the vertical. Gravity can be easily implemented by increasing the vertical speed by a constant every act cycle. For move fluid movement, you may want to track the location coordinates in a more accurate way other than what is provided through greenfoot (either track them using the primitive type 'double' or by using the primitive type int with a magnification factor).
Tree Tree

2015/3/20

#
Can you please show me how to create the codes i'm a beginner and I don't know how to realize your instructions. Thank you!
danpost danpost

2015/3/20

#
What do you have -- as far as a Ball class, so far? Start off with a simple ball that moves around bouncing off the edges of the world. Then build from there, adding the additional behaviors one by one (tracking coordinates, gravity, paddles).
Tree Tree

2015/3/24

#
The ball is working but the flight path doesn´t work... How can I get the flight path? Ball code
public class Ball extends MoveEffects
{
    int weight = 1;
    int speed = 5;
    boolean started = false;
    private Mover_1 mover1;
    private Mover_2 mover2;
        
    public void act() 
    {
        run();
        actGravity(weight);
        moveHorizontal(speed);
        bounce(weight);
        speed = getNewSpeed(speed);
    }

    private void run() 
    {
        Actor mover1 = getOneIntersectingObject (Mover_1.class);
        if (mover1 != null) 
        {
            gravityeffect = - gravityeffect;
            setLocation(getX() , getY()-30);
            
        }
        
    }
}
MoveEffects Code
public class MoveEffects extends Players
{
    boolean collidedright = false;
    boolean collidedleft = false;
    int gravityeffect = 1/2;
    
    public void actGravity(int weight)
    {
        int x = getX();
        int y = getY();
        gravityeffect += weight;
        if (weight >= 2)
            gravityeffect--;
        if (gravityeffect >= weight);
            setLocation(x, y + gravityeffect);
    }
    
    public void spin(int speed)
    {
        int rotation = getRotation();
        setRotation(rotation + (speed * 2));
    }

    public boolean detectCollision(Class item, int xcord, int ycord)
    {
        if (getOneObjectAtOffset( xcord, ycord, item) != null)
            return true;
        return false;
    }

    public int getNewSpeed(int speed)
    {
        if (getY() >= getWorld().getHeight() - 5 || getX() >= getWorld().getWidth() - 5 || getX() <= 5)
        {
            if (speed > 0)
                return speed - 1;
            else if (speed < 0)
                return speed + 1;
        }
        return speed;
    }

    public void bounce(int weight)
    {
        int y = getY();
        if (y >= getWorld().getHeight() - 5)
            gravityeffect = 0 - (gravityeffect * 5)/(5 + weight);
    }

    public void moveHorizontal(int speed)
    {
        int x = getX();
        int y = getY();
        
        if (speed >= 0)
        {
            if (collidedleft)
                setLocation(x + speed, y);
            else if (collidedright)
                setLocation(x - speed, y);
           
        }
        else
        {
            if (collidedleft)
                setLocation(x - speed, y);
            else if (collidedright)
                setLocation(x + speed, y);
            
        }
    }

    public void wrapHorizontal()
    {
        if (getX() >= getWorld().getWidth() - 5)
            setLocation(5, getY());
        else if (getX() <= 5)
            setLocation(getWorld().getWidth() - 5, getY());
    }

    public void wrapVertical()
    {
        if (getY() >= getWorld().getHeight() - 5)
            setLocation(getX(), 5);
        else if (getY() <= 5)
            setLocation(getX(), getWorld().getHeight() - 5);
    }
}
danpost danpost

2015/3/24

#
What do you mean by 'the flight path doesn't work'? How is it behaving compared to what you want it to do? Be specific.
Tree Tree

2015/3/24

#
Sorry for that... The ball should fly from one to the other mover - like a volleyball. At the moment it is still bouncing over the movers and I can´t shoot the ball form one to the opposite mover. I search for the correct code to play with the two movers to complete the game...
danpost danpost

2015/3/24

#
Still, you are being quite vague. 'like a volleyball' does kind of give an idea of what you want, 'bouncing over' is ambiguous, and 'shoot the ball' seems confusing.
Tree Tree

2015/3/24

#
I want that the ball is able to be kicked between two Players. If one Player is touching the ball it should fly in a kind of halfcircle to the other Player I think the problem is the setLocation Part but i don´t know how to solve the Problem.
 Actor mover2 = getOneIntersectingObject (Mover_2.class);
        if (mover2 != null) 
        
        {
            gravityeffect = - gravityeffect;
            setLocation(getX() , getY()-30);
            Greenfoot.playSound("bums.wav");

        }
danpost danpost

2015/3/24

#
I think the main issue is in the naming of the fields. 'gravityeffect' appears to be more of a vertical speed field and 'weight' appears to be more of a force of gravity constant. The 'gravityeffect' field, if it is indeed representing that which I believe, should have a constant value (force of gravity) added to it every act, which I believe is done at line 10 of the MoveEffects class. This will produce the downward pull associated with gravity which looks to be processed in the 'actGravity' method of the MoveEffects class (though I cannot immediately say if the code in that class works properly or as you would expect).
danpost danpost

2015/3/24

#
Another issue may be your class structure. Could you please list all your classes, indicating what class each one extends.
danpost danpost

2015/3/24

#
One major issue found. The 'moveHorizontal' method of the MoveEffects class will never produce any horizontal movement to your actor. It would require that one of the two booleans, 'collidedleft' or 'collidedright', be true; but, nowhere in the code are the values of either field changed or re-assigned any value (their initial values of 'false' forever remain).
You need to login to post a reply.