{"id":401,"date":"2018-11-16T18:00:03","date_gmt":"2018-11-17T00:00:03","guid":{"rendered":"http:\/\/www.chroniclesoftright.com\/blog\/?p=401"},"modified":"2018-11-16T15:45:18","modified_gmt":"2018-11-16T21:45:18","slug":"math-is-hard","status":"publish","type":"post","link":"https:\/\/www.chroniclesoftright.com\/blog\/2018\/11\/16\/math-is-hard\/","title":{"rendered":"Math is hard"},"content":{"rendered":"<p>Math is just hard.\u00a0 I&#8217;m an educated person.\u00a0 I&#8217;ve never been afraid of math.\u00a0 But sometimes it&#8217;s just hard.\u00a0 Especially when dealing with 3d graphics.\u00a0 I fully admit that there things going on in the background of the game that I don&#8217;t understand.\u00a0 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.<\/p>\n<p>Here&#8217;s what I&#8217;m talking about.\u00a0 I have been working on getting Penrith to the point that I can release a demo-type version of the game.\u00a0 That means adding objects to the world, modifying the terrain to be somewhat interesting, and testing as much as I can along the way.\u00a0 This was all going pretty well until two weird things cropped up.\u00a0 First, my player character started moving *very* slowly.\u00a0 This was confusing because the movement at each frame is based on the game&#8217;s framerate so that the speed at which the character moves should be framerate independent.\u00a0 I noticed it especially on my faster development machine.\u00a0 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.\u00a0 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.\u00a0\u00a0<strong>But<\/strong> 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%.\u00a0 The bizarre thing is that the faster a machine is, the more noticeable the drop.\u00a0 If a computer was capable of looping 600 times\/second, the character would at 10% of the desired speed.\u00a0 On the flip side, any machine not capable of looping more than 60 times\/second will never see this problem.\u00a0 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.<\/p>\n<p>Now, the more complicated problem.\u00a0 My collision detection system is still a bit of a black box to me so I&#8217;m thrilled when it works and slightly befuddled when it doesn&#8217;t.\u00a0 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.\u00a0 Without this tranform the colliders were not rotating and positioning correctly over the rendered objects and I was seeing strange behavior.\u00a0 Again, it only took three lines of code to fix, but required several hours of testing to find the problem nonetheless.<\/p>\n<p>In any case, math is hard and I&#8217;m still moving forward.\u00a0 I hope to have a version of the game up and running so that multiple players can simply walk around Penrith by December 1st.\u00a0 I think this is a reasonable goal given how close I am to that point right now.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Math is just hard.\u00a0 I&#8217;m an educated person.\u00a0 I&#8217;ve never been afraid of math.\u00a0 But sometimes it&#8217;s just hard.\u00a0 Especially when dealing with 3d graphics.\u00a0 I fully admit that there things going on in the background of the game that I don&#8217;t understand.\u00a0 THREE.js does a great job of abstracting away much of the dirty&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-401","post","type-post","status-publish","format-standard","hentry","category-planning-and-development"],"_links":{"self":[{"href":"https:\/\/www.chroniclesoftright.com\/blog\/wp-json\/wp\/v2\/posts\/401","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.chroniclesoftright.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chroniclesoftright.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chroniclesoftright.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chroniclesoftright.com\/blog\/wp-json\/wp\/v2\/comments?post=401"}],"version-history":[{"count":1,"href":"https:\/\/www.chroniclesoftright.com\/blog\/wp-json\/wp\/v2\/posts\/401\/revisions"}],"predecessor-version":[{"id":402,"href":"https:\/\/www.chroniclesoftright.com\/blog\/wp-json\/wp\/v2\/posts\/401\/revisions\/402"}],"wp:attachment":[{"href":"https:\/\/www.chroniclesoftright.com\/blog\/wp-json\/wp\/v2\/media?parent=401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chroniclesoftright.com\/blog\/wp-json\/wp\/v2\/categories?post=401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chroniclesoftright.com\/blog\/wp-json\/wp\/v2\/tags?post=401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}