坐标网格中球的实际弹跳(离线)

时间:2015-05-12 01:50:26

标签: java graphics trigonometry physics-engine

问题

所以我试图制作一个程序,通过计算球与线的角度 - 如果它是一条相交线 - 并旋转该线,使球从一条线上反弹找到新斜率的交点。我拥有所有算法和公式,除了我将斜率移回球动量的部分。所有计算的最终产品(我已确定工作)是线的斜率和交点或反弹点。如果我只有一个斜坡,我怎么能在弹跳后知道球在斜坡上的方向?

旁注 : 我正在使用的语言是带有一些任意外部图形库的Java 1.8,但是我没有找到代码只是插入我原先存在的代码,我正在寻找你认为我的想法的一般概念也许能做到。此外,对问题至关重要的是,整个项目都是基于坐标的。

非常感谢任何输入或可能的答案,并问我是否想要解决问题的规格!

1 个答案:

答案 0 :(得分:3)

www.cwynn.com/bounce是几年前我制作的一个小小的html5画布,它说明了op是如何处理“反弹”的

你有你的球B,它将在碰撞点C上击中L线.B-> C形成一条线。在C之外的那条线上取一个点并将其反射到L以获得反射点R.当球击中C时,你可以删除它的方向向量,并给它C-> R的向量。你需要重置他们的速度。因此,抓住方向向量的大小,并缩放新的方向向量以匹配。

编辑:决定添加代码(这让我意识到我忘了缩放速度)

    //closestCollPt[0] is the line the 'player' will hit next
    var dist = player.position.dist(closestCollPt[0]);

    //the 'player' in my case is a circle, so this indicates collision
    if(dist < player.radius*2)
    {
        //the collision point to the reflection like I mentioned
        //in the text above
        var newDir = closestCollPt[0].vecTo(reflection);

        //I break out their parts, b/c I want to scale the vector
        //doable in one line, but hard to debug
        var newDirX = newDir.x;
        var newDirY = newDir.y;
        var newDirDist = newDir.dist(new Vector(0,0));
        //for whatever reason I was calling the size of a vector
        //'dist' when I wrote this


        var currDirDist = player.direction.dist(new Vector(0,0));

        //give the player the direction we got from Coll->Ref
        //scale it so their speed doesn't change
        player.direction = newDir.scale(currDirDist).scale(1/newDirDist);
    }

决定添加图片......

唯一的“真正”的东西是球和棕色线

'球'是粉红色的, 前往射线'路径'中心的“接触”点,

投影是球在接触点上的反射,反射是投影点在线上的反射。

一旦球接触到棕色线,它的方向向量应该从“路径”变为“新路径”(位于接触和反射的线)

enter image description here

相关问题