+ 8

Messed up Collision

I have used Pythagorean Theorem to program collision detection. Although the collision has now freaked out and nothing is working. The ball shaked rapidly everytime it hits the other ball or the edge. Can someone help? https://code.sololearn.com/WKOqwX3cLJDV/?ref=app

16th Jun 2019, 8:20 AM
Clueless Coder
Clueless Coder - avatar
12 Respostas
+ 4
1. Quick Fix : https://code.sololearn.com/WyNTONozFjXl/?ref=app 2. explanation : 2.1 initilization: don't set move as `if left x - = xSpeed` (my line 44), your xSpeed sign tells direction, so update it instead. (my line 29, 30). 2.2 don't simply *=-1 everything when balls collide, there are four cases to consider: whether xDirSame or not and whether yDirSame or not. 2.2.1 use Math.sign() function (my line 95 96) 2.2.2 when the directions are different, both bounces back. (my line 103 to 114) 2.2.3 when the directions are the same, the faster one is chasing the slower one, the faster one bounces back. (my line 116 to 122) 3. this is just a simple fix on logic, still not real physics. to simulate reality, you need to : 3.1 calculate the line of two ball centers. calculate the incision angle and the reflection angle for each ball. 3.2 considering the effects on the velocities. p. s. thanks marjel101 for mentioning =)
16th Jun 2019, 10:39 AM
Gordon
Gordon - avatar
+ 9
i think it’s because of the way the collision detection works, it just keeps changing the direction of the ball and the ball appears to be stuck. I had the same problem with a code I made a while ago. Gordon helped me a lot with that issue. https://code.sololearn.com/WM2x10n1vy1A/?ref=app Look for the comments by Gordon. These might be helpful for you too...
16th Jun 2019, 9:37 AM
marjel101
marjel101 - avatar
+ 8
☕__JavaScripter__💐 it looks okay now. The ball bounces of the walls as expected. The backwards and forwards bouncing occurs only when the balls bounce against eachother. I have no idea how to solve that, I have been struggling with that myself. The mathematics for bouncing balls is probably different than from bounces against a flat surface...
16th Jun 2019, 10:03 AM
marjel101
marjel101 - avatar
+ 7
☕__JavaScripter__💐 Maybe I gave you wrong advice. I checked again my own collision code. I included a collide method in the Ball class with just 1 argument, a generic object that comes from another collison detection function. https://code.sololearn.com/WM2x10n1vy1A/?ref=app From line 33 is the collide method of the ball, and in line 202 the collision detection that checks the collision with all blocks (in your case these could be the paddles). Hope this helps... đŸ€”
16th Jun 2019, 5:53 PM
marjel101
marjel101 - avatar
+ 6
☕__JavaScripter__💐 I’m not sure you can reference the paddles from inside the Ball class. You need an independent function to detect a collision between the ball and the paddle, with two arguments (ball and paddle) I think.
16th Jun 2019, 5:05 PM
marjel101
marjel101 - avatar
+ 5
Thanks marjel101 Gordon @Josh Creig, Théophile !
16th Jun 2019, 10:47 AM
Clueless Coder
Clueless Coder - avatar
+ 4
Be careful, when there is y collision, you change xSpeed, but only yspeed must be changed.
16th Jun 2019, 9:09 AM
Théophile
Théophile - avatar
+ 3
When you check collision, there is no else if, only another if to check y position.
16th Jun 2019, 8:35 AM
Théophile
Théophile - avatar
+ 3
marjel101 I've done some tweaking, do you think it's ready to be published? Edit: On your code, when the ball hits the square, yours seems to bounce off at the same angle on the opposite side, while mine goes backwards and forwards. We used the same technique (*= -1) but mine isn't working somehow đŸ€”
16th Jun 2019, 9:42 AM
Clueless Coder
Clueless Coder - avatar
+ 3
16th Jun 2019, 3:23 PM
Clueless Coder
Clueless Coder - avatar
+ 2
Sorry, it still isn't working
16th Jun 2019, 9:07 AM
Clueless Coder
Clueless Coder - avatar