Saturday, March 9, 2013

Again with Physijs Plane Meshes

‹prev | My Chain | next›

I solved a minor Physijs mystery last night when I realized that I had to use nothing but ConvexMesh objects when assembling grouped objects. I believe that I now have my very old rafting game almost working condition in the code editor used in 3D Game Programming for Kids.

As an aside, it is surprising how much I have grown accustomed to coding in my fork of Mr Doob's code editor. Between the changes that I made and the latest and greatest editing features in ACE code editor, it is really quite nice. Much better than coding in Emacs and reloading the browser to see my changes. It is hard to believe that this is how I started way back when. I really owe Mr.doob a many debts of thanks between the Three.js library and his code editor (now html editor).

Anyhow, I take some time to rip out my early scoreboard prototype from this game, replacing it with the scoreboard class that I wrote recently to support the book. That is something of a superficial concern. Much more troubling is that I still do not know how to easily code the various river segments in a fashion that is digestible by beginners. The solution that eventually made me set aside this game involved much trigonometry. I still worry about this problem, but before I can tackle it, I still have a bug in the copied game.

Right when the game starts, the raft shoots off to the side:



This turns out to be the same thing that caused me trouble last night—only in an entirely new way. In the game, there are obstacles in the river that cause the game to end:
function addObstacleMarker(water) {
  var marker = new Physijs.PlaneMesh(
    new THREE.PlaneGeometry(1, 1),
    new THREE.MeshBasicMaterial()
  );
  // ...
}
This is merely intended to provide a frame of reference for the actual marker, hence the one-by-one plane. Last night I found that these plane meshes, when combined with convex meshes, wound up with no “physical” impact in the game—players passed right through them.

Today, I find that this plane geometry—even through it is supposed to be 1 pixel by 1 pixel in dimension, is extending out infinitely in physical space, causing the raft to shoot out from the starting line. The solution is simple enough, even if a bit strange, I convert my frame of reference into a Physijs convex mesh:
function addObstacleMarker(water) {
  var marker = new Physijs.ConvexMesh(
    new THREE.PlaneGeometry(1, 1),
    new THREE.MeshBasicMaterial()
  );
  // ...
}
With that, I can again play (and lose) the game without shooting off into dry land:



I call it a night here. Up tomorrow -- I really need to finalize how I want to add these river segments together. And if I cannot, it may be time to punt on this game and chose another for the last chapter in the book.


Day #685

No comments:

Post a Comment