Home > Software engineering >  Gravity in two lib-gdx dimensions
Gravity in two lib-gdx dimensions

Time:05-02

I’m developing an application inspired by the first ping pong games. So there are simply two bars and one ball and two players can challenge each other by moving these bars with their finger. The application is for android phones and developed in java.

I used the gdx lib framework

This is the style of the game

Style Example

It all seemed simple until I got to the point of having to develop the gravity of the ball.

Making the gravity of a 2d game didn’t seem too complex, but it turned out to be quite difficult. In fact to develop the game after several attempts I set a random gravity. In a nutshell the ball does not follow a logic but moves towards a random direction with respect to the side of the screen that hits.

The result is this, a function that generates random numbers in few words.

public void gravity(){
        switch (gravitylevel){
            case 1:
                if(gravitychanche){
                    gravity.setXincrement(rand.getX());
                    gravity.setYincrement(rand.getY());
                }
                else{
                    gravity.setXdecrement(rand.getX());
                    gravity.setYincrement(rand.getY());
                }
                break;
            case 2:
                if(gravitychanche){
                    gravity.setXdecrement(rand.getX());
                    gravity.setYdecrement(rand.getY());
                }
                else{
                    gravity.setXincrement(rand.getX());
                    gravity.setYdecrement(rand.getY());
                }
                break;
            case 3:
                if(gravitychanche){
                    gravity.setXdecrement(rand.getX());
                    gravity.setYdecrement(rand.getY());
                }
                else{
                    gravity.setXdecrement(rand.getX());
                    gravity.setYincrement(rand.getY());
                }
                break;
            case 4:
                if(gravitychanche){
                    gravity.setXincrement(rand.getX());
                    gravity.setYincrement(rand.getY());
                }
                else{
                    gravity.setXincrement(rand.getX());
                    gravity.setYdecrement(rand.getY());
                }
                break;
        }
    }

This code is very rudimentary, however when I tried to reproduce a fairly realistic gravity on lib gdx, all the guides forced me to do a gravity for a three-dimensional game. Is there a way to create a not too complex two-dimensional gravity on lib gdx?

CodePudding user response:

You don't realy need gravity for this. It's much easier to just use a velocity vector for the ball. If the ball hits a wall or a player's bar you just have to invert either the x or y component of the velocity.

Something like this (simplified):

public class Game {
  
  private Ball ball;
  
  public void spawnBall() {
    ball = new Ball();
    ball.velocity = new Vector2(Math.random(), Math.random()); // init with a random vector
  }

  public void moveBall() {
    ball.position = ball.position.add(ball.velocity); // move the ball by it's velocity
  }

  public void changeBallDirection() {
    if (ballHitPlayerBar()) {
      ball.velocity.x *= -1; // invert x direction
    }
    else if (ballHitTopOrBottomOfField()) {
      ball.velocity.y *= -1; // invert y direction
    }

    // TODO maybe move the ball a bit, to prevent it from touching the bar or the field border multiple times...
  }
}
  • Related