{"id":145,"date":"2015-11-28T18:17:41","date_gmt":"2015-11-28T18:17:41","guid":{"rendered":"http:\/\/metareal.net\/blog\/?p=145"},"modified":"2015-11-28T18:17:41","modified_gmt":"2015-11-28T18:17:41","slug":"math","status":"publish","type":"post","link":"http:\/\/metareal.net\/blog\/?p=145","title":{"rendered":"Math"},"content":{"rendered":"<p>Metareal has some objects that just stay in one place and rotate. They look nice.<\/p>\n<p>Came across a funny little bug this morning. I&#8217;d let Metareal run all night, and the rotating objects were all moving a bit chunkily, not turning smoothly like they should.<\/p>\n<p>Frame rate: AOK, 60 FPS still rock solid.<br \/>\nPlayer movement: AOK, nice and smooth.<br \/>\nSpinning objects: All of them kikk&#8217;ty kikk&#8217;ty kich. Broke!<\/p>\n<p>Took a look at the rotating object code. Each game objects has a &#8220;body&#8221; and a &#8220;mind&#8221;, spinning is implemented in a mind&#8230;<br \/>\nThe &#8220;rotate&#8221; parameter is a vec4 as {axisX, axisY, axisZ, degreesPerSecond}.<\/p>\n<pre>\r\nbool SpinMind::mindTick() override\r\n{\r\n  MOVEC4(rotate); \/\/ retrieve instance parameter \"rotate\" to local variable.\r\n  this->tix++;\r\n  float degrees = rotate[3] * tix \/ 60.0;\r\n  rotate.v[3] = degrees;\r\n  this->setBodyRotation(rotate, degrees);\r\n        \r\n  return true;\r\n}\r\n<\/pre>\n<p>Can you spot the bug?<\/p>\n<p>It&#8217;s roundoff error. As &#8220;tix&#8221; (ticks, ha ha) gets large, degrees gets large. Then degrees loses precision after running a while.<\/p>\n<p>The solution? Accumulate degree (not ticks), and wrap at +\/-360.<\/p>\n<pre>\r\nbool SpinMind::mindTick() override\r\n{\r\n  MOVEC4(rotate);\r\n  this->degrees = fmod(this->degrees + rotate[3] \/ 60.0, 360.0);\r\n  rotate.v[3] = this->degrees;\r\n  this->setBodyRotation(rotate, degrees);\r\n        \r\n  return true;\r\n}\r\n<\/pre>\n<p>This is all basic and obvious! But apparently I got something working just enough, and moved onward. That&#8217;s the way to make progress.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Metareal has some objects that just stay in one place and rotate. They look nice. Came across a funny little bug this morning. I&#8217;d let Metareal run all night, and the rotating objects were all moving a bit chunkily, not turning smoothly like they should. Frame rate: AOK, 60 FPS still rock solid. Player movement: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,1],"tags":[5],"class_list":["post-145","post","type-post","status-publish","format-standard","hentry","category-3","category-uncategorized","tag-math"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/metareal.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/145","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/metareal.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/metareal.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/metareal.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/metareal.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=145"}],"version-history":[{"count":3,"href":"http:\/\/metareal.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/145\/revisions"}],"predecessor-version":[{"id":149,"href":"http:\/\/metareal.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/145\/revisions\/149"}],"wp:attachment":[{"href":"http:\/\/metareal.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/metareal.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=145"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/metareal.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}