HTML5 games: 3D collision detection
hacks.mozilla.org
hacks.mozilla.org
It reminds me of 2-3 years ago, where almost every Javascript tutorial was littered with JQuery library function calls. Most Stackoverflow questions were answered with JQuery answers. In the meantime things have obviously changed for the better.
With Three.js it's still extreme, there are even several books with "WebGL" in the title that cover only or 90% of the content Three.js. In the meantime organisations like Mozilla could start and ditch Three.js in favour WebGL. It would be in common interest of everone.
WebGL is probably very hard to do in Vanilla JavaScript, so people are using Three.js...
If you just want to show a pixel shader, you probably don't need any library. Still, creating textures, setting up the buffer for your single quad, compiling pixel and vertex shaders and the shaders themselves will easily pile up to a good 100 lines of code.
Now, if you want to do anything with real 3d objects, such as in this example, you need code to:
- generate or load the models
- set up your camera
- trace clicks on the canvas to the objects (not a simple feat in 3d space)
- implement this mover widget/drag+drop of objects
- implement some sort of lighting
- implement some sort of shadow rendering, so it's easier to estimate
the position of the objects
- (for all of the above) implement a library for
vector and matrix math
You'll easily end up with a thousand lines of code there, and you haven't done anything yet to solve the actual problem this article discusses.Similarly for 3D most people just want to get shit done. In that case Three.js is a great way to to just get stuff done.
There's man years of work to do to a full 3d engine. You can easily spend a couple of man years just writing exporter/importers and trying to get every little piece of data out and into some meaningful way into your engine. People spend years making rendering systems and shader combiners and post processing system.
I'm not saying three.js is perfect but at least at some level much of that is already in there. Why should anyone skip all of that? Just to make years of extra work for themselves?
If you want to do raw WebGL then go for, but don't push that on others. To me that's like someone saying "you should write in assembly language". No, I'll choose something that provides existing solutions so I can move on to my actual content instead of re-inventing the wheel.
I agree that it is important to know how things work without 3rd party helpers, regardless of whether you'd use a game engine / library later or not.
However, the post on the Hacks blog presents some articles and demos we created for the MDN and IMHO the title is appropriate, since one of the two articles introduced (https://developer.mozilla.org/en-US/docs/Games/Techniques/3D...) is generic and it's about algorithms in raw JavaScript.
I just thought there would be value too in showing how to do collision detection using one of the most popular 3D graphics library and a physics engine as well, since that would be the most common case when people are developing games (versus making an engine)
The problem with the wide spread of Three is that it is some crazy sprawly combination of utility library, engine, and framework, and that it encourages a particular worldview on new graphics programmers that don't have any understanding on how the underlying hardware or math works.
It's useful in the hands of somebody that already knows that stuff, but harmful from a pedagogical point of view.
I've been playing around with 3D for a while, and have found that collision detection is the (relatively) easy bit: it's collision resolution that messes me up!
Doing a dot-product for every face pair is O(N^2). Bad for large N. If you do this right, convex hulls with a few hundred faces are no problem.
When constructing convex hulls for this, you can use a tool like QHull. Tell it that you want at least some minimal angle such as 1 degree at each edge. This will eliminate many near-coplanar faces. Also, it's better to use polygonal faces than triangular faces. With triangles, a rectangular face has two coplanar faces, and GJK will toggle between them, making resting object jiggle. (Basic annoyance of polyhedra and roundoff: if you tesselate, you get coplanar faces; if you use polygon faces, they're not perfectly flat.)
It's basically 3D data structures but also with a focus on cache friendly-ness that you don't traditionally see anywhere else.