想使一个物体反弹一个圈子里面,最终使得沿着圆的边缘物体移动 [英] Wanted to make an object bounce inside a circle, ended up making the object move along the rim of the circle

查看:173
本文介绍了想使一个物体反弹一个圈子里面,最终使得沿着圆的边缘物体移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的code的问题:

Here's the code in question:

public void calculate() {
        // Center of circle is at (250, 250).
        //THIS ALGORITHM IS NOW PROVEN TO BE WORSE THAN I FEARED...

        /*      What it does:
         *          Moves object around in a circle.
         *          Does not move the object towards the center.
         *          Object always stays on the rim of the circle.
         * 
         *      Algorithm I used. (DOES NOT WORK):
         *          N is normalized vector. 
         *          R = -2*(V dot N)*N + V
         */

        vx += Accelero.X * 0.1;
        vy += Accelero.Y * 0.1;
        double nx = x - 250;
        double ny = y - 250;
        double nd = Math.hypot(nx, ny);
        if (nd == 0)
            nd = 1;
        nx /= nd;
        ny /= nd;
        double dotProduct = vx * nx + vy * ny;
        vx += (float) (-2 * dotProduct * nx);
        vy += (float) (-2 * dotProduct * ny);
        x -= vx * 2;
        y -= vy * 2;
        vx *= 0.99;
        vy *= 0.99;
    }

这是什么情况。

And this is what happens.

您看到的黑线就是紫色的物体(盒)移动。碰巧的是正确的圈线我画与 Canvas.drawCircle()

The black line you see is where the purple object (box) moves. It just so happens to be right on the circle line I drew with Canvas.drawCircle().

我不明白为什么反射没有工作。如果对象是打一个圆形的墙壁,应该不是反映速度的物体的方向,这是什么算法的意思是什么?或者我用了错误的算法?

I don't understand why reflection didn't work. If an object is to hit a circular wall, shouldn't it reflect the object's direction of velocity, which is what the algorithm was meant to be? Or I used the wrong algorithm?

任何帮助是AP preciated。先谢谢了。

Any help is appreciated. Thanks in advance.

推荐答案

这是我所得到的,我想分享我的发现到所有的人。

This is what I've got, and I'm going to share my findings to all of you.

public void calculate() {
    // Center of circle is at (250, 250). Radius is 40.
    //THIS ALGORITHM IS PROVEN TO BE BETTER THAN I FEARED...

    /*      What it does:
     *          Moves object around in a circle, if object is 
     *              inside of circle.
     *          Does not move the object towards the center, 
     *              nor outwards. This is crucial.
     *          Object always stays on the rim of the circle, 
     *              if the collision detection allows it to.
     * 
     *      Algorithm I used. (DOES WORK, NOT EXPECTING THIS THOUGH.):
     *          N is normalized vector. 
     *          R = -2*(V dot N)*N + V
     */



    double nx = x - 250;
    double ny = y - 250;
    double nd = Math.hypot(nx, ny);
    if (nd < 40){
        vx += Accelero.X * 0.1;
        vy += Accelero.Y * 0.1;
        x -= vx;
        y -= vy;
        vx *= 0.9;
        vy *= 0.9;
        return;
    }

    vx += Accelero.X * 0.1;
    vy += Accelero.Y * 0.1;


    if (nd == 0)
        nd = 1;
    nx /= nd;
    ny /= nd;
    double dotProduct = vx * nx + vy * ny;
    vx += (float) (-2 * dotProduct * nx);
    vy += (float) (-2 * dotProduct * ny);
    x -= vx * 2;
    y -= vy * 2;
    vx *= 0.99;
    vy *= 0.99;
}

我的嵌入式碰撞检测我的功能在里面,基本上使这个功能不是尽可能提高效率。忽略的是,它不是主要焦点。

I embedded a collision detection inside my function, basically making this function not as efficient as possible. Ignore that, for it's not the main focus.

圆的半径是40,(X,Y)的位置是(250250)。

The circle's radius is 40, the (x,y) position is (250,250).

只有当对象是在圆,或进一步远离圆心,我们应该计算碰撞响应,这是由该算法R = -2 *(V点N)* N + V给出,其中,法向矢量N是已经标准化。

Only when the object is either on the circle, or further away from the center of circle, should we calculate the collision response, which is given by the algorithm R = -2*(V dot N)*N + V, where normal vector N is already normalized.

该算法确实是正确的,这是我的碰撞检测的布尔条件是什么原因导致留在圆圈的边缘对象,去圆一个回合就可以了。

The algorithm is indeed correct, it's the boolean condition of my collision detection is what causes the object to stay on the rim of the circle and go round-a-bout on it.

我没有说其他的算法,它@trashgod提供了是错误的。这是因为一些奇怪的问题,不知怎的,导致异常移动的对象。我想这是我使用的错,它没有让双打的API,但我可能是不正确的。我就是无法找到问题的根源。这我也高兴不考虑进一步了。

I didn't say the other algorithm, which @trashgod had provided is wrong. It's because of some weird issue that somehow causes the object to move unusually. I would guess it's the API I'm using's fault, which it didn't allow doubles, but I may be incorrect. I just couldn't find the source of the problem. Which I'm also happy to not look into it further anymore.

碰撞检测布尔条件本身可以改变一切,如果有人稍有改变。如果不是因为@牛米。指点,我莫名其妙地似乎忘记了一个减号(在这种情况下,不签),我可能永远不会意识到这将是多么微不足道的是。

The collision detection boolean condition itself could change everything, if it was slightly altered. If it wasn't for @n.m. pointing that I somehow seemed to forget a minus sign (in this case, a NOT sign), I probably would never realized how trivial it would be.

这篇关于想使一个物体反弹一个圈子里面,最终使得沿着圆的边缘物体移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆