Hi there, just finished the adding features to this task (bouncing balls). So I’ll appreciate any opinions on it and there are a few lines in the code which caught my interest. The lines in main.js from 70 through 83 where the ‘collisionDetect’ is realized. I can’t comprehend how the process can distinct ‘this’ from the ‘ball’ in this particular instance. I mean isn’t ‘this’ actually ‘the ball’? Obviously the cycle (for … of) picks particular ‘ball’ up and checks it, so there is no other ‘ball’ than that checked at the moment. Following the mere logic “this” is actually “the ball” both being the same element. I apologize for this mess. The entire code works, the balls are bouncing, the “Evil Circle” swallows the balls, but I’m still surprised by this code portion ))))
Great job, @texas-triumph!
As far as I can see everything is correct.
Whenever you have a boolean like ball.exists
inside a condition you can just leave out === true
.
if (ball.exists) {...}
is enough.
Regarding your question about collisionDetect()
:
Remember that this isn’t a global function but a method of the “Ball” class. This means each one of the 25 balls has this method and inside the loop on line 198 all 25 collisionDetect()
methods are getting called. Now when we look inside this method on line 70 we see another loop over all balls. This means there are 25 * 25 checks. Since it’s a method of the “Ball” class, the ball calling it is “this”.
For example: In the fifth iteration of the loop (line 194), the fifth ball’s collisionDetect()
method gets called. During its call the fifth ball is “this” and gets compared to all the other balls. There’s also an if
condition on line 71 to prevent the ball to be compared to itself or non-existent balls.
Was that more or less understandable?
Stay safe,
Michael
Hello Michael,
Many thanks for your clarifications on the «this vs ball» issue.
Seems like my approach was somewhat shallow and childish. I consciously followed the instructions and worked within separate portions of the code but didn’t see the entire picture.
A simple analysis of the entire code would have sufficed yet I failed to do that and simply grabbed on the ‘ball’ term )))
So, as I see now on the line 198 I pick the ball from the array of preconstructed balls and check its collisions by running a completely different cycle (line 70) where I first of all exclude this ball from the rest of the balls (since it definitely is destined to collide with itself )))) and then check it’s position relative to the rest of the balls. Then we get back to the line 198 pick the next ball up and use the same method on the line 70.
Thank you kindly for your attention to my question, Michael. I appreciate it.
Andrew.
You’re welcome.
Yes, that’s exactly how the information flows in the code.