Could someone assess this code please? And tell me how it could be improved.
When I got to the section “Adding collision detection” in the
“Object building practice” of the module “objects”, see link below.
Right before the explanation of “Adding collision detection” it says: “This method is a little complex,
so don’t worry if you don’t understand exactly how it works for now.”
This paragraph got me curious, so I wanted to fully understand it, which I did,
by the way it took me about a day to understand it.
Then, I decided to write a version of the bouncing balls with only one ball (a star) and the EvilCircle (a black hole) which is pulling the star to eventually eat it. As the star approches the black hole, the horizontal distance (dx), the vertical distance (dy) and the distance from the star to the black hole gets updated along with a triangle formed by dx and dy to calculate the third side (hypotenuse) based on the pythagorean theorem.
Also, I added two buttons:
The “Start” button, which basically does a page refresh
And the “Pause/Continue” toggle button to stop the animation and
give the user an opportunity to manually calculate the distance of
the star from the black hole using the current (dx, dy) dimensions and the pythagorean theorem.
The reason in doing it is to practice what I have learned so far, and to help others to understand faster… I hope, how collision detection is calculated, by seeing it graphically.
I truly hope that it can benefit other beginners and avoid spending a long day like me to figure it out,
also hope that I’m explaining myself.
For some reason, the code runs ok in the browser but in codepen the buttons
don’t work and it runs so weird.
I’d like help to make it run in Codepen as it does in the browser.
Codepen link: https://codepen.io/estalta/pen/OJzvdLK
Thanks in advance,