{"id":617,"date":"2018-04-13T15:14:19","date_gmt":"2018-04-13T14:14:19","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=617"},"modified":"2024-08-09T21:26:43","modified_gmt":"2024-08-09T20:26:43","slug":"javascript-game-functions","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/javascript-game-functions\/","title":{"rendered":"Javascript Game functions"},"content":{"rendered":"\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"javascript\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">scene\nsceneobjects\n\nimages\/textures\/animated sprites\nsounds\n\ncamera\nlights\nbounding box\n\n\ndirection\/angle\n\nanimation &amp; easing functions\n\nsphere collision\nbounds collision\n\nfunction abs(a) {\n   return Math.abs(a);\n}\n\nfunction length(a) { \n   return Math.sqrt(a.x*a.x + a.y*a.y + a.z*a.z); \n}\n\nfunction distance(a, b) { \n   return Math.sqrt((a.x - b.x) * (a.x - b.x) +\n                    (a.y - b.y) * (a.y - b.y) +\n                    (a.z - b.z) * (a.z - b.z));\n}\n\nvar p1 = {\n\tx: 20,\n\ty: 20\n};\n\nvar p2 = {\n\tx: 40,\n\ty: 40\n};\n\n\/\/ angle in radians\nvar angleRadians = Math.atan2(p2.y - p1.y, p2.x - p1.x);\n\n\/\/ angle in degrees\nvar angleDeg = Math.atan2(p2.y - p1.y, p2.x - p1.x) * 180 \/ Math.PI;\n\n\/\/ http:\/\/onedayitwillmake.com\/blog\/2011\/09\/getting-the-angle-between-two-3d-points\/\n\nfunction() {\n  \/\/ Make up two vectors\n  var vectorA = new Vector3(5, -20, -14);\n  var vectorB = new Vector3(-1, 3, 2);\n \n  \/\/ Store some information about them for below\n  var dot = vectorA.dot(vectorB);\n  var lengthA = vectorA.length();\n  var lengthB = vectorB.length();\n \n  \/\/ Now to find the angle\n  var theta = Math.acos( dot \/ (lengthA * lengthB) ); \/\/ Theta = 3.06 radians or 175.87 degrees\n  \/\/ But what can you do with it?\n}\n\n\nfunction now() {\n   return (new Date()).getTime() \/ 1000.0;\n}\n\n\/\/\/ Check intersections \/ collisions\n\/\/\/ https:\/\/developer.mozilla.org\/en-US\/docs\/Games\/Techniques\/3D_collision_detection\n\nfunction isPointInsideAABB(point, box) {\n  return (point.x >= box.minX &amp;&amp; point.x &lt;= box.maxX) &amp;&amp; (point.y >= box.minY &amp;&amp; point.y &lt;= box.maxY) &amp;&amp; (point.z >= box.minY &amp;&amp; point.z &lt;= box.maxZ);\n}\n\nfunction intersectCubes(a, b) {\n  return (a.minX &lt;= b.maxX &amp;&amp; a.maxX >= b.minX) &amp;&amp;\n         (a.minY &lt;= b.maxY &amp;&amp; a.maxY >= b.minY) &amp;&amp;\n         (a.minZ &lt;= b.maxZ &amp;&amp; a.maxZ >= b.minZ);\n}\n\nfunction intersectSpheres(a, b) {\n  \/\/ we are using multiplications because it's faster than calling Math.pow\n  var distance = Math.sqrt((a.x - b.x) * (a.x - b.x) +\n                           (a.y - b.y) * (a.y - b.y) +\n                           (a.z - b.z) * (a.z - b.z));\n  return distance &lt; (a.radius + b.radius);\n}\n\nfunction intersectSphereBox(sphere, box) {\n  \/\/ get box closest point to sphere center by clamping\n  var x = Math.max(box.minX, Math.min(sphere.x, box.maxX);\n  var y = Math.max(box.minY, Math.min(sphere.y, box.maxY);\n  var z = Math.max(box.minZ, Math.min(sphere.z, box.maxZ);\n\n  \/\/ this is the same as isPointInsideSphere\n  var distance = Math.sqrt((x - sphere.x) * (x - sphere.x) +\n                           (y - sphere.y) * (y - sphere.y) +\n                           (z - sphere.z) * (z - sphere.z));\n  \n  return distance &lt; sphere.radius; \n} \n\nfunction doStep(factor) { \n    objects.forEach(function(object) { \n        object.prevPosition = object.position; \n        if (object.velocity) \n            object.position += (factor * object.velocity); \n        if (object.acceleration) \n            object.velocity += (factor * object.acceleration); \n        if (object.friction) \n            object.velocity -= (factor * object.friction); \n        object.collisionCheck(); \n    }; \n} \n\nvar speed = 1; \nvar prevTime = null; \nfunction step(timestamp) { \n    if (!prevTime) { prevTime = now(); } \n    var elapsed = now() - prevTime; \n    factor = elapsed * speed; \n    if (factor > 0) {\n     doStep(factor);\n    }\n    window.requestAnimationFrame(step);\n}\n\nwindow.requestAnimationFrame(step);\n\n<\/pre><\/div>\n\n\n\n<p><a href=\"https:\/\/www.codeproject.com\/Articles\/43751\/Battlefield-Simulator\">https:\/\/www.codeproject.com\/Articles\/43751\/Battlefield-Simulator<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.codeproject.com\/KB\/game\/BattleField\/BattleField_Unit_Types.jpg\" alt=\"BattleField_Unit_Types.jpg\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.codeproject.com\/KB\/game\/BattleField\/BattleField_Ranks.PNG\" alt=\"BattleField_Ranks.PNG\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.codeproject.com\/KB\/game\/BattleField\/BattleField_TerrainTypes.PNG\" alt=\"BattleField_TerrainTypes.PNG\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.codeproject.com\/KB\/game\/BattleField\/BattleField_POC_001.JPG\" alt=\"BattleField_POC_001.JPG\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/www.codeproject.com\/Articles\/43751\/Battlefield-Simulator<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[5,4],"tags":[],"class_list":["post-617","post","type-post","status-publish","format-standard","hentry","category-javascript","category-programming"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/617","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=617"}],"version-history":[{"count":18,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/617\/revisions"}],"predecessor-version":[{"id":8782,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/617\/revisions\/8782"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}