简体   繁体   English

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

[英]Realistic Bounce of Ball (off Line) in Coordinate Grid

The Problem问题

So I'm trying to make a program that creates a ball bounce off of a line by calculating the angle that the ball would make with the line -- if it were an intersecting line -- and rotating that line around the intersection point to find the new slope.所以我正在尝试制作一个程序,通过计算球与线形成的角度——如果它是一条相交线——并围绕交点旋转那条线来找到一个球从一条线上反弹的程序新的斜坡。 I have all the algorithms and formulas for everything except the part where I hand the slope back to the ball momentum.除了我将坡度返回给球动量的部分之外,我拥有所有algorithms和公式。 The end product of all the calculations (which I have made sure work) is a slope of the line and the intersection, or bounce, point.所有计算的最终产品(我已经确定有效)是线和交点或反弹点的斜率。 If I only have a slope, how would I be able to tell which direction the ball would be traveling on the slope after the bounce?如果我只有一个斜坡,我怎么能知道弹跳后球会在斜坡上移动的方向?

Sidenote : The language I am working in is Java 1.8 with some arbitrary external graphics library , but I'm not looking for code to plug into my preexisting code, I'm looking for a general idea of what you think I might be able to do.旁注:我正在使用的语言是带有一些任意外部graphics library Java 1.8 ,但我不是在寻找代码来插入我预先存在的代码,我正在寻找您认为我可能能够做到的一般概念做。 Also, critical to the problem, the entire project is coordinate-based.此外,问题的关键在于,整个项目都是基于坐标的。

Thanks a lot for any input or possible answer, and ask me if you would like specifications on the problem!非常感谢您的任何输入或可能的答案,并询问我是否需要有关该问题的规范!

https://cwynn.com/ball-bounce/ is a little html5 canvas thing I made a few years ago that illustrates how op was wanting to handle the 'bounce' https://cwynn.com/ball-bounce/是我几年前制作的一个 html5 画布小东西,它说明了 op 如何处理“反弹”

You have your ball B, that will hit a line L on collision point C. B->C makes a line.你有你的球 B,它将在碰撞点 C 上击中一条线 L。B->C 形成一条线。 Take a point on that line beyond C and reflect it across L to get the Reflection Point R. When the ball hits C you can delete it's direction vector, and give it the vector of C->R.在 C 以外的那条线上取一个点并将其反射到 L 上以获得反射点 R。当球击中 C 时,您可以删除它的方向向量,并赋予它 C->R 的向量。 You need to reset their speed though.不过,您需要重置他们的速度。 So grab the size of the direction vector, and scale the new direction vector to match.所以抓住方向向量的大小,并缩放新的方向向量以匹配。

Edit: Decided to add the code (which made me realize I forgot scaling their speed)编辑:决定添加代码(这让我意识到我忘记了他们的速度)

    //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);
    }

Decided to add an image...决定加个图。。。

The only 'real' things are the ball and the brown line唯一“真实”的东西是球和棕线

The 'ball' is pink, headed towards 'contact' point in center on ray 'path', “球”是粉红色的,朝向射线“路径”中心的“接触”点,

Projection is the reflection of the ball across contact point, and reflection is the reflection of the projection point across the line.投影是球穿过接触点的反射,反射是投影点穿过直线的反射。

Once the ball contacts the brown line it's direction vector should change from 'path' to 'new path' (line that sits on contact and reflection)一旦球接触到棕色线,它的方向向量应该从“路径”变为“新路径”(位于接触和反射上的线)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM