[Solved]Why Collision Detection Not Working [Code Error] | Sololearn: Learn to code for FREE!

+12

[Solved]Why Collision Detection Not Working [Code Error]

https://code.sololearn.com/W0kM4J5r9WcF/?ref=app i am trying to do fun with collision detection but here in this code sometimes two box colloide and didn't get separate , Like This https://www.sololearn.com/post/1200072/?ref=app How can i separate them?

7/22/2021 4:23:24 AM

Mask Man(What's in a name?)

4 Answers

New Answer

+6

[part 1] The boxes are sticking together because of the way you handle velocities after collision if you just flip the velocities evertime (multiplying by -1) for the boxes which are overlapped by some pixels less than the mag of velocity will beahave normally but for the boxes which overlapped by px more than the magnitude of velocity in both axis this will happen in one frame it will detect collision and flip velocity then Box will move in opposite direction (= mag(velocity)px ) and as the overlap is greater than the mag of velocity boxes are still colliding and hence in next frame it will detect collision again and reverse the velocity again in the wrong side so it becomes same as before.. and this will continue forever that's the reason for that vibration and box sticking together another bug: collision isn't realistic as it flips both axis at the same time. I can explain more if you want

+5

Virtual Pixel Thanks , Now i got it

+3

[part 2] potential fixes : push the boxes apart after detecting collision into opposite direction equal to the amount of overlap so they are apart

+3

One of the solution would be, to use a flag to indicate whether the same collision session has been exited. a demo for another sololearner a few years ago: https://code.sololearn.com/W385n4ccicXX/?ref=app