Realistic ball bounce (off line) in a coordinate grid

Problem

So I'm trying to make a program that makes the ball bounce off a line by calculating the angle the ball would make to the line - if it were an intersecting line - and rotating that line around the intersection to find a new slope. I have all the algorithms and formulas for everything except the part where I pass the slope back to the ball's momentum. The end product of all the calculations (which I did the job) is the slope of the line and the intersection or bounce point. If I only had a slope, how would I know in which direction the ball would move down the slope after it bounced?

Sidenote : The language I'm working in is Java 1.8 with some arbitrary external graphics library, but I'm not looking for code to just plug into my existing code, I'm looking for a general idea of ​​what you think I can do. Also, to solve this problem, the entire project is based on coordinates.

Thanks a lot for any input or possible answer, and ask me if you want the specs of the problem!

+3


source to share


1 answer


www.cwynn.com/bounce is a little html5 canvas thing I did a few years ago that illustrates how op wanted to handle the "bounce"

You have ball B that hits line L at the point of impact C. B-> C makes a line. Take a point on that line outside of C and flip it through L to get a reflection point R. When the ball hits C, you can delete its direction vector and pass it a C-> R vector. However, you need to reset their speed. So, take the dimension of the direction vector and draw a new direction vector to match.

Edit: decided to add some code (which made me realize I forgot to scale my 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);
    }

      

Solved to add an image ...



The only "real" things are the ball and the brown line

The "ball" is pink, directed to the point of "contact" in the center along the "path" ray,

Projection is the reflection of the ball at the contact point, and reflection is the reflection of the projection point along the line.

As soon as the ball contacts the brown line, the direction vector should change from "path" to "new path" (the line that is on contact and reflection).

enter image description here

+3


source







All Articles