We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi everyone, I have finally had time to update a set of collision detection examples I built a few years ago, but it sort of morphed from a simple tutorial repo to an online book with interactive examples:
http://jeffreythompson.org/collision-detection/
Would very much appreciate any feedback, comments, suggestions, typo-finding, etc from everyone before it goes into wide release!
Comments
wow! amazing! I am new to processing so I can't give much feedback but I give you a thank you :)
Thanks for sharing!!! Great work. Congrats.
Indeed it's very detailed & easy to follow. >:D<
However, I'd be gr8 if there was some more directly example codes too.
For example, when dealing w/ circles, we don't really need any sqrt() for collision purposes:
http://jeffreythompson.org/collision-detection/point-circle.php
https://github.com/jeffThompson/CollisionDetection/blob/master/CodeExamples/PointCircle/PointCircle.pde
All of the above can be synthesized in 1
return
expression: *-:)IMO, both the longer & shorter versions should be provided! O:-)
Definitely a balance between simplicity/efficiency and clarity. The example above can definitely be shortened, but it loses readability, especially for new programmers. Hard to know if adding another example will be helpful, or confusing.
For example, this statement in the Polygon/Point example:
Does the same thing as this:
The first version is really clean and super smart, but it even took me a while to understand it. I debated leaving it in, and ultimately decided it was too clever to omit.
dist()
slow and easy....
Yes! I skipped using
dist()
because I want the algorithms to be as clear as possible, step-by-step. It gets a little repetitive with all the Pythagorean theorem, for sure.Did you find it annoying not to use
dist()
?no, it's ok
P.S.:
vc.y > py != vn.y > py
can be replaced w/(vc.y > py) ^ (vn.y > py)
. ;)Although in this case there's no performance gain. Just another silly way to do it! :P
"Did you find it annoying not to use dist()?"
Explaining what is the algorithm is good, but perhaps you can mention that dist() is a shortcut for this formula.
Explaining that sqrt() is not necessary for distance checking can be explained as an optimization, near the end perhaps.
Unlike GoToLoop, I don't believe that shorter code is more performant, but dropping the sqrt() is really a good tip.
Yes, there is an explanation about dist() in an early example. It sounds like a chapter on optimization at the end might be the way to go.
Any place you can point to showing that sqrt() is less efficient than dist()? I'd like to explain why/how that's the case.
FWIW, there's a major disclaimer that the code is definitely not super efficient. The explanations are meant to be simple as a jumping off point – not for building whole game engines.
dist() does the same thing as my shorter
sq(px - cx) + sq(py - cy)
example.Only that it adds sqrt() as your longer example does:
https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java#L4300
It'd be really gr8 if Processing had distSq() function just like "java.awt.geom" package got Point2D's distanceSq(): 8-|
http://docs.oracle.com/javase/8/docs/api/java/awt/geom/Point2D.html#distanceSq-double-double-double-double-
A ran a little test with interesting results: my version using the vanilla Pythagorean Theorem is very fast (390ms for 1-billion calculations) versus dist() which took 400ms and an algorithm I found on a forum without square roots (392ms).
Have you tested how my version fared compared to those? =P~
About the same for the distance calculation: 404ms.
Indeed there's almost no diff. among those approaches. :-\"
Got no idea what kinda voodoo optimizations Java's runtime is doing behind the scenes! 8-}
Made 2 tests. 1 where coordinates are picked randomly once.
And the other coordinates are picked randomly for each single iteration:
I suspect avoiding the sqrt will provide better results in p5js, so it's worth including it as a possible optimisation. One thing I see a lot of in posted questions is unoptimised loops being used to do multi-object collision detection, so that's a good subject for a chapter ;)