1
\$\begingroup\$

I was wondering how to figuring out how to slice a ball (banana kick) by flinging it curve-ways. However, according to the LibGDX's gesture listener, the method for flinging only focuses on a straight direction, whether if the ball went to the left, right, or straight.

@Override
public boolean fling(float velocityX, float velocityY, int button) 
{
    // Set flag for goal.
    gotPoint = false;
    isBallMissed = true;

    // Set flag to enable scrolling goalkeeper when a player's turn as a goalkeeper.
    isAIKicked = false;

    // Re-adjust ball fling coordinates...
    x = velocityX/25;
    y = (velocityY/25) * -1;

    if(!flingLocked && !gotPoint)
    {
        if(y > 100) // --> Checks if the gling power is just right or else it miss.
        {
            if(y > 130) 
            {
                goalpostHit = false;
                crossbarHit = false;
                disableGoal = true;
                meterSpeed = 50f;

            } else {

                goalpostHit = false;
                crossbarHit = true;
                disableGoal = true;
                meterSpeed = 20f;

            }

        } else {

            goalpostHit = true;
            crossbarHit = true;
            disableGoal = false;
            meterSpeed = 3f;

        }
    }

    // Get the value to set the collision on or off.
    bp.setGhostPost(goalpostHit);
    bp.setGhostCrossbar(crossbarHit);

    // For debugging purposes only. Checks if the fling direction of the ball went. (output display console only)
    System.out.println("Hit Post Enabled Status: " + goalpostHit);
    System.out.println("Hit Bar Enabled Status: " + crossbarHit);
    System.out.println("touchUp() at Y: " + y);
    System.out.println("fling() at Y: " + y);
    System.out.println("fling() at X: " + x);

    // For debugging purposes only. Checks if the fling direction of the ball went.
    if(x >= 1)
    {
        directionStatus = "RIGHT";

    } else if(x <= -1) {

        directionStatus = "LEFT";

    } else {

        directionStatus = "FOWARD";

    }

    if(!goalKeeper && !flingLocked && !freezeGame && !isPaused && !gotPoint) // --> Tells is the ball is flinged or not while the player is under kicker role.
    {
        // Play SFX.
        SFX.getBallKicked().play(1f);

        // Set Timer
        timer4 = 0;

        // Set AI's smart level as a goalkeeper
        speedLevel = (rnd.nextInt(175 - 55) + 55);
        isReversed = rnd.nextInt(10);

        // Set mass of a body.
        MassData mass = new MassData();
        mass.mass = 400;

        // Re-create body of the ball.
        wc.getWorld().destroyBody(ballBody);
        ballDef.position.rotate(0f);
        ballDef.angle = 30f;
        ballDef.position.set(rc.getSoccerBallX(), 10);
        ballDef.allowSleep = true;
        ballBody = wc.getWorld().createBody(ballDef);
        ballBody.setActive(true);
        ballBody.applyLinearImpulse(x, y, rc.getSoccerBallX(), 10); // --> Makes the direction go left or right?
        ballBody.applyAngularImpulse(200);
        ballBody.isBullet();
        ballBody.setMassData(mass);
        ballBody.setLinearDamping(-990f); // --> This will set how fast will accelerate the object will fall. Set it to a negative value to increase falling speed.
        ballBody.setGravityScale(0); // --> Set it to negative value and the ball will lift. Set it to a positive value and the ball falls normally.

        // Load the ball.
        ballLoader.attachFixture(ballBody, "soccer ball", ballFixture, rc.getSoccerBallWidth());
        v2.ball_origin = ballLoader.getOrigin("soccer ball", rc.getSoccerBallWidth()).cpy();

        // Disable fling and set timer.
        switchRoleTimer = true;
        flingLocked = true;

        // Action ended per turn.
        if ((noOfTries < 11) && !isSuddenDeathmatch)
        {
            noOfTries++;
        }

        // Disable fling temporarily.
        Gdx.input.setInputProcessor(this);
    }


    return false;
}

What can I do to make the ball fling in curve-ways if I fling it curve-ways? Should I make a flag and mathematically computed the coordinates by analyzing the finger in order to recognize the response of this kind of fling? Take note that I'm making a 2D overview of the soccer game.

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

i didn't read all of your code but if i get it right you want a curve movement.

in linar algebra you can use vector addition for velocity and acceleration of object

lets say :

  my_pos = (0,0)

  velocity = (1,3)

  acceleration = (0,-1) //gravity?

  do(lets say  every half a second?)
  {
       to_move_x =velocity.x  + acceleration.x  
       to_move_y =velocity.y  + acceleration.y  


       my_pos .x += to_move_x 
       my_pos .y += to_move_y 
   }
   //if (y<0) {y=0} or the object hits ground or solid, stop move loop

first loop to move will be(1,2) and my_pos =(1,2)

second loop to move will be(1,1) and my_pos =(2,3)

third loop to move will be(1,0) and my_pos =(3,3)

fourth loop to move will be(1,-1) and my_pos =(4,2)

which will look like:

           (3,3)
             _
 (2,3) ->   /  \<-(4,2)
 (1,2) ->  /    \<-(5,1)

and thats a curve.

i hope thats what you looked for.

learn linar algebra it solves stuff like that.its very helpful for game dev (and geometry and trigonometry).

\$\endgroup\$
2
  • \$\begingroup\$ Is the loop beginning from the first one until the last creates a one whole curve-way ball shoot? \$\endgroup\$ Jun 21, 2013 at 0:56
  • \$\begingroup\$ it curves "becuz of the math", you can do the hole calculation from start to end in one go if you wanted to. if you dont stop it at some point it keeps on going. i hope this was helpfull to you \$\endgroup\$
    – rolen123
    Jun 25, 2013 at 12:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .