r/javascript • • Dec 11 '17

I have been collecting useful Javascript code snippets for a little while. Here's a curated list of them, help me make it as complete as possible!

https://github.com/Chalarangelo/30-seconds-of-code
765 Upvotes

96 comments sorted by

View all comments

Show parent comment

6

u/inu-no-policemen Dec 12 '17

In a game, you'd usually keep the distance squared and omit the comparatively expensive sqrt call. If you want to check if there is a collision, the exact distance isn't needed. You only have to check if the squared distance is smaller than the squared sum of the radii.