[英]Collision between ball and rectangles, LibGDX
I'm trying to make a reflection of the ball from the walls and rectangles. 我正在尝试从墙壁和矩形中反射出球。 With walls this code works fine, but with rectangles I have problems. 在墙壁上,此代码可以正常工作,但是在矩形下,我有问题。 It just reflect to the left and then to the right every 1 pixel until collision ends. 它仅向左反射,然后每隔1个像素向右反射,直到碰撞结束。 Example: 例:
what am I doing wrong? 我究竟做错了什么?
Ball: 球:
public class Ball {
private int DEFAULT_SPEED = 2;
private double angle;
private static final int PI = 180;
private int mAngle;
private Rectangle bounds;
private Circle circle;
private Vector2 position;
Player player;
Block block;
public Ball(Vector2 position, Block block) {
this.position = position;
this.block = block;
mAngle = getRandomAngle();
bounds = new Rectangle(position.x, position.y, Gdx.graphics.getWidth() / 20, Gdx.graphics.getHeight() / 15);
circle = new Circle(position.x, position.y, Gdx.graphics.getWidth() / 26);
}
// update moves
public void update() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
bounds.set(position.x, position.y, bounds.getWidth(), bounds.getHeight());
circle.set(position.x, position.y, circle.radius);
double angle = Math.toRadians(mAngle);
position.x += 2*(int)Math.round(DEFAULT_SPEED * Math.cos(angle));
position.y += 2*(int)Math.round(DEFAULT_SPEED * Math.sin(angle));
if(position.x >= Gdx.graphics.getWidth() - circle.radius){
reflectVertical();
} else if(position.x <= 0){
reflectVertical();
}
if(position.y >= Gdx.graphics.getHeight()- circle.radius){
reflectHorizontal();
} else if(position.y <= 0){
reflectHorizontal();
} else if(bounds.overlaps(block.getBounds())){
reflectHorizontal();
System.out.println("BLOCK");
}
}
// update |
public void reflectVertical(){
if(mAngle > 0 && mAngle < PI){
mAngle = PI - mAngle;
} else {
mAngle = 3 * PI - mAngle;
}
}
// update -
public void reflectHorizontal(){
mAngle = 2 * PI - mAngle;
}
private int getRandomAngle() {
Random rnd = new Random(System.currentTimeMillis());
return rnd.nextInt(1) * PI + PI / 2 + rnd.nextInt(15) + 285;
}
public double getAngle() {
return angle;
}
public void setAngle(double angle) {
this.angle = angle;
}
public int getDEFAULT_SPEED() {
return DEFAULT_SPEED;
}
public void setDEFAULT_SPEED(int dEFAULT_SPEED) {
DEFAULT_SPEED = dEFAULT_SPEED;
}
public Circle getCircle() {
return circle;
}
public void setCircle(Circle circle) {
this.circle = circle;
}
public Rectangle getBounds() {
return bounds;
}
public void setBounds(Rectangle bounds) {
this.bounds = bounds;
}
public Vector2 getPosition() {
return position;
}
}
Rectangle: 长方形:
public class Block {
private Rectangle bounds;
private Vector2 position;
public Block(Vector2 position) {
this.position = position;
bounds = new Rectangle(position.x, position.y, Gdx.graphics.getWidth() / 8, Gdx.graphics.getHeight() / 12);
}
public void update() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
bounds.set(position.x, position.y, bounds.getWidth(), bounds.getHeight());
}
public Rectangle getBounds() {
return bounds;
}
public void setBounds(Rectangle bounds) {
this.bounds = bounds;
}
public Vector2 getPosition() {
return position;
}
}
Replace 更换
bounds.set(position.x, position.y, bounds.getWidth(), bounds.getHeight());
with 与
bounds.set(position.x - bounds.getWidth() / 2,
position.y - bounds.getHeight() / 2,
bounds.getWidth(), bounds.getHeight());
Also, 也,
bounds.set(position.x, position.y, bounds.getWidth(), bounds.getHeight());
circle.set(position.x, position.y, circle.radius);
should be at the end of update method. 应该在更新方法的末尾。 This should solve the issue. 这应该可以解决问题。
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.