[LibGDX] Ball bouncing problem

I wanted to make like pinball style game ,but i wanted when ball hits that rectangle stuff sides, then ball changes direction. Or when ball hits that stuff then ball doesn’t change direction it just goes up.Can someone help me how to do it.

Yeah there is code of my small project ;D

package com.me.mygdxgame;

import java.util.Random;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;

public class GameScreen implements Screen {

	final MyGdxGame game;
	OrthographicCamera camera;
	Texture gro;
	Texture ball;
	Rectangle bal;
	Sound ballhit;
	Random sin;
	Random cos;
	Texture glass;
	Rectangle glas;
	boolean hit = false;
	boolean hitglas = false;
	int center = 0;
	int left = 0;
	int right = 0;
	public GameScreen(final MyGdxGame game) {
		this.game = game;
		camera = new OrthographicCamera();
		camera.setToOrtho(false,800,400);
		
		gro = new Texture(Gdx.files.internal("main.png"));
		ball = new Texture(Gdx.files.internal("ball.png"));
		bal = new Rectangle();
		ballhit = Gdx.audio.newSound(Gdx.files.internal("ballhit.wav"));
		glass = new Texture(Gdx.files.internal("glass.png"));
		sin = new Random();
		cos = new Random();
		glas = new Rectangle();
		
		bal.x = 800/2;
		bal.y = 400/2;
		
		bal.width = 64;
		bal.height = 64;
		
		glas.width = 128;
		glas.height = 32;
		glas.x = 350;
		glas.y = 20;
		
	}
	
	public void render(float delta) {
	Gdx.gl.glClearColor(0, 0, 0, 1);
	Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
	
	camera.update();
	game.batch.setProjectionMatrix(camera.combined);
	game.batch.begin();
	game.batch.draw(gro,0,0);
	game.batch.draw(ball,bal.x,bal.y);
	game.batch.draw(glass,glas.x,glas.y);
	game.batch.end();
	
	if(hitglas){
		if(center == 1) {
			bal.y += 10;
			bal.x += 1;
			if(bal.y > 300) {
				hitglas = false;
				center = 0;
			}
		}else  if(left == 1) {
			bal.y += 10;
			bal.x -= 2;
			if(bal.y > 300) {
				hitglas = false;
				left = 0;
			}
		}else if(right == 1) {
			bal.y += 10;
			bal.x += 2;
			if(bal.y > 300) {
				hitglas = false;
				right = 0;
			}
		}
			}else if(bal.overlaps(glas)){
				float balx = bal.getX();
				float glasx = bal.getX();
	if(balx > glasx) {
		left = 1;
		hitglas = true;
	}else if(balx == glasx) {
		center = 1;
		hitglas = true;
	}else if(balx < glasx) {
		bal.y += 20;
		right = 1;
		hitglas = true;
	
	}
		}else  if(hit) {
		bal.y += 10;
		bal.x -= 1;
		if(bal.y > 300) {
			hit = false;
		}
	}else if(bal.y < 0) {
		bal.y = 0;
		hit = true;
	}else if(bal.x < 0) {
		bal.x = 0;
	}else if(bal.x > 800-64) {
		bal.x = 800-64;
	}else if(bal.y > 400-64) {
		bal.y = 400-64;
	}else if(hit == false) {
		bal.y -= 10;
	}else if(hitglas == false) {
		bal.y -= 10;
	}
		
	if(glas.x < 0) {
		glas.x = 0;
	}
	if(glas.x > 800-64) {
		glas.x = 800-64;
	}
	if(Gdx.input.isKeyPressed(Keys.LEFT)) glas.x -= 200 * Gdx.graphics.getDeltaTime();
    
	if(Gdx.input.isKeyPressed(Keys.RIGHT)) glas.x += 200 * Gdx.graphics.getDeltaTime();
	
	}

	@Override
	public void resize(int width, int height) {
		

	}

	@Override
	public void show() {
		

	}

	@Override
	public void hide() {
		

	}

	@Override
	public void pause() {
		

	}

	@Override
	public void resume() {
	

	}

	@Override
	public void dispose() {
	gro.dispose();
	ball.dispose();
	game.batch.dispose();
     
	}

}

Your asking us to explain physics and trajectory. This is a huge subject on its own.

Use box2d, that is probably the most straight forward answer.

Thanks! You just threw all of your code to us, and even without a single comment! :-*

Not that I think that I want a comment since you cant even explain properly what the problem is.
I mean, what are you talking about: ‘rectangle stuff sides’ and ‘that stuff’?

Serious, if you don’t even take the time to explain your problem properly or give us any explanation about your code, you expect us to translate all of your ‘stuff’ and to take the time to explain the answer?

Please be a little more accurate next time than I can maybe help u.
No offense meant by this post.

I am having a hard time reading your code. A little formatting and good variable names will help people read your code.

I would suggest using vectors. Libgdx has a 2d vector class built in. For example:


Vector2 velocity = new Vector2(1, 3).nor().scl(3); // Creates a velocity vector pointing up and to the right with length of 3 (speed of 3)

Using this kind of code you can move the ball a lot easier


// This makes the ball bounce off flat walls
if(collisionRight || collisionLeft) // If there is a collision on the right or left side of the ball
{
     velocity.x = -velocity.x; // Reflect the x velocity
}
else if(collisionTop || collisionBottom) // If there is a collision on the top or bottom of the ball
{
     velocity.y = -velocity.y; // Reflect the y velocity
}

// Then you update the ball's position
ballPosX += velocity.x;
ballPosY += velocity.y;

I would also recommend you use a collision manager so that you can have the ball react differently with different types of obstacles. For example:


// This would make the ball bounce off faster than it hit
if(ball.collides(glass))
{
     velocity.x = -velocity.x * 2;
     velocity.y = -velocity.y * 2;
}

Using a bit of math, you can use vectors with elliptical objects too but if you are wanting collisions with irregular objects then I would suggest using box2d.

Owh soory next time i will add comments.Yeah thanks for help i will check Box2d.