简体   繁体   中英

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. 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. 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'

You have your ball B, that will hit a line L on collision point C. B->C makes a line. 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. 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)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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