Jump to content

2 ball collision (2d plane)

Spartaton

I am not concerned with conservation of momentum, just the angle it is a game in space and all balls travel the same speed and are the same size anyways

Hey I'm currently making a game with MIT's app inventor (so I'll have some limited functionality) and I was wondering if anyone had a better idea to calculate the heading of the balls once they collide. Currently I'm finding the slope of the line between the coordinates of the ball and the raising the the -1 and multiplying by -1 to get the slope of the tangent line to the point of collision. Then depending on ball position (which one is higher, whether they are at the same y value) I run the angle of each ball (respect to the positive x axis) and the angle the tangent line creates through some equations to get the new heading for each ball (it currently boils down to an angle of incidence = angle of reflection off the tangent line which is hopefully realistic enough). So if anyone knows of a better way to do this, it would be much appreciated if you told me. I also will be adding in more balls (there will be 4-8 balls, haven't decided how many to have yet) so if there's a better way to handle it with more balls that might not be as efficient with only 2 please share.

 

The process:

y1 = ball1.y

y2 = ball2.y

x1 = ball1.x

x2 = ball2.x

m = y1 - y2 / x1 - x2

slope of tangent line = m ^ -1 * -1

angle of tangent line = atan2 (1, slope of tangent line)

 

And then 3 different equations all 5 lines long depending on the positioning of the ball ( ball1.y > ball2.y, ball1.y < ball2.y and ball1.y == ball2.y)

 

And then finally setting the new headings for BOTH the balls

Link to comment
Share on other sites

Link to post
Share on other sites

this is the correct way of doing it, i dont think there is anything shorter

but

i dont think you need 3 different equations for each case, the signs in front of the slope and the angle of the slope should be enough to determine the final angles of the balls

try it out and see, might need some tweaking and playing around with the - signs, but i think it can be done with a single equation

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Enderman said:

this is the correct way of doing it, i dont think there is anything shorter

but

i dont think you need 3 different equations for each case, the signs in front of the slope and the angle of the slope should be enough to determine the final angles of the balls

try it out and see, might need some tweaking and playing around with the - signs, but i think it can be done with a single equation

I figured as much. I plan on talking to my physics teacher about it on Monday since I have no education on the physics of balls.

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, Spartaton said:

I figured as much. I plan on talking to my physics teacher about it on Monday since I have no education on the physics of balls.

are you just trying to do perfectly elastic collisions?

because if you are then your code is right

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×