{"id":3401,"date":"2026-09-08T21:40:15","date_gmt":"2026-09-08T13:40:15","guid":{"rendered":"http:\/\/www.keisyaavicenna.com\/blog\/?p=3401"},"modified":"2026-09-08T21:40:15","modified_gmt":"2026-09-08T13:40:15","slug":"how-to-use-cosine-in-game-development-471c-f03b3d","status":"publish","type":"post","link":"http:\/\/www.keisyaavicenna.com\/blog\/2026\/09\/08\/how-to-use-cosine-in-game-development-471c-f03b3d\/","title":{"rendered":"How to use cosine in game development?"},"content":{"rendered":"<p>Hey there, game dev enthusiasts! I&#8217;m here representing a top &#8211; notch COS supplier, and today we&#8217;re gonna dive deep into how to use cosine in game development. <a href=\"https:\/\/www.everbright-laser.com\/laser-device\/cos\/\">COS<\/a><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.everbright-laser.com\/uploads\/42177\/single-emitter-chipsfe931.jpg\"><\/p>\n<h3>Basics of Cosine<\/h3>\n<p>First things first, let&#8217;s get clear on what cosine is. In trigonometry, cosine is a function that relates the angles of a right &#8211; triangle to the ratios of its sides. For a given angle \u03b8 in a right &#8211; triangle, cos(\u03b8) is defined as the ratio of the adjacent side to the hypotenuse. But in game development, we&#8217;re not always dealing with physical right &#8211; triangles. Cosine comes in really handy in many virtual scenarios.<\/p>\n<p>One of the most common uses of cosine is in character movement. Imagine you&#8217;ve got a character in a 2D game, and you want it to move in a circular path. Cosine plays a vital role here. We know that for a circle with radius r centered at the origin (0,0), the x &#8211; coordinate of a point on the circle at an angle \u03b8 from the positive x &#8211; axis is given by x = r * cos(\u03b8).<\/p>\n<p>Let&#8217;s say you&#8217;re creating a top &#8211; down shooter game. You might have an enemy spaceship that orbits around a central point (like a power generator). To calculate the position of the spaceship at any given time, you need to use cosine (along with sine for the y &#8211; coordinate). You&#8217;d increment the angle \u03b8 over time (let&#8217;s say every frame), and then calculate the new x and y positions of the spaceship using the cos and sin functions.<\/p>\n<pre><code class=\"language-javascript\">\/\/ JavaScript example for spaceship movement in a circle\nlet radius = 100; \/\/ radius of the circular path\nlet angle = 0;    \/\/ initial angle\nlet speed = 0.01; \/\/ speed at which the angle changes\n\nfunction updatePosition() {\n    angle += speed;\n    let x = radius * Math.cos(angle);\n    let y = radius * Math.sin(angle);\n    \/\/ Here you'd update the spaceship's position on the screen\n    console.log(`New position: (${x}, ${y})`);\n}\n\n\/\/ Call this function every frame\nsetInterval(updatePosition, 16);\n<\/code><\/pre>\n<h3>Collision Detection<\/h3>\n<p>Cosine is also a key player in collision detection. When two objects are moving around in a game, figuring out if they&#8217;re about to collide is crucial. For instance, in a billiards game, when a ball is moving towards a wall, you can use cosine to determine the angle of incidence.<\/p>\n<p>The angle of incidence is important because it helps you calculate the angle of reflection. According to the law of reflection, the angle of incidence (the angle between the incoming path of the ball and the normal to the wall) is equal to the angle of reflection. Cosine can be used to calculate the components of the ball&#8217;s velocity vector when it hits the wall.<\/p>\n<p>Let&#8217;s assume our ball has a velocity vector <code>v = (vx, vy)<\/code> and it hits a vertical wall. The normal vector of the vertical wall is <code>n = (1, 0)<\/code>. The dot product of the velocity vector and the normal vector is <code>v \u00b7 n = vx * 1+ vy * 0= vx<\/code>.<\/p>\n<p>We know that <code>v \u00b7 n = ||v|| * ||n|| * cos(\u03b8)<\/code>, where <code>||v||<\/code> is the magnitude of the velocity vector and <code>||n||<\/code> is the magnitude of the normal vector (which is 1 in our case). So, <code>cos(\u03b8)= vx \/ ||v||<\/code>. Once we have the angle of incidence, we can calculate the new velocity vector after the collision.<\/p>\n<pre><code class=\"language-python\">import math\n\n# Initial velocity of the ball\nvx = 5\nvy = 3\n# Magnitude of the velocity vector\nv_magnitude = math.sqrt(vx**2 + vy**2)\n# Angle of incidence\ncos_theta = vx \/ v_magnitude\ntheta = math.acos(cos_theta)\n# New velocity after hitting a vertical wall (reflecting the x - component)\nnew_vx = -vx\nnew_vy = vy\nprint(f&quot;New velocity components: ({new_vx}, {new_vy})&quot;)\n<\/code><\/pre>\n<h3>Lighting and Shading<\/h3>\n<p>In 3D game development, cosine is super important for lighting and shading. You know how in real life, the way light hits an object affects how we perceive it? The same principle applies in games.<\/p>\n<p>The Phong lighting model, which is widely used in game lighting, makes use of cosine. The model consists of three components: ambient, diffuse, and specular lighting. The diffuse lighting component is where cosine comes in.<\/p>\n<p>Diffuse lighting is based on how much light is reflected by a surface in all directions. The amount of light reflected by a surface is proportional to the cosine of the angle between the surface normal and the light direction. If the light hits the surface directly (the angle between the normal and the light direction is 0 degrees), the cosine of the angle is 1, and the surface receives the maximum amount of light. As the angle increases, the cosine value decreases, and the surface receives less light.<\/p>\n<pre><code class=\"language-glsl\">\/\/ GLSL shader code for diffuse lighting\n#version 330 core\n\nin vec3 FragPos;\nin vec3 Normal;\nout vec4 FragColor;\n\nuniform vec3 lightPos;\nuniform vec3 lightColor;\nuniform vec3 objectColor;\n\nvoid main()\n{\n    \/\/ Calculate the normal vector\n    vec3 norm = normalize(Normal);\n    \/\/ Calculate the light direction vector\n    vec3 lightDir = normalize(lightPos - FragPos);\n    \/\/ Calculate the diffuse factor using cosine\n    float diff = max(dot(norm, lightDir), 0.0);\n    vec3 diffuse = diff * lightColor;\n    \/\/ Final color\n    vec3 result = diffuse * objectColor;\n    FragColor = vec4(result, 1.0);\n}\n<\/code><\/pre>\n<h3>Why Our COS Supplier is a Great Fit<\/h3>\n<p>Now that you&#8217;ve seen how important cosine is in game development, let me tell you why our COS (Cosine &#8211; related products and services, you can think of it as high &#8211; precision math libraries, optimized algorithms, etc.) is a great choice for your game development needs.<\/p>\n<p>Our team has years of experience in the field. We&#8217;ve worked with some of the biggest names in the game industry, and we know what it takes to develop high &#8211; quality, efficient game &#8211; related solutions. Our products are designed to be easy to integrate into your existing game development pipeline. Whether you&#8217;re using Unity, Unreal Engine, or any other popular game development platform, we&#8217;ve got you covered.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.everbright-laser.com\/uploads\/42177\/3d-sensing-vcsel-tof44e44.jpg\"><\/p>\n<p>We also offer top &#8211; notch support. If you run into any issues while using our COS, our support team is just a message away. We&#8217;ll help you troubleshoot and get the most out of our products. And we&#8217;re constantly innovating. We&#8217;re always looking for new ways to improve our offerings and make them even more useful for game developers.<\/p>\n<h3>Let&#8217;s Connect<\/h3>\n<p><a href=\"https:\/\/www.everbright-laser.com\/laser-device\/\">Laser Device<\/a> If you&#8217;re interested in learning more about how our COS can take your game development to the next level, we&#8217;d love to hear from you. Whether you&#8217;re working on a small indie project or a big &#8211; budget AAA game, we&#8217;ve got solutions that can fit your needs. Reach out to us to start a conversation about your project and how we can help. You can contact us to discuss pricing, customization options, and more. Don&#8217;t miss out on the opportunity to enhance your game with the power of cosine and our top &#8211; quality COS!<\/p>\n<h3>References<\/h3>\n<ul>\n<li>&quot;Trigonometry for Game Programming&quot; by Fletcher Dunn and Ian Parberry<\/li>\n<li>&quot;3D Math Primer for Graphics and Game Development&quot; by Fletcher Dunn and Ian Parberry<\/li>\n<li>OpenGL Shading Language (GLSL) documentation<\/li>\n<\/ul>\n<hr>\n<p><a href=\"https:\/\/www.everbright-laser.com\/\">Suzhou Everbright Photonics Co., Ltd.<\/a><\/p>\n<p>Address: No.56, Lijiang Road, SND,Suzhou, Jiangsu Province, China<br \/>E-mail: sales@everbrightphotonics.com<br \/>WebSite: <a href=\"https:\/\/www.everbright-laser.com\/\">https:\/\/www.everbright-laser.com\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey there, game dev enthusiasts! I&#8217;m here representing a top &#8211; notch COS supplier, and today &hellip; <a title=\"How to use cosine in game development?\" class=\"hm-read-more\" href=\"http:\/\/www.keisyaavicenna.com\/blog\/2026\/09\/08\/how-to-use-cosine-in-game-development-471c-f03b3d\/\"><span class=\"screen-reader-text\">How to use cosine in game development?<\/span>Read more<\/a><\/p>\n","protected":false},"author":565,"featured_media":3401,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3364],"class_list":["post-3401","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-industry","tag-cos-4c1f-f094d1"],"_links":{"self":[{"href":"http:\/\/www.keisyaavicenna.com\/blog\/wp-json\/wp\/v2\/posts\/3401","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.keisyaavicenna.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.keisyaavicenna.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.keisyaavicenna.com\/blog\/wp-json\/wp\/v2\/users\/565"}],"replies":[{"embeddable":true,"href":"http:\/\/www.keisyaavicenna.com\/blog\/wp-json\/wp\/v2\/comments?post=3401"}],"version-history":[{"count":0,"href":"http:\/\/www.keisyaavicenna.com\/blog\/wp-json\/wp\/v2\/posts\/3401\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.keisyaavicenna.com\/blog\/wp-json\/wp\/v2\/posts\/3401"}],"wp:attachment":[{"href":"http:\/\/www.keisyaavicenna.com\/blog\/wp-json\/wp\/v2\/media?parent=3401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.keisyaavicenna.com\/blog\/wp-json\/wp\/v2\/categories?post=3401"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.keisyaavicenna.com\/blog\/wp-json\/wp\/v2\/tags?post=3401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}