Add screen edge reflection.

This commit is contained in:
Matt Low 2018-11-11 15:07:29 +04:00
parent 66bd930e2e
commit 79f4334cd7
3 changed files with 33 additions and 3 deletions

View File

@ -3,6 +3,7 @@ package com.me.brickbuster;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.math.Vector2;
import com.me.brickbuster.entity.Ball;
import com.me.brickbuster.entity.Block;
import com.me.brickbuster.entity.Paddle;
@ -15,6 +16,11 @@ public class BrickBuster extends ApplicationAdapter {
public static final int HEIGHT = 600;
public static final String TITLE = "Brick Buster";
public static final Vector2 RIGHT_EDGE = new Vector2(0, 1);
public static final Vector2 TOP_EDGE = new Vector2(-1, 0);
public static final Vector2 LEFT_EDGE = new Vector2(0, -1);
public static final Vector2 BOTTOM_EDGE = new Vector2(1, 0);
Ball ball;
Paddle paddle;
ArrayList<Block> blocks;

View File

@ -0,0 +1,12 @@
package com.me.brickbuster;
import com.badlogic.gdx.math.Vector2;
public class Utils {
public static Vector2 reflect(Vector2 incoming, Vector2 surface) {
Vector2 normal = new Vector2(-surface.y, surface.x);
return incoming.sub(normal.scl(normal.dot(incoming) * 2));
}
}

View File

@ -5,6 +5,7 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.me.brickbuster.BrickBuster;
import com.me.brickbuster.Utils;
public class Ball extends Entity {
@ -31,11 +32,22 @@ public class Ball extends Entity {
if (!getBrickBuster().isPlaying()) {
return;
}
int newX = getX() + (int) (direction.x * speed);
int newY = getY() + (int) (direction.y * speed);
if (newX + RADIUS > BrickBuster.WIDTH) {
Utils.reflect(direction, BrickBuster.RIGHT_EDGE);
} else if (newY + RADIUS > BrickBuster.HEIGHT) {
Utils.reflect(direction, BrickBuster.TOP_EDGE);
} else if (newX - RADIUS < 0) {
Utils.reflect(direction, BrickBuster.LEFT_EDGE);
} else if (newY - RADIUS < 0) {
Utils.reflect(direction, BrickBuster.BOTTOM_EDGE);
}
setX(getX() + (int) (direction.x * speed));
setY(getY() + (int) (direction.y * speed));
/*if (this.getX() + RADIUS < BrickBuster.WIDTH) {
this.setX(this.getX() + 1);
}*/
}
public void launch() {