jMonkeyEngine Tips, Tricks, and Gotchas
I’ve been having great fun hobbying around on a 3D rendering project in between lessons of stork lately. I’m not terribly familiar with 3D libraries and such, so to get myself started I’ve adopted jMonkeyEngine3 for some momentum. I’m not talking much about the project yet, but I’ll drop a hint here:
Anyway, I’ve noticed some… undocumented features of the engine, and I thought i’d scribble them down in case anyone happens to find them useful.
This list is likely to grow with time, but here’s a good start:
Meshes, Collisions, and
If your meshes are not colliding properly, you may need to call
updateBound() on them before you add them to the scene graph.
I’ve been loading custom meshes out of model files with a library I’m writing, and while the models rendered fine I couldn’t get them to register a collision. Specifically, this lovely bit of code was coming up with empty
CollisionResults collisions=new CollisionResults(); Vector2f click=inputManager.getCursorPosition(); Vector3f cursor=cam.getWorldCoordinates(new Vector2f(click.x, click.y), 0.0f).clone(); Vector3f dir=cam.getWorldCoordinates(new Vector2f(click.x, click.y), 1.0f).subtractLocal(cursor).normalizeLocal(); Ray ray=new Ray(cursor, dir); rootNode.collideWith(ray, collisions);
This casts a ray from the camera through the cursor onto the scene graph. In more detail, the code (a) determines the location of the cursor in 2D screen-space, (b) projects the click point into a line in 3D world-space using the corresponding point on the near (
0.0f) and far (
1.0f) planes of the viewing frustum, and then (c) checks a ray constructed from those two points against the scene graph. (Also, this code is straight out of the jME3 “Hello Picking” mouse picking tutorial, so the jME3 folks are pretty sure it should work, too.)
When my meshes didn’t collide as expected, I confirmed that the demo code works (it does), and then tried dropping a
Box into my scene (per the demo) rather than my own meshes, which worked nicely. A quick read of the
Box source code revealed a call to the heretofore unknown-to-me
updateBound() method. I tried calling that method on my
Meshes before adding them to their respective
Geometry objects, and everything then worked as expected.
So, if you find that
collideWith() isn’t working on your custom
Mesh objects, make sure you’re calling
updateBound() on them. It turns out that’s important.
Good Luck, and God Speed
Hope this is helpful to someone! At the very least, it should be helpful to me in a few months after I’ve forgotten.