Glitchy Collision | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Glitchy Collision

I am making a platformer, once the player hits the left/right edge of the platform speed should be set to 0. This prevents the player from going beyond the boundary and stops the scrolling effect. Although the player passes straight through and falls through the screen. And the bottom collision is playing up too. How can I fix this? (Move the player to the blue barrier to see the effect) https://code.sololearn.com/W8WAYyI3DRmP/?ref=app

28th Jul 2019, 11:33 AM
Clueless Coder
Clueless Coder - avatar
12 Answers
+ 3
I am adding a copy of the working setting here: https://code.sololearn.com/WahgRZN1BxTc/?ref=app
28th Jul 2019, 11:54 AM
Gordon
Gordon - avatar
+ 3
Gordon Is this right? I think I've done it right but it still shakes
28th Jul 2019, 12:45 PM
Clueless Coder
Clueless Coder - avatar
+ 3
no what you have done is taking the handler out at line 219, you should look at line 243 instead. in your collision testing, you only check rightward collision and assume non-rightward is left. But non-rightward can be non-leftward too. you should add a second calculation, comparing the left boundary of player and right boundary of platform, and determine whether there is leftward based on the above result.
28th Jul 2019, 1:02 PM
Gordon
Gordon - avatar
+ 3
comparing middle points x distance and y distance with sum of half width and sum of half height respectively. so the collision testing is correct. so the problem is with the handler. √ ... 👌JavaScripter✌️... √ , in the current state of your code, when you are on the platform and walk left, you can pass through the pillar now. Coder is more knowledgeable, i am sure he can help you from here.
28th Jul 2019, 2:52 PM
Gordon
Gordon - avatar
+ 2
1. Quick Answer : At line 61, change from 0.8 to this.gravity = 0.3; 2. Full explanation : 2.1 Your logic is correct 2.2 just the player is too quick and 2.3 at a frame, the whole player has not touched the platform (whole square above and not touching) 2.4 and then the next frame, it jumps pass the platform (whole square below and not touching) 2.5 that's why your collision testing fails.
28th Jul 2019, 11:41 AM
Gordon
Gordon - avatar
+ 2
i see that you made an edit and added an else at line 240, now the logic is totally wrong. Because if right is wrong, it always assume there is a leftward collision; and top/bottom is not an else case for left/right collision, vertical and horizontal collisions are independent to each other.
28th Jul 2019, 11:53 AM
Gordon
Gordon - avatar
+ 2
part 4, i just check your code it is working now but not totally correct the shaking when it lands, is because you only check right collision and assume "not right must left ", you should instead check the left collision explicitly.
28th Jul 2019, 12:05 PM
Gordon
Gordon - avatar
+ 2
Gordon How do I check the left side? Sorry asking so many questions. I was given this by another programmer so I don't know how it works yet
28th Jul 2019, 1:09 PM
Clueless Coder
Clueless Coder - avatar
+ 2
He says he will debug tommorow. I don't want to disturb his studying. #chemistry
28th Jul 2019, 2:56 PM
Clueless Coder
Clueless Coder - avatar
+ 2
Gordon I made a copy of the function so I can modify and understand it. It kinda works apart from left and right https://code.sololearn.com/W8WAYyI3DRmP/?ref=app
29th Jul 2019, 8:02 AM
Clueless Coder
Clueless Coder - avatar
+ 2
√ ... 👌JavaScripter✌️... √ It seems that your last answer is telling me your finding instead of asking about fixing of left/right. If so, Thank you for telling me about your finding. And I am sorry that I gave a wrong answer before. Coder's vector approach works. His collision testing is based on comparing distance between centers and sum of half widths/heights. If you like to know more about the logic of his collision test, please let me know, so I can prepare an explanation
30th Jul 2019, 1:07 AM
Gordon
Gordon - avatar
+ 2
Coder Gordon Thank you so much!
31st Jul 2019, 12:23 PM
Clueless Coder
Clueless Coder - avatar