Collision Not working properly | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Collision Not working properly

Don't know why, but the collision of ball with in the canvas is not behaving properly after some time. I applied all the physics/maths/vector law I know. not working yet r8w9 Gordon Haris @... I think, main problem is on line 59. But I couldn't figure out that problem. https://code.sololearn.com/WHs31clTgtxY/?ref=app

29th Dec 2019, 6:39 PM
Sarthak
Sarthak - avatar
11 Answers
+ 2
sorry, it wasn't a big change, but I did something else meanwhile and forgot about it. Here's the version that doesn't cut the borders: https://code.sololearn.com/W7tEQ1xFdwg4
30th Dec 2019, 5:03 PM
Selin Genkur
+ 6
Sarthak 🇳🇵 look at my code again I made changes to support different angles
30th Dec 2019, 10:05 AM
Burey
Burey - avatar
+ 5
https://code.sololearn.com/W8dySxHpMGik/?ref=app Try messing around with this code Click the canvas to pause/play As you can see, when the ball hit the right wall and coming from bottom to top, it actually hits twice because of the new angle given to it You should calculate the new angle that should be added on a wall hit I suggest the isCollided function will return either 0 or the angle that should be added (you should calculate it in a seperate function) edit: done you can notice when using high velocity the ball may go beyond the wall, but due to the direction check, it won't change direction uncontrolably
29th Dec 2019, 7:18 PM
Burey
Burey - avatar
29th Dec 2019, 8:40 PM
Haris
Haris - avatar
+ 4
You need to address the direction of the ball in the collision calculations as well. What happens is that due to the ball velocity, it sometimes overlaps with the walls more than you'd want. When that happens you are likely to get a positive collisions in a row, and as a result change the direction multiple times. Just increase the velocity and see it occurs much more often
29th Dec 2019, 6:49 PM
Burey
Burey - avatar
+ 3
Sarthak 🇳🇵 yeah I think I know what happens: sometimes, in a corner, it crosses both a vertical and a horizontal boundary, whereas isCollided(), as soon as it finds one of them, returns without checking the other. I changed it to verify both conditions now, so far seems to work well. EDIT: by working well I mean the ball doesn't get lost anymore. If you want it to not cross the lines at all, I'll have to make a little change, which I didn't want to make because it will change the trajectory in a way I didn't want to. https://code.sololearn.com/W6XQEX71cSqa
30th Dec 2019, 12:01 PM
Selin Genkur
+ 2
Yes, you were right. When velocity is 1, everything goes right. But when velocity is higher, the direction of ball sometimes changes twice/more. I'll try to correct it by limiting the velocity before overlapping with walls
29th Dec 2019, 7:13 PM
Sarthak
Sarthak - avatar
+ 2
I tried a simpler fix here following Burey's suggestion that isCollided() could return something: https://code.sololearn.com/W6XQEX71cSqa
30th Dec 2019, 6:54 AM
Selin Genkur
+ 2
Selin Genkur if I change velocity to 50, it doesn't work
30th Dec 2019, 10:01 AM
Sarthak
Sarthak - avatar
+ 2
Burey Selin Genkur The ball still crosses the wall when velocity is increased. I tried to debug the code and the following are the results. https://media.sololearn.com/i/14924647/Screenshot_from_2019-12-30_16-39-57.png https://media.sololearn.com//i/14924647/Screenshot_from_2019-12-30_16-40-10.png
30th Dec 2019, 11:08 AM
Sarthak
Sarthak - avatar
+ 1
Well you have worked on Js...mmm i don't know about that language but i can try to help you with these kinda stuff if you are using Tkinter in python for making any GUI stuff...
31st Dec 2019, 10:36 AM
Tihsrah
Tihsrah - avatar