Math is hard

Math is just hard.  I’m an educated person.  I’ve never been afraid of math.  But sometimes it’s just hard.  Especially when dealing with 3d graphics.  I fully admit that there things going on in the background of the game that I don’t understand.  THREE.js does a great job of abstracting away much of the dirty work, but sometimes things come up that have to be dealt with at a low level and that can get downright frustrating.

Here’s what I’m talking about.  I have been working on getting Penrith to the point that I can release a demo-type version of the game.  That means adding objects to the world, modifying the terrain to be somewhat interesting, and testing as much as I can along the way.  This was all going pretty well until two weird things cropped up.  First, my player character started moving *very* slowly.  This was confusing because the movement at each frame is based on the game’s framerate so that the speed at which the character moves should be framerate independent.  I noticed it especially on my faster development machine.  It turns out (I believe) that THREE.js caps the display frame rate to 60 frames/second, but this cap is not reflected by the Clock object.  So if the game is running fast enough that it is looping through my main loop at, say, 100 times/second, then the delta on the clock for each loop is going to register 0.01.  But if the renderer is capped at 60 frames/second, then at each frame the character is moving 1/100 of the distance he should move in a full second, but only actually doing that 60 times/second, so he movement speed is dropped to 60%.  The bizarre thing is that the faster a machine is, the more noticeable the drop.  If a computer was capable of looping 600 times/second, the character would at 10% of the desired speed.  On the flip side, any machine not capable of looping more than 60 times/second will never see this problem.  It was simple enough to just test if the delta time was less than 1/60 and, if so, set it to 1/60, but it was still frustrating to find.

Now, the more complicated problem.  My collision detection system is still a bit of a black box to me so I’m thrilled when it works and slightly befuddled when it doesn’t.  To make a long story short, I had to apply the world matrix of the parent of each collider object to the points of the triangles the collision system is testing against.  Without this tranform the colliders were not rotating and positioning correctly over the rendered objects and I was seeing strange behavior.  Again, it only took three lines of code to fix, but required several hours of testing to find the problem nonetheless.

In any case, math is hard and I’m still moving forward.  I hope to have a version of the game up and running so that multiple players can simply walk around Penrith by December 1st.  I think this is a reasonable goal given how close I am to that point right now.

Leave a Reply

Your email address will not be published. Required fields are marked *