New MINIMUM_ANGLE constant in Ball, currently PI/16

Apply impulses to ball when angle relative to horizontal axis is less
than MINIMUM_ANGLE. Prevents balls getting stuck going horizontally
This commit is contained in:
Matt Low 2018-11-15 16:17:26 +04:00
parent 998286de8d
commit cb90c949ff

View File

@ -20,6 +20,8 @@ public class Ball extends Entity implements PhysicsBody, CollisionListener {
public static final float BOOST_SPEED = 55;
public static final int BLOCKS_FOR_BOOST = 39;
public static final float MINIMUM_ANGLE = MathUtils.PI/16;
private float speed;
private boolean isStuck = true;
private boolean touchedPaddle = false;
@ -56,11 +58,18 @@ public class Ball extends Entity implements PhysicsBody, CollisionListener {
touchedPaddle = false;
}
if (getY() + RADIUS < 0) {
if (pos.y + RADIUS < 0) {
deleted = true;
return;
}
body.setLinearVelocity(body.getLinearVelocity().nor().scl(speed));
Vector2 velocity = body.getLinearVelocity();
float rad = velocity.angleRad();
if (Math.abs(rad) < MINIMUM_ANGLE || Math.abs(rad) > MathUtils.PI - MINIMUM_ANGLE) {
body.applyLinearImpulse(new Vector2(0, rad > 0? 1 : -1), pos, true);
}
body.setLinearVelocity(velocity.nor().scl(speed));
}
@Override
@ -111,10 +120,9 @@ public class Ball extends Entity implements PhysicsBody, CollisionListener {
}
public Vector2 paddleReflectAngle() {
float trim = state.paddle.getWidth() * 0.10f;
float rel = MathUtils.clamp((pos.x - state.paddle.getX()) + (state.paddle.getWidth()/2),
trim, state.paddle.getWidth()-trim);
float rel = (pos.x - state.paddle.getX()) + (state.paddle.getWidth()/2);
float newAngle = MathUtils.PI - (MathUtils.PI * (rel / state.paddle.getWidth()));
newAngle = MathUtils.clamp(newAngle, MINIMUM_ANGLE, MathUtils.PI-MINIMUM_ANGLE);
return new Vector2(MathUtils.cos(newAngle), MathUtils.sin(newAngle));
}
@ -126,7 +134,6 @@ public class Ball extends Entity implements PhysicsBody, CollisionListener {
// launch at random angle between 135 and 45 degrees
float angle = MathUtils.random(MathUtils.PI/2) + MathUtils.PI/4;
direction = new Vector2(MathUtils.cos(angle), MathUtils.sin(angle));
}
body.setLinearVelocity(direction.scl(speed));
isStuck = false;