【问题标题】:collision detection not working right碰撞检测无法正常工作
【发布时间】:2011-07-25 18:43:40
【问题描述】:

好的,所以我的桨碰撞工作正常:

if(velo.y > 0){
            float t = ((position.y - radius) - paddle.position.y)/ velo.y;

            float ballHitX = position.x + velo.x * t;

            if(t <= 1.0){
                if(ballHitX >= paddle.position.x && ballHitX <= paddle.position.x + paddle.width){
                    velo.y = -velo.y;
                }
            }
        }

但我的墙壁碰撞不是。 (球在桨下时向上,不在时向下)

if(velo.y < 0){
                float t = ((position.y - radius) - (wall[2].y + wall[2].height))/ velo.y;

                if(t <= 1.0){
                velo.y = -velo.y;
                }
            }

我如何停止这个错误并让它从墙上反弹?

【问题讨论】:

    标签: java math trigonometry collision


    【解决方案1】:

    我猜你翻了两次。

    if(wall) {
        velo = -velo;
    }
    if(paddle) {
        velo = -velo;
    }
    

    所以,当你做桨时,它是这样的:

    am i hitting the wall? nope
    am i hitting the paddle? yep! okay flip velocity
    

    但是当你做你的墙时,它是这样的:

    am i hitting the wall? yep! okay flip velocity
    am i hitting the paddle? yep! okay flip velocity
    

    所以因为你同时满足了这两个条件,所以它翻转了两次。

    您需要确定您是否已经翻转,以防止重复翻转。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多