{"id":534,"date":"2018-07-26T10:13:42","date_gmt":"2018-07-26T17:13:42","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=3025"},"modified":"2018-07-26T10:13:42","modified_gmt":"2018-07-26T17:13:42","slug":"3d-point-to-2d-point-easy-mini-3d-engine","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=534","title":{"rendered":"3d Point to 2d Point (Easy Mini 3d Engine)"},"content":{"rendered":"<p>Many years ago when I had just started programming I found this absolute gem by Andries Odendaal.<\/p>\n<p><em>modern es6 version<\/em><\/p>\n<pre lang=\"javascript\" line=\"1\">\r\nlet rotX = 0, rotY = 0,\r\n    perspective = 500, \r\n    depth,\r\n    currX, currY;\r\n\/\/ learned something like this at Andries Odendaal's www.wireframe.co.za \r\nfunction point3d(x, y, z) {\r\n    let cosX = Math.cos(rotX),\r\n        cosY = Math.cos(rotY),\r\n        sinX = Math.sin(rotX),\r\n        sinY = Math.sin(rotY),\r\n        posX, posY, posZ;\r\n   \r\n    posZ = z * cosX - x * sinX,\r\n    posX = z * sinX + x * cosX,\r\n    posY = y * cosY - posZ * sinY,\r\n    posZ = y * sinY + posZ * cosY;\r\n    \r\n    depth = 1 \/ (posZ \/ perspective + 1);\r\n    currX = posX * depth;\r\n    currY = posY * depth;\r\n \r\n    return [ currX, currY, depth ];\r\n}\r\n<\/pre>\n<p>Here&#8217;s is an example of it in action:<\/p>\n<p><iframe height='395' scrolling='no' title='Nonsense Clocks w Color' src='\/\/codepen.io\/ZevanRosser\/embed\/zBpbzK\/?height=395&#038;theme-id=5318&#038;default-tab=result&#038;embed-version=2' frameborder='no' allowtransparency='true' allowfullscreen='true' style='width: 100%;'>See the Pen <a href='https:\/\/codepen.io\/ZevanRosser\/pen\/zBpbzK\/'>Nonsense Clocks w Color<\/a> by Zevan Rosser (<a href='https:\/\/codepen.io\/ZevanRosser'>@ZevanRosser<\/a>) on <a href='https:\/\/codepen.io'>CodePen<\/a>.<br \/>\n<\/iframe><\/p>\n<p><iframe height='300' scrolling='no' title='Nonsense Clocks' src='\/\/codepen.io\/ZevanRosser\/embed\/WxdgqE\/?height=300&#038;theme-id=5318&#038;default-tab=js,result&#038;embed-version=2' frameborder='no' allowtransparency='true' allowfullscreen='true' style='width: 100%;'>See the Pen <a href='https:\/\/codepen.io\/ZevanRosser\/pen\/WxdgqE\/'>Nonsense Clocks<\/a> by Zevan Rosser (<a href='https:\/\/codepen.io\/ZevanRosser'>@ZevanRosser<\/a>) on <a href='https:\/\/codepen.io'>CodePen<\/a>.<br \/>\n<\/iframe><\/p>\n<p>I&#8217;ve used this code many many times, it&#8217;s just easy to throw into any language and instantly get 3d points rendered in 2d. Here is a short video of a Java applet from 2003 called &#8220;Catch Env&#8221; that made use of it:<\/p>\n<p><iframe loading=\"lazy\" width=\"100%\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/yiq8ePKLMNs\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen><\/iframe><\/p>\n<p>Here is the source code for that ^^<br \/>\n<a href=\"http:\/\/www.zevanrosser.com\/shape2\/j\/Catchenv.java\" target=\"blank\">http:\/\/www.zevanrosser.com\/shape2\/j\/Catchenv.java<\/a><\/p>\n<p>You&#8217;ll notice in that source, that I nested the equation to allow for local and global transformations. It was around that time that I learned the ins and outs of real 2D and 3D matrix transformation math&#8230; <a href=\"https:\/\/mrl.nyu.edu\/~perlin\/experiments\/packing\/render\/Matrix.java\" target=\"blank\">Ken Perlin&#8217;s classfiles<\/a> from NYU were a real help when I was learning that stuff. I don&#8217;t think this was the exact file I was working with, but it was definitely on his site somewhere.<\/p>\n<p>Before all that, during my junior year in college I created a 3d engine based off Odendaal&#8217;s code in Director (Lingo). Here is a video of some of the demos for it:<\/p>\n<p><iframe loading=\"lazy\" width=\"100%\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/I8SNP3j9W7A\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen><\/iframe><\/p>\n<p>&#8230;and here is a strange screenshot from my personal website at the time:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/actionsnippet.com\/imgs\/vacuum.jpg\" \/><\/p>\n<p>Just an example of a powerful snippet and a gateway to learning matrix transformation math. When I first really dug in to html5 canvas &#8211; before WebGL was supported so widely &#8211; having this trick up my sleeve was great. As you can see in the below link, I used it more than a few times back then:<\/p>\n<p>Daily canvas experiments:<br \/>\n<a href=\"http:\/\/zreference.com\/projects\/all-graphics.php\" target=\"blank\"><br \/>\nhttp:\/\/zreference.com\/projects\/all-graphics.php<\/a><\/p>\n<p>\ud83d\ude00<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Many years ago when I had just started programming I found this absolute gem by Andries Odendaal. modern es6 version let rotX = 0, rotY = 0, perspective = 500, depth, currX, currY; \/\/ learned something like this at Andries Odendaal&#8217;s www.wireframe.co.za function point3d(x, y, z) { let cosX = Math.cos(rotX), cosY = Math.cos(rotY), sinX [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101,33,114,161,160],"tags":[72,162,173,134],"class_list":["post-534","post","type-post","status-publish","format-standard","hentry","category-3d","category-graphics","category-graphics-algorithms","category-html5","category-javascript","tag-3d","tag-canvas","tag-es6","tag-javascript"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/534","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=534"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/534\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=534"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=534"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}