Ball movement in Volleyball game is too fast

Asked

Viewed 127 times

3

For some time I’ve been developing a game of Volleyball in Java with Slick2d, but I’m having a very boring problem with the ball’s movement: I can’t get the ball to perform a parabola move (draw character, ball goes up, goes down on the other character) decent and keep the speed cool to play.

class update ball

    float originalSpeed = (velocidade * InGameState.GetDeltaTime());
    // Movimentação no eixo X

        float speed;        
        if (distanceX < originalSpeed) {
            speed = distanceX;
        } else {
            if (distanceX > originalSpeed) {
                speed = originalSpeed;
            } else {
                speed = 0;
            }
        }

        if (this.x < this.targetPositionX) {            
            this.speedX = speed;
        } else {
            if (this.x >= this.targetPositionX) {
                this.speedX = -speed;
            }
        }

        // Movimentação no eixo Y                
        if (distanceY < originalSpeed) {
            speed = distanceY;
        } else {
            if (distanceY > originalSpeed) {
                speed = originalSpeed;
            } else {
                speed = 0;
            }
        }
        if (this.y < this.targetPositionY) {
            this.speedY = speed;
        } else {
            if (this.y >= this.targetPositionY) {
                this.speedY = -speed;
            }
        }


        float percentX;
        float percentY;      

        if (distanceX > distanceY) {
            if (Math.abs(distanceY) < 1) {
                percentX = 0;
            } else {
                percentX = (distanceX/distanceY);
            }
            percentY = 1;
        } else {
            if (distanceY > distanceX) {
                percentX = 1;
                if (Math.abs(distanceX) < 1) {
                    percentY = 0;
                } else {
                    percentY = (distanceY/distanceX);
                }
                percentX = 1;
            } else {
                percentX = 1;
                percentY = 1;
            }
        }

        // Aplicação da "velocidade"                    
        setX(this.x + (this.speedX * percentX));
        setY(this.y + (this.speedY * percentY));

    }

The way the code is above, sometimes the ball gets extremely fast (I suspect it is when the percentY or the percentX are very high) and so can not predict the movement of the ball. Can anyone help me? Thanks!

1 answer

5


First of all, there are some concept problems in the method.

If you want to perform a parable movement, you have to think in terms of a quadratic function of the kind y = ax² + bx + c.

The fact that your code does not have any call to methods that compute square root or square shows that there is no way the movement looks real. Also, comparing distance variables with speed variables doesn’t make much sense, which at the very least makes the code confusing.

Moreover, it is good to remember that in real life the movement of a ball is not a perfect parable, since in addition to gravity details such as the rotation of the ball, wind and air resistance influence the movement.

However, ignoring the details, the ball’s movement can be calculated in two phases, considering two horizontal velocity vectors (X) and vertical (Y).

First, the horizontal movement, on the axis X, can be constant because it does not suffer interference from the force vector of gravity. Optionally you could add a small deceleration factor due to air resistance.

Second, the vertical movement on the axis Y, could be described by a parabola based on a quadratic function as described above. You need to determine the appropriate values of a, b and c. This can be done by studying the effects of each factor in the equation. With the equation determined, just replace x at each different position and you get the approximate value of y.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.