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();
}
}