【问题标题】:Ball reflection of a wall at an angle not working墙壁的球反射角度不起作用
【发布时间】:2014-07-13 14:47:21
【问题描述】:

我正在尝试让球在墙上反弹。墙壁可以是任意角度,球可以任意角度撞击墙壁。球有一个速度矢量。我已经计算并归一化了球碰撞的墙壁的法线。下面这段代码是我用来计算点积的:

public float dotProduct(Vector2 v1, Vector2 v2){
    float theta1 = (float) (Math.atan(v1.getY()/v1.getX()));
    float theta2 = (float) (Math.atan(v2.getY()/v2.getX()));
    float alpha = theta1 - theta2;
    float v1Magnitude = (float) Math.sqrt(Math.pow(Math.abs(v1.getX()), 2) + Math.pow(Math.abs(v1.getY()), 2));
    float v2Magnitude = (float) Math.sqrt(Math.pow(Math.abs(v2.getX()), 2) + Math.pow(Math.abs(v2.getY()), 2));
    return (float) Math.abs((v1Magnitude * v2Magnitude * Math.cos(alpha)));
}

由于某种原因,球只是以高速向某个奇怪的方向反弹。这是计算反射速度的主要函数:

startX、startY、stopX 和 stopY 是墙的坐标。

   public Vector2 calculateReflection(float startX, float startY, float stopX, float stopY){
    Vector2 normal;

    normal = new Vector2(-(stopY - startY), (stopX - startX));

    normal = normalize(normal);

    float velocityDotProduct = dotProduct(velocity, normal);
    Vector2 reflectionVelocity = new Vector2(velocity.getX() - 2*velocityDotProduct*normal.getX(), 
                                             velocity.getY() - 2*velocityDotProduct*normal.getY());

    return reflectionVelocity;
}

有人知道我做错了什么吗?

【问题讨论】:

  • 我假设您使用了正确的方程式?如果不确定,您可能需要与Physics Stack Exchange 联系。
  • 您在创建reflectionVelocity 时是否有机会切换了构造函数的参数(即第一个参数不应该是x 而第二个是y)?

标签: java math collision dot-product


【解决方案1】:

从 A(ax,ay) 到 E(ex,ey) 的墙的法线向量应该是

-(ey - ay), ex - ax

public Vector calculateReflection( Vector velocity){
    double velocityDotProduct = Vector.dotProduct(normal, velocity);
    Vector reflectionVelocity = 
       new Vector(velocity.getX() + 2*velocityDotProduct*normal.getX(),
       velocity.getY() + 2*velocityDotProduct*normal.getY());
    return reflectionVelocity;
}
public void normalize(){
    double d = Math.sqrt( x*x + y*y );
    x /= d;
    y /= d;
}

public static double dotProduct(Vector v1, Vector v2){
    double res = v1.getX()*v2.getX() + v1.getY()*v2.getY();
}

public class Reflect {
private Vector normal;

public Reflect(double begX, double begY, double endX, double endY ){
    normal = new Vector(-(endY - begY), endX - begX);
    normal.normalize();
    System.out.println( "normal: " + normal );
}

public Vector calculateReflection( Vector velocity){
     double velocityDotProduct = Vector.dotProduct(normal, velocity);
     Vector reflectionVelocity = 
       new Vector(velocity.getX() - 2*velocityDotProduct*normal.getX(),
           velocity.getY() - 2*velocityDotProduct*normal.getY());
     return reflectionVelocity;
}

【讨论】:

  • 天哪,我明白了。编码的时候我一定是睡着了,哈哈。另外,您使用的那个等式中的“b”是什么?
  • b 喜欢弹跳球中的 b。
  • n 的方向不清楚,因为您可以交换 A 和 E。我不知道如何解决这个问题,除非通过计算两个备选方案并检查 |b'| = |b|。 (我的力学课太远了。)
  • 我改变了正规方程并交换了反射速度中的 X 和 Y。我不知道如何在代码中实现带有 b 的方程。到目前为止,我有:
  • 代码格式不正确。我提出一个新问题怎么样?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多