Compare commits

...

2 Commits

Author SHA1 Message Date
0a071e40d2 Render bricks first 2018-11-11 18:20:21 +04:00
8f788851b2 Add paddle collision detection
Change tracking of entity positions from int to float
2018-11-11 18:20:07 +04:00
4 changed files with 32 additions and 12 deletions

View File

@ -45,12 +45,12 @@ public class BrickBuster extends ApplicationAdapter {
Gdx.gl.glClearColor(0.5f,1,1,1); Gdx.gl.glClearColor(0.5f,1,1,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
ball.render();
paddle.render();
for (Block block : blocks) { for (Block block : blocks) {
block.render(); block.render();
} }
ball.render();
paddle.render();
} }
public void update(float dt) { public void update(float dt) {

View File

@ -1,5 +1,6 @@
package com.me.brickbuster; package com.me.brickbuster;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
public class Utils { public class Utils {
@ -9,4 +10,11 @@ public class Utils {
return incoming.sub(normal.scl(normal.dot(incoming) * 2)); return incoming.sub(normal.scl(normal.dot(incoming) * 2));
} }
public static Vector2 nearestPoint(Vector2 linep1, Vector2 lineDir, Vector2 p1) {
float lineLen = lineDir.len();
lineDir.nor();
Vector2 ballDir = p1.sub(linep1);
return linep1.add(lineDir.nor().scl(MathUtils.clamp(ballDir.dot(lineDir), 0, lineLen)));
}
} }

View File

@ -6,6 +6,7 @@ import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.me.brickbuster.BrickBuster; import com.me.brickbuster.BrickBuster;
import com.me.brickbuster.Utils; import com.me.brickbuster.Utils;
import net.dermetfan.utils.Pair;
public class Ball extends Entity { public class Ball extends Entity {
@ -33,8 +34,8 @@ public class Ball extends Entity {
return; return;
} }
int newX = getX() + (int) (direction.x * speed); float newX = getX() + (direction.x * speed);
int newY = getY() + (int) (direction.y * speed); float newY = getY() + (direction.y * speed);
if (newX + RADIUS > BrickBuster.WIDTH || newX - RADIUS < 0) { if (newX + RADIUS > BrickBuster.WIDTH || newX - RADIUS < 0) {
Utils.reflect(direction, BrickBuster.VERTICAL_EDGE); Utils.reflect(direction, BrickBuster.VERTICAL_EDGE);
@ -44,8 +45,19 @@ public class Ball extends Entity {
reset(); reset();
} }
setX(getX() + (int) (direction.x * speed)); if (direction.y < 0) {
setY(getY() + (int) (direction.y * speed)); Pair<Vector2, Vector2> paddle = getBrickBuster().getPaddle().getTopEdge();
Vector2 ball = new Vector2(newX, newY);
Vector2 lineDir = paddle.getValue().sub(paddle.getKey());
Vector2 nearest = Utils.nearestPoint(paddle.getKey().cpy(), lineDir, ball.cpy());
if (nearest.dst(newX, newY) < RADIUS) {
Utils.reflect(direction, lineDir.nor());
}
}
setX(getX() + (direction.x * speed));
setY(getY() + (direction.y * speed));
} }
public void launch() { public void launch() {

View File

@ -7,7 +7,7 @@ public abstract class Entity {
private ShapeRenderer shapeRenderer; private ShapeRenderer shapeRenderer;
private BrickBuster brickBuster; private BrickBuster brickBuster;
private int x, y; private float x, y;
public Entity(BrickBuster brickBuster, int x, int y) { public Entity(BrickBuster brickBuster, int x, int y) {
this.shapeRenderer = new ShapeRenderer(); this.shapeRenderer = new ShapeRenderer();
@ -28,19 +28,19 @@ public abstract class Entity {
return shapeRenderer; return shapeRenderer;
} }
public int getX() { public float getX() {
return x; return x;
} }
public void setX(int x) { public void setX(float x) {
this.x = x; this.x = x;
} }
public int getY() { public float getY() {
return y; return y;
} }
public void setY(int y) { public void setY(float y) {
this.y = y; this.y = y;
} }