Java 使用LibGDX反弹另一个圆的圆
在我的游戏中,我有一系列的圆,形成一个圆形的边界,在边界内我有一个球应该反弹,我希望球从任何一个边界圆上反弹。问题是球没有反弹,它只是直接穿过。这是我的密码:Java 使用LibGDX反弹另一个圆的圆,java,libgdx,Java,Libgdx,在我的游戏中,我有一系列的圆,形成一个圆形的边界,在边界内我有一个球应该反弹,我希望球从任何一个边界圆上反弹。问题是球没有反弹,它只是直接穿过。这是我的密码: private Array<Circle> colCircles = new Array<Circle>(); // circle array // method to calculate circle position private Vector2 calculatePosition(int i) {
private Array<Circle> colCircles = new Array<Circle>(); // circle array
// method to calculate circle position
private Vector2 calculatePosition(int i) {
float cx = Constants.CENTER_X;
float cy = Constants.CENTER_Y;
float angle = 4 * i;
float x = (float) (cx + m * Math.cos(Math.toRadians(angle)));
float y = (float) (cy + m * Math.sin(Math.toRadians(angle)));
return new Vector2(x, y);
}
当球和任何圆之间发生碰撞时,我使用此代码尝试反弹,但没有反弹帮助!:
public void bouncer(Ball b){
// original velocity vector
Vector2 v1 = new Vector2(b.getVelocity().x, b.getVelocity().y);
// normal vector
Vector2 n = new Vector2(
Constants.CENTER_X - b.getPosition().x,
Constants.CENTER_Y - b.getPosition().y
);
// normalize
if (n.len() > 0) n = n.nor();
// dot product
float dot = v1.x * n.x + v1.y * n.y;
// reflected vector values
v2.x = v1.x - 2 * dot * n.x;
v2.y = v1.y - 2 * dot * n.y;
// set new velocity
b.setVelocity(v2);
}
在update()
方法中,通过方向向量和速度量计算速度
所以改变方向而不是速度
direction.rotate(180); // for centric bounce
另一方面,若你们想要一些偏差,计算中间点和球位置之间的角度,并使用一些随机偏差
direction.setAngle(angle-180+ MathUtils.random(-18,18));
direction.nor();
在update()
方法中,通过方向向量和速度量计算速度
所以改变方向而不是速度
direction.rotate(180); // for centric bounce
另一方面,若你们想要一些偏差,计算中间点和球位置之间的角度,并使用一些随机偏差
direction.setAngle(angle-180+ MathUtils.random(-18,18));
direction.nor();