Ball movement in volleyball game is too fast

3

I've been developing a volleyball game in Java with Slick2D for some time now, but I'm having a pretty annoying problem with the movement of the ball: I can not get the ball to perform a parabola move draws, the ball goes up, descends into the other character) decent and keep the cool speed 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));

    }

As the code above is, sometimes the ball gets extremely fast (I suspect it is when percentY or percentX gets too high) and thus can not predict the movement of the ball. Can someone help me? Thanks!

    
asked by anonymous 13.03.2016 / 04:59

1 answer

5

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

If you want to make a parable move, you have to think in terms of a quadratic function of type y = ax² + bx + c .

The fact that your code has no call to methods that calculate square root or square shows that it does not have how the movement looks real. In addition, comparing distance variables with speed variables does not make much sense, and at the very least makes the code confusing.

In addition, it is worth remembering that in real life the movement of a ball is not a perfect parabola, since in addition to gravity details such as the rotation of the ball, wind and air resistance influence the movement.

However, bypassing the details, ball motion can be calculated in two phases, considering two vectors of horizontal ( X ) and vertical ( Y ) velocity.

First, the horizontal motion on the X axis 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 motion on the Y axis could be described by a parabola based on a quadratic function, as described above. You need to determine the appropriate values for a , b and c . This can be done by studying the effects of each factor in the equation . With the given equation, just replace x in each different position and you get the approximate value of y .

    
14.03.2016 / 02:51