<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ javascript game - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ javascript game - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Sun, 26 Jul 2026 11:16:53 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/javascript-game/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to Code a Crossy Road Game Clone with Three.js ]]>
                </title>
                <description>
                    <![CDATA[ In this tutorial, you’ll learn how to create a clone of the mobile game Crossy Road with Three.js. The goal of this game is to move a character through an endless path of static and moving obstacles. You have to go around trees and avoid getting hit ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-code-a-crossy-road-game-clone-with-threejs/</link>
                <guid isPermaLink="false">67b7bad6bcd9ef47aba6001d</guid>
                
                    <category>
                        <![CDATA[ ThreeJS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Game Development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ javascript game ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Hunor Márton Borbély ]]>
                </dc:creator>
                <pubDate>Thu, 20 Feb 2025 23:29:26 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1740094139765/282432bf-1a5b-40e4-8b74-7aa854fd20ac.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>In this tutorial, you’ll learn how to create a clone of the mobile game Crossy Road with Three.js. The goal of this game is to move a character through an endless path of static and moving obstacles. You have to go around trees and avoid getting hit by cars.</p>
<p>There's a lot to cover in this tutorial: we will start with setting up the scene, the camera, and the lights. Then you’ll learn how to draw the player and the map with the trees and the cars. We’ll also cover how to animate the vehicles, and we’ll add event handlers to move the player through the map. Finally, we’ll add hit detection between the cars and the player.</p>
<p>This article is a shortened version of the Crossy Road tutorial from my site <a target="_blank" href="https://javascriptgametutorials.com/">JavaScriptGameTutorials.com</a>. The extended tutorial is also available as a video on <a target="_blank" href="https://www.youtube.com/watch?v=vNr3_hQ3Bws&amp;ab_channel=HunorM%C3%A1rtonBorb%C3%A9ly">YouTube</a>.</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ol>
<li><p><a class="post-section-overview" href="#heading-how-to-set-up-the-game">How to Set Up the Game</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-render-a-map">How to Render a Map</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-animate-the-cars">How to Animate the Cars</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-move-the-player">How to Move the Player</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-hit-detection">Hit Detection</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-next-steps">Next Steps</a></p>
</li>
</ol>
<h2 id="heading-how-to-set-up-the-game">How to Set Up the Game</h2>
<p>In this chapter, we’ll set up the drawing canvas, camera, and lights and render a box representing our player.</p>
<h3 id="heading-initializing-the-project">Initializing the Project</h3>
<p>I recommend using Vite to initialize the project. To do so, go to your terminal and type <code>npm create vite</code>, which will create an initial project for you.</p>
<pre><code class="lang-bash"><span class="hljs-comment"># Create app</span>
npm create vite my-crossy-road-game

<span class="hljs-comment"># Navigate to the project</span>
<span class="hljs-built_in">cd</span> my-crossy-road-game

<span class="hljs-comment"># Install dependencies</span>
npm install three

<span class="hljs-comment"># Start development server</span>
npm run dev
</code></pre>
<p>When generating the project, select <strong>Vanilla</strong> because we won't use any front-end framework for this project. Then navigate to the project folder Vite just created for you and install Three.js with <code>npm install three</code>. Finally, you can go to the terminal and type <code>npm run dev</code> to start a development server. This way, you can see live the result of your coding in the browser.</p>
<h3 id="heading-the-drawing-canvas">The Drawing Canvas</h3>
<p>Now, let's look into this project. The entry point of this project is the <strong>index.html</strong> file in the root folder. Let's replace the div element with a canvas element with the ID <strong>game</strong>. This is the drawing canvas that Three.js will use to render the scene. This file also has a script tag that points to the main JavaScript file.</p>
<pre><code class="lang-xml"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"icon"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"image/svg+xml"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/vite.svg"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Vite App<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">canvas</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"game"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">canvas</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"module"</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"/src/main.ts"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<h3 id="heading-the-mainjs-file">The main.js file</h3>
<p>The <strong>main.js</strong> file is the root of our game. Let's replace its content. We’ll define a Three.js scene containing all the 3D elements, including the player, that we will soon define. The scene also includes a camera that we’ll use together with the renderer to render a static frame of it. We’ll define these in the following steps.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> THREE <span class="hljs-keyword">from</span> <span class="hljs-string">"three"</span>;
<span class="hljs-keyword">import</span> { Renderer } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Renderer"</span>;
<span class="hljs-keyword">import</span> { Camera } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Camera"</span>;
<span class="hljs-keyword">import</span> { player } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Player"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">"./style.css"</span>;

<span class="hljs-keyword">const</span> scene = <span class="hljs-keyword">new</span> THREE.Scene();
scene.add(player);

<span class="hljs-keyword">const</span> camera = Camera();
player.add(camera);

<span class="hljs-keyword">const</span> renderer = Renderer();
renderer.render(scene, camera);
</code></pre>
<h3 id="heading-the-player">The Player</h3>
<p>Let's start adding the necessary objects to render the first scene. Let's add a simple box to represent the player. We already added the player to the scene in the main file, so let's see how to define this player.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739804077435/9667f94f-5005-41f8-a6ed-faa6706f0be1.png" alt="The player" class="image--center mx-auto" width="3840" height="2160" loading="lazy"></p>
<p>In this file, we write a function that creates a 3D object and exports a property containing the player instance. The player is a singleton. There is only one player object in the game, and every other file can access it through this export.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> THREE <span class="hljs-keyword">from</span> <span class="hljs-string">"three"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> player = Player();

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Player</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> player = <span class="hljs-keyword">new</span> THREE.Group();

  <span class="hljs-keyword">const</span> body = <span class="hljs-keyword">new</span> THREE.Mesh(
    <span class="hljs-keyword">new</span> THREE.BoxGeometry(<span class="hljs-number">15</span>, <span class="hljs-number">15</span>, <span class="hljs-number">20</span>),
    <span class="hljs-keyword">new</span> THREE.MeshLambertMaterial({ <span class="hljs-attr">color</span>: <span class="hljs-string">"white"</span> })
  );
  body.position.z = <span class="hljs-number">10</span>;
  player.add(body);

  <span class="hljs-keyword">return</span> player;
}
</code></pre>
<p>Initially, the player will be a simple box. To draw a 3D object, we’ll define a geometry and a material. The geometry defines the object's shape, and the material defines its appearance. Here, we’re using box geometry to define a box. The box geometry takes three arguments: the width, depth, and height of the box along the x, y, and z axes.</p>
<p>We have different options for the material. The main difference between them is how they react to light, if at all. Here, we're using <strong>MeshLambertMaterial</strong>, a simple material that responds to light. We set the color property to white.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739804142917/7b6a49e2-30df-40d8-bd4c-7ac1a2725931.png" alt="Different light options" class="image--center mx-auto" width="3840" height="2160" loading="lazy"></p>
<p>Then, we wrap the geometry and the material into a mesh, which we can add to the scene. We can also position this mesh by setting its X, Y, and Z positions. In the case of a box, these set the center position. By setting the Z position of this box, we’re elevating it above the ground by half its height. As a result, the bottom of the box will be standing on the ground.</p>
<p>We also wrap the mesh into a group element. This is not necessary at this point, but having this structure will be handy when animating the player. When it comes to player animation, we want to separate the horizontal and vertical movement. We want this to be able to follow the player with the camera as it moves but not to move the camera up and down when the player is jumping. We will move the group horizontally along the XY plane together with the camera and move the mesh vertically.</p>
<h3 id="heading-the-camera">The Camera</h3>
<p>Now, let's look into different camera options. There are two main camera options: the perspective camera, as you can see on the left in the image below, and the orthographic camera, which you can see on the right.</p>
<p>The perspective camera is the default camera in Three.js and is the most common camera type across all video games. It creates a perspective projection, which makes things further away appear smaller and things right in front of the camera appear bigger.</p>
<p>On the other hand, the orthographic camera creates parallel projections, which means that objects are the same size regardless of their distance from the camera. We’ll use an orthographic camera here to give our game more of an arcade look.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739800656673/d2643544-b44c-418e-a475-2844e6626dbb.png" alt="Perspective vs orthographic camera" class="image--center mx-auto" width="2420" height="1224" loading="lazy"></p>
<p>In Three.js, we place the 3D objects along the X, Y, and Z axes. We define the coordinate system in a way where the ground is on the XY plane so the player can move left and right along the x-axis, forward and backward along the y-axis, and when the player is jumping, it will go up along the z-axis.</p>
<p>We place the camera in this coordinate system to the right along the x-axis, behind the player along the y-axis, and above the ground. Then, the camera will look back at the origin of the coordinate system to the 0,0,0 coordinate, where the player will be placed initially.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739803742848/d202d09f-1c1b-4246-be23-2a6f6af52423.png" alt="The coordinate system" class="image--center mx-auto" width="1576" height="892" loading="lazy"></p>
<p>With all this theory in mind, let's define our camera. We create a new file for the camera and export the camera function, which returns an orthographic camera that we'll use to render the scene.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> THREE <span class="hljs-keyword">from</span> <span class="hljs-string">"three"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Camera</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> size = <span class="hljs-number">300</span>;
  <span class="hljs-keyword">const</span> viewRatio = <span class="hljs-built_in">window</span>.innerWidth / <span class="hljs-built_in">window</span>.innerHeight;
  <span class="hljs-keyword">const</span> width = viewRatio &lt; <span class="hljs-number">1</span> ? size : size * viewRatio;
  <span class="hljs-keyword">const</span> height = viewRatio &lt; <span class="hljs-number">1</span> ? size / viewRatio : size;

  <span class="hljs-keyword">const</span> camera = <span class="hljs-keyword">new</span> THREE.OrthographicCamera(
    width / <span class="hljs-number">-2</span>, <span class="hljs-comment">// left</span>
    width / <span class="hljs-number">2</span>, <span class="hljs-comment">// right</span>
    height / <span class="hljs-number">2</span>, <span class="hljs-comment">// top</span>
    height / <span class="hljs-number">-2</span>, <span class="hljs-comment">// bottom</span>
    <span class="hljs-number">100</span>, <span class="hljs-comment">// near</span>
    <span class="hljs-number">900</span> <span class="hljs-comment">// far</span>
  );

  camera.up.set(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>);
  camera.position.set(<span class="hljs-number">300</span>, <span class="hljs-number">-300</span>, <span class="hljs-number">300</span>);
  camera.lookAt(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>);

  <span class="hljs-keyword">return</span> camera;
}
</code></pre>
<p>To define a camera, we need to define a camera frustum. This will determine how to project the 3D elements onto the screen. In the case of an orthographic camera, we define a box. Everything in the scene within this box will be projected onto the screen. In the image below, the green dot represents the camera position and the gray box around the scene represents the camera frustum.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739804878454/3af6d9ba-1d35-4b98-8731-95af5889a1ed.png" alt="The camera frustum" class="image--center mx-auto" width="1720" height="1292" loading="lazy"></p>
<p>In this function, we set up the camera frustum to fill the browser window, and the width or height will be 300 units, depending on the aspect ratio. The smaller value between width and height will be 300 units, and the other one will fill the available space. If the width is larger than the height, then the height is 300 units. If the height is larger, then the width is 300 units.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739805040485/c7b9aa02-3c5d-4a2e-a1ff-e9941ce83c39.png" alt="Sizing the scene" class="image--center mx-auto" width="3840" height="2160" loading="lazy"></p>
<p>Then, we set the camera's position. We move the camera to the right along the x-axis with 300 units, then behind the player along the y-axis with -300 units, and finally above the ground. We also look back to the origin of the coordinate system, to the 0,0,0 coordinate, where the player is positioned initially. Finally, we set which axis is pointing upwards. Here, we set the z-axis to point upwards.</p>
<h3 id="heading-the-lights">The Lights</h3>
<p>After setting up the camera, let's set up the lights. There are many types of lights in Three.js. Here, we're going to use an ambient light and a directional light.</p>
<p>You can see the result of ambient light only on the left side of the below image. The ambient light brightens the entire scene. It doesn't have a specific position or direction. You can think of it like the light on a cloudy day when it's bright, but there are no shadows. The ambient light is used to simulate indirect light.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739800781538/9e140ef9-4133-4151-9fc5-b629504259a8.png" alt="Ambient vs directional light" class="image--center mx-auto" width="2420" height="1224" loading="lazy"></p>
<p>Now, let's look at the directional light that you can see on the right of the image above. A directional light has a position and a target. It shines light in a specific direction with parallel light rays. Even though it has a position, you can rather think of it as the sun that is shining from very far away. The position here is more to define the direction of the light, but then all the other light rays are also parallel with this light ray. So you can think of it like the sun.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739805160031/4742e1de-5dc0-4443-9716-af18581bfa1e.png" alt="The directional light shines with parallel light rays" class="image--center mx-auto" width="3840" height="2160" loading="lazy"></p>
<p>That's why we're combining an ambient light (so that we have a base brightness all around the scene) with a directional light (to illuminate specific sides of our objects with a brighter color).</p>
<p>After seeing what the lights look like, let's add an ambient and directional light to the scene in our main file. We also position the directional light to the left along the x-axis, behind the player along the y-axis, and above the ground. By default, the target of the directional light is going to be the 0,0,0 coordinate. We don't have to set that.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> THREE <span class="hljs-keyword">from</span> <span class="hljs-string">"three"</span>;
<span class="hljs-keyword">import</span> { Renderer } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Renderer"</span>;
<span class="hljs-keyword">import</span> { Camera } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Camera"</span>;
<span class="hljs-keyword">import</span> { player } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Player"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">"./style.css"</span>;

<span class="hljs-keyword">const</span> scene = <span class="hljs-keyword">new</span> THREE.Scene();
scene.add(player);

<span class="hljs-keyword">const</span> ambientLight = <span class="hljs-keyword">new</span> THREE.AmbientLight();
scene.add(ambientLight);

<span class="hljs-keyword">const</span> dirLight = <span class="hljs-keyword">new</span> THREE.DirectionalLight();
dirLight.position.set(<span class="hljs-number">-100</span>, <span class="hljs-number">-100</span>, <span class="hljs-number">200</span>);
scene.add(dirLight);

<span class="hljs-keyword">const</span> camera = Camera();
player.add(camera);

<span class="hljs-keyword">const</span> renderer = Renderer();
renderer.render(scene, camera);
</code></pre>
<p>Note that we add the lights to the scene, but we add the camera to the player. This way, when we animate the player, the camera will follow the player.</p>
<h3 id="heading-the-renderer">The Renderer</h3>
<p>We have defined many things, but we still don’t see anything on the screen. As a final piece, we need to have a renderer to render the scene. A renderer renders the 3D scene into a canvas element.</p>
<p>In this function, we get the canvas element we defined in the HTML and set it as the drawing context. We also set a couple more parameters. We make the background of the 3D scene transparent with the alpha flag, set the pixel ratio, and set the size of the canvas to fill the entire screen.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> THREE <span class="hljs-keyword">from</span> <span class="hljs-string">"three"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Renderer</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> canvas = <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">"canvas.game"</span>);
  <span class="hljs-keyword">if</span> (!canvas) <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">"Canvas not found"</span>);

  <span class="hljs-keyword">const</span> renderer = <span class="hljs-keyword">new</span> THREE.WebGLRenderer({
    <span class="hljs-attr">alpha</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-attr">antialias</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-attr">canvas</span>: canvas,
  });
  renderer.setPixelRatio(<span class="hljs-built_in">window</span>.devicePixelRatio);
  renderer.setSize(<span class="hljs-built_in">window</span>.innerWidth, <span class="hljs-built_in">window</span>.innerHeight);

  <span class="hljs-keyword">return</span> renderer;
}
</code></pre>
<p>This is how our first scene comes together. We rendered a simple box.</p>
<h2 id="heading-how-to-render-a-map">How to Render a Map</h2>
<p>Now, let's add all the other objects to the scene. In this chapter, we’ll define the map. The map will consist of multiple rows, each described by metadata. Each row can be a forest, a car, or a truck lane. We’ll go through each type and define the 3D objects representing them.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739803813951/3796e0c0-6f02-4c82-979b-a5192d8c3b8c.png" alt="The different row types" class="image--center mx-auto" width="2560" height="1442" loading="lazy"></p>
<p>The map can be broken down into rows, and each row can be broken down into multiple tiles. The player will move from tile to tile. Trees are also placed on a distinct tile. Cars, on the other hand, do not relate to tiles. They move freely through the lane.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739803829719/38318821-8496-43d1-9b1c-a5b36d78af14.png" alt="A row can be broken down into a tile" class="image--center mx-auto" width="2560" height="1442" loading="lazy"></p>
<p>We define a file for the constants. Here, we define the number of tiles in each row. In this case, there are 17 ties per row, going from -8 to +8. The player will start in the middle at tile zero.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> minTileIndex = <span class="hljs-number">-8</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> maxTileIndex = <span class="hljs-number">8</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> tilesPerRow = maxTileIndex - minTileIndex + <span class="hljs-number">1</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> tileSize = <span class="hljs-number">42</span>;
</code></pre>
<h3 id="heading-the-starting-row">The Starting Row</h3>
<p>First, let's add the starting row. We’ll define a couple of components that we’re going to use to render the map, and we’ll render the initial row.</p>
<p>Let's create a new component called Map. This file will expose the map's metadata and the 3D objects representing it. Let's export a group called map. This container will contain all the 3D objects for each row. Soon, we will add this group to the scene.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> THREE <span class="hljs-keyword">from</span> <span class="hljs-string">"three"</span>;
<span class="hljs-keyword">import</span> { Grass } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Grass"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> map = <span class="hljs-keyword">new</span> THREE.Group();

<span class="hljs-keyword">const</span> grass = Grass(<span class="hljs-number">0</span>);
map.add(grass);
</code></pre>
<p>Then, we set the map's content. Later, we will generate the 3D objects based on the metadata and use it to render the map. For now, let's just call the Grass function, which will return another Three.js group. We call the grass function with the row index, so the grass component will position itself based on this row index. Then, we add the returned group to the map.</p>
<p>Now, let's define the Grass component. The Grass function returns the foundation and container of the forest rows and is also used for the starting row. It returns a group containing a flat, wide, green box. The dimensions of this box are determined by the constants <strong>tileSize</strong> and <strong>tilesPerRow</strong>. The box also has some height, so it sticks out compared to the road, which will be completely flat.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> THREE <span class="hljs-keyword">from</span> <span class="hljs-string">"three"</span>;
<span class="hljs-keyword">import</span> { tilesPerRow, tileSize } <span class="hljs-keyword">from</span> <span class="hljs-string">"./constants"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Grass</span>(<span class="hljs-params">rowIndex</span>) </span>{
  <span class="hljs-keyword">const</span> grass = <span class="hljs-keyword">new</span> THREE.Group();
  grass.position.y = rowIndex * tileSize;

  <span class="hljs-keyword">const</span> foundation = <span class="hljs-keyword">new</span> THREE.Mesh(
    <span class="hljs-keyword">new</span> THREE.BoxGeometry(tilesPerRow * tileSize, tileSize, <span class="hljs-number">3</span>),
    <span class="hljs-keyword">new</span> THREE.MeshLambertMaterial({ <span class="hljs-attr">color</span>: <span class="hljs-number">0xbaf455</span> })
  );
  foundation.position.z = <span class="hljs-number">1.5</span>;
  grass.add(foundation);

  <span class="hljs-keyword">return</span> grass;
}
</code></pre>
<p>The grass can serve as a container for the trees in the row. That's why we wrap the green box into a group so that later, we can also add children to this group. We position the group along the y-axis based on the row index that we received from the Map component. For the initial lane, this is zero, but as we're going to have multiple lanes, we need to place them according to this position.</p>
<p>Now that we have the map container and the grass component, we can finally add the map to the scene.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> THREE <span class="hljs-keyword">from</span> <span class="hljs-string">"three"</span>;
<span class="hljs-keyword">import</span> { Renderer } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Renderer"</span>;
<span class="hljs-keyword">import</span> { Camera } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Camera"</span>;
<span class="hljs-keyword">import</span> { player } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Player"</span>;
<span class="hljs-keyword">import</span> { map } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Map"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">"./style.css"</span>;

<span class="hljs-keyword">const</span> scene = <span class="hljs-keyword">new</span> THREE.Scene();
scene.add(player);
scene.add(map);

<span class="hljs-keyword">const</span> ambientLight = <span class="hljs-keyword">new</span> THREE.AmbientLight();
scene.add(ambientLight);

<span class="hljs-keyword">const</span> dirLight = <span class="hljs-keyword">new</span> THREE.DirectionalLight();
dirLight.position.set(<span class="hljs-number">-100</span>, <span class="hljs-number">-100</span>, <span class="hljs-number">200</span>);
scene.add(dirLight);

<span class="hljs-keyword">const</span> camera = Camera();
scene.add(camera);

<span class="hljs-keyword">const</span> renderer = Renderer();
renderer.render(scene, camera);
</code></pre>
<h3 id="heading-how-to-add-a-forest-row">How to Add a Forest Row</h3>
<p>Now that we have an empty forest, let's add another row containing trees. We define the map's metadata and render the rows based on this metadata.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739806992172/139197f0-f2c1-4f11-aacf-f1fedeeaf9b0.png" alt="A forest row" class="image--center mx-auto" width="2560" height="1442" loading="lazy"></p>
<p>Back in the Map component, let's define the map's metadata. The metadata is an array of objects that contain information about each row. Each row will contain a type that will determine the kind of the row and the rest of the properties depending on the row type.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> THREE <span class="hljs-keyword">from</span> <span class="hljs-string">"three"</span>;
<span class="hljs-keyword">import</span> { Grass } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Grass"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> metadata = [
  {
    <span class="hljs-attr">type</span>: <span class="hljs-string">"forest"</span>,
    <span class="hljs-attr">trees</span>: [
      { <span class="hljs-attr">tileIndex</span>: <span class="hljs-number">-3</span>, <span class="hljs-attr">height</span>: <span class="hljs-number">50</span> },
      { <span class="hljs-attr">tileIndex</span>: <span class="hljs-number">2</span>, <span class="hljs-attr">height</span>: <span class="hljs-number">30</span> },
      { <span class="hljs-attr">tileIndex</span>: <span class="hljs-number">5</span>, <span class="hljs-attr">height</span>: <span class="hljs-number">50</span> },
    ],
  },
];

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> map = <span class="hljs-keyword">new</span> THREE.Group();

<span class="hljs-keyword">const</span> grass = Grass(<span class="hljs-number">0</span>);
map.add(grass);
</code></pre>
<p>The metadata for a forest includes the type of “forest” and a list of trees. Each tree has a tile index, which represents which tile it is standing on. In this case, we have 17 tiles per row, going from -8 to +8. The trees also have a height, which is actually the height of the crown.</p>
<p>To render the rows, we loop over this array and generate 3D objects for each row based on the row type. For the forest type, it calls the Grass function again, which will return a Three.js group. We call this Grass function with the row index so the Grass function can position itself along the y-axis. The row index is off by one compared to the array index because the first item in the metadata will become the second row right after the starting row, which is not part of the metadata.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> THREE <span class="hljs-keyword">from</span> <span class="hljs-string">"three"</span>;
<span class="hljs-keyword">import</span> { Grass } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Grass"</span>;
<span class="hljs-keyword">import</span> { Tree } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Tree"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> metadata = [
  {
    <span class="hljs-attr">type</span>: <span class="hljs-string">"forest"</span>,
    <span class="hljs-attr">trees</span>: [
      { <span class="hljs-attr">tileIndex</span>: <span class="hljs-number">-3</span>, <span class="hljs-attr">height</span>: <span class="hljs-number">50</span> },
      { <span class="hljs-attr">tileIndex</span>: <span class="hljs-number">2</span>, <span class="hljs-attr">height</span>: <span class="hljs-number">30</span> },
      { <span class="hljs-attr">tileIndex</span>: <span class="hljs-number">5</span>, <span class="hljs-attr">height</span>: <span class="hljs-number">50</span> },
    ],
  },
];

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> map = <span class="hljs-keyword">new</span> THREE.Group();

<span class="hljs-keyword">const</span> grass = Grass(<span class="hljs-number">0</span>);
map.add(grass);

metadata.forEach(<span class="hljs-function">(<span class="hljs-params">rowData, index</span>) =&gt;</span> {
  <span class="hljs-keyword">const</span> rowIndex = index + <span class="hljs-number">1</span>;

  <span class="hljs-keyword">if</span> (rowData.type === <span class="hljs-string">"forest"</span>) {
    <span class="hljs-keyword">const</span> row = Grass(rowIndex);

    rowData.trees.forEach(<span class="hljs-function">(<span class="hljs-params">{ tileIndex, height }</span>) =&gt;</span> {
      <span class="hljs-keyword">const</span> three = Tree(tileIndex, height);
      row.add(three);
    });

    map.add(row);
  }
});
</code></pre>
<p>Forest rows also have trees. For each item in the trees array, we render a tree. The Tree function will return a 3D object representing the tree.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739807310182/40cbb90b-0356-4db5-97bb-cdbe0c8e8cb8.png" alt="A tree" class="image--center mx-auto" width="2560" height="1442" loading="lazy"></p>
<p>We pass on to this function the tile index that we will use to position the tree within the row and the height. We add the trees to the group the Grass function returned, and then we add the whole group returned by the Grass function to the map.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> THREE <span class="hljs-keyword">from</span> <span class="hljs-string">"three"</span>;
<span class="hljs-keyword">import</span> { tileSize } <span class="hljs-keyword">from</span> <span class="hljs-string">"../constants"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Tree</span>(<span class="hljs-params">tileIndex, height</span>) </span>{
  <span class="hljs-keyword">const</span> tree = <span class="hljs-keyword">new</span> THREE.Group();
  tree.position.x = tileIndex * tileSize;

  <span class="hljs-keyword">const</span> trunk = <span class="hljs-keyword">new</span> THREE.Mesh(
    <span class="hljs-keyword">new</span> THREE.BoxGeometry(<span class="hljs-number">15</span>, <span class="hljs-number">15</span>, <span class="hljs-number">20</span>),
    <span class="hljs-keyword">new</span> THREE.MeshLambertMaterial({ <span class="hljs-attr">color</span>: <span class="hljs-number">0x4d2926</span> })
  );
  trunk.position.z = <span class="hljs-number">10</span>;
  tree.add(trunk);

  <span class="hljs-keyword">const</span> crown = <span class="hljs-keyword">new</span> THREE.Mesh(
    <span class="hljs-keyword">new</span> THREE.BoxGeometry(<span class="hljs-number">30</span>, <span class="hljs-number">30</span>, height),
    <span class="hljs-keyword">new</span> THREE.MeshLambertMaterial({ <span class="hljs-attr">color</span>: <span class="hljs-number">0x7aa21d</span> })
  );
  crown.position.z = height / <span class="hljs-number">2</span> + <span class="hljs-number">20</span>;
  tree.add(crown);

  <span class="hljs-keyword">return</span> tree;
}
</code></pre>
<p>Since we’ve already added the map to the scene, the forest will appear on the screen. But first, we need to define how to render a tree. We are going to represent a tree with two boxes. We're going to have a box for the trunk and one for the crown.</p>
<p>These are both simple boxes, just like we had before with the player and also in the Grass component. The trunk is placed on top of the ground. We lift it along the Z-axis by half of its height, and the crown is placed on top of the trunk. The crown's height is also based on the height property. These two meshes are wrapped together into a group, and then we position this group along the X-axis based on the tile index property.</p>
<h3 id="heading-car-lanes">Car Lanes</h3>
<p>Now, let's add another row type: car lanes. The process of adding car lanes will follow a similar structure. We define the lanes' metadata, including the vehicles, and then map them into 3D objects.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739807532566/1c9cfdc9-2c45-476c-9ada-09f7abf79328.png" alt="The car lane" class="image--center mx-auto" width="2560" height="1442" loading="lazy"></p>
<p>In the Map component in the metadata, let's replace the first row with a car lane. The car lane will contain a single red car moving to the left. We have a direction property, which is a boolean flag. If this is true, that means the cars are moving to the right in the lane, and if it's false, then the vehicles are moving to the left. We also have a speed property, which defines how many units each vehicle takes every second.</p>
<p>Finally, we have an array of vehicles. Each car will have an initial tile index, which represents only its initial position because the cars will move later. Each car will also have a color property, which is a hexadecimal color value.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> THREE <span class="hljs-keyword">from</span> <span class="hljs-string">"three"</span>;
<span class="hljs-keyword">import</span> { Grass } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Grass"</span>;
<span class="hljs-keyword">import</span> { Tree } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Tree"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> metadata = [
  {
    <span class="hljs-attr">type</span>: <span class="hljs-string">"car"</span>,
    <span class="hljs-attr">direction</span>: <span class="hljs-literal">false</span>,
    <span class="hljs-attr">speed</span>: <span class="hljs-number">1</span>,
    <span class="hljs-attr">vehicles</span>: [{ <span class="hljs-attr">initialTileIndex</span>: <span class="hljs-number">2</span>, <span class="hljs-attr">color</span>: <span class="hljs-number">0xff0000</span> }],
  },
];

. . .
</code></pre>
<p>Now, to render this lane type, we have to extend our logic to support car lanes. We add another if block that is very similar to the rendering of the forest. In the case of a car type, we call the Road function, which will also return a Three.js group. We also call this function with the row index to position the group according to the lane.</p>
<p>Then, for each item in the vehicles array, we create a 3D object representing the car with the Car function. We add the cars to the group returned by the Road function, and we add the whole group to the map. For the car function, we also pass on the initial tile index that we will use to position the car within the row, the direction, and the color.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> THREE <span class="hljs-keyword">from</span> <span class="hljs-string">"three"</span>;
<span class="hljs-keyword">import</span> { Grass } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Grass"</span>;
<span class="hljs-keyword">import</span> { Road } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Road"</span>;
<span class="hljs-keyword">import</span> { Tree } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Tree"</span>;
<span class="hljs-keyword">import</span> { Car } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Car"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> metadata = [
  {
    <span class="hljs-attr">type</span>: <span class="hljs-string">"car"</span>,
    <span class="hljs-attr">direction</span>: <span class="hljs-literal">false</span>,
    <span class="hljs-attr">speed</span>: <span class="hljs-number">1</span>,
    <span class="hljs-attr">vehicles</span>: [{ <span class="hljs-attr">initialTileIndex</span>: <span class="hljs-number">2</span>, <span class="hljs-attr">color</span>: <span class="hljs-number">0xff0000</span> }],
  },
];

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> map = <span class="hljs-keyword">new</span> THREE.Group();

<span class="hljs-keyword">const</span> grass = Grass(<span class="hljs-number">0</span>);
map.add(grass);

metadata.forEach(<span class="hljs-function">(<span class="hljs-params">rowData, index</span>) =&gt;</span> {
  <span class="hljs-keyword">const</span> rowIndex = index + <span class="hljs-number">1</span>;

  <span class="hljs-keyword">if</span> (rowData.type === <span class="hljs-string">"forest"</span>) {
    <span class="hljs-keyword">const</span> row = Grass(rowIndex);

    rowData.trees.forEach(<span class="hljs-function">(<span class="hljs-params">{ tileIndex, height }</span>) =&gt;</span> {
      <span class="hljs-keyword">const</span> three = Tree(tileIndex, height);
      row.add(three);
    });

    map.add(row);
  }

  <span class="hljs-keyword">if</span> (rowData.type === <span class="hljs-string">"car"</span>) {
    <span class="hljs-keyword">const</span> row = Road(rowIndex);

    rowData.vehicles.forEach(<span class="hljs-function">(<span class="hljs-params">vehicle</span>) =&gt;</span> {
      <span class="hljs-keyword">const</span> car = Car(
        vehicle.initialTileIndex,
        rowData.direction,
        vehicle.color
      );
      row.add(car);
    });

    map.add(row);
  }
});
</code></pre>
<p>The Road and Car functions are new here, so let's examine them next. The Road function returns the foundation and container of the car and truck lanes. Similar to the Grass function, it also returns a group containing a gray plane.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739807755293/e731a758-ac20-40f5-819b-82a5ae81e65f.png" alt="The Road component" class="image--center mx-auto" width="2560" height="1442" loading="lazy"></p>
<p>The size of the plane is also determined by the constants <strong>tileSize</strong> and <strong>tilesPerRow</strong>. Unlike the grass function, though, it doesn't have any height. It's completely flat. The road will also serve as a container for the cars and trucks in the row, so that's why we wrap the plane into a group – so that we can add children to it.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> THREE <span class="hljs-keyword">from</span> <span class="hljs-string">"three"</span>;
<span class="hljs-keyword">import</span> { tilesPerRow, tileSize } <span class="hljs-keyword">from</span> <span class="hljs-string">"../constants"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Road</span>(<span class="hljs-params">rowIndex</span>) </span>{
  <span class="hljs-keyword">const</span> road = <span class="hljs-keyword">new</span> THREE.Group();
  road.position.y = rowIndex * tileSize;

  <span class="hljs-keyword">const</span> foundation = <span class="hljs-keyword">new</span> THREE.Mesh(
    <span class="hljs-keyword">new</span> THREE.PlaneGeometry(tilesPerRow * tileSize, tileSize),
    <span class="hljs-keyword">new</span> THREE.MeshLambertMaterial({ <span class="hljs-attr">color</span>: <span class="hljs-number">0x454a59</span> })
  );
  road.add(foundation);

  <span class="hljs-keyword">return</span> road;
}
</code></pre>
<p>Now, let's look at the Car. The Car function returns a very simple 3D car model.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739807975022/dc04c3c1-de94-49fd-ad85-ade999c8862a.png" alt="A car" class="image--center mx-auto" width="2560" height="1442" loading="lazy"></p>
<p>It contains a box for the body and a smaller box for the top part. We also have two wheel meshes. Because we never see the cars from underneath, we don't need to separate the wheels into left and right. We can just use one long box for the front wheels and another one for the back wheels.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> THREE <span class="hljs-keyword">from</span> <span class="hljs-string">"three"</span>;
<span class="hljs-keyword">import</span> { tileSize } <span class="hljs-keyword">from</span> <span class="hljs-string">"./constants"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Car</span>(<span class="hljs-params">initialTileIndex, direction, color</span>) </span>{
  <span class="hljs-keyword">const</span> car = <span class="hljs-keyword">new</span> THREE.Group();
  car.position.x = initialTileIndex * tileSize;
  <span class="hljs-keyword">if</span> (!direction) car.rotation.z = <span class="hljs-built_in">Math</span>.PI;

  <span class="hljs-keyword">const</span> main = <span class="hljs-keyword">new</span> THREE.Mesh(
    <span class="hljs-keyword">new</span> THREE.BoxGeometry(<span class="hljs-number">60</span>, <span class="hljs-number">30</span>, <span class="hljs-number">15</span>),
    <span class="hljs-keyword">new</span> THREE.MeshLambertMaterial({ color })
  );
  main.position.z = <span class="hljs-number">12</span>;
  car.add(main);

  <span class="hljs-keyword">const</span> cabin = <span class="hljs-keyword">new</span> THREE.Mesh(
    <span class="hljs-keyword">new</span> THREE.BoxGeometry(<span class="hljs-number">33</span>, <span class="hljs-number">24</span>, <span class="hljs-number">12</span>),
    <span class="hljs-keyword">new</span> THREE.MeshLambertMaterial({ <span class="hljs-attr">color</span>: <span class="hljs-string">"white"</span> })
  );
  cabin.position.x = <span class="hljs-number">-6</span>;
  cabin.position.z = <span class="hljs-number">25.5</span>;
  car.add(cabin);

  <span class="hljs-keyword">const</span> frontWheel = <span class="hljs-keyword">new</span> THREE.Mesh(
    <span class="hljs-keyword">new</span> THREE.BoxGeometry(<span class="hljs-number">12</span>, <span class="hljs-number">33</span>, <span class="hljs-number">12</span>),
    <span class="hljs-keyword">new</span> THREE.MeshLambertMaterial({ <span class="hljs-attr">color</span>: <span class="hljs-number">0x333333</span> })
  );
  frontWheel.position.x = <span class="hljs-number">18</span>;
  frontWheel.position.z = <span class="hljs-number">6</span>;
  car.add(frontWheel);

  <span class="hljs-keyword">const</span> backWheel = <span class="hljs-keyword">new</span> THREE.Mesh(
    <span class="hljs-keyword">new</span> THREE.BoxGeometry(<span class="hljs-number">12</span>, <span class="hljs-number">33</span>, <span class="hljs-number">12</span>),
    <span class="hljs-keyword">new</span> THREE.MeshLambertMaterial({ <span class="hljs-attr">color</span>: <span class="hljs-number">0x333333</span> })
  );
  backWheel.position.x = <span class="hljs-number">-18</span>;
  backWheel.position.z = <span class="hljs-number">6</span>;
  car.add(backWheel);

  <span class="hljs-keyword">return</span> car;
}
</code></pre>
<p>We group all these elements, position them based on the <strong>initialTileIndex</strong> property, and turn them based on the <strong>direction</strong> property. If the car goes to the left, we rotate it by 180°. When we set rotation values in Three.js. We have to set them in radians, so that's why we set it to Math.Pi, which is equivalent to 180°.</p>
<p>You can also find a more extended version of how to draw this car with textures <a target="_blank" href="https://www.freecodecamp.org/news/three-js-tutorial/">in this article</a>.</p>
<p>Based on the metadata, we can now render a map with several rows. Here’s an example with a few more lanes. Of course, feel free to define your own map.</p>
<pre><code class="lang-javascript">. . .

export <span class="hljs-keyword">const</span> metadata = [
  {
    <span class="hljs-attr">type</span>: <span class="hljs-string">"car"</span>,
    <span class="hljs-attr">direction</span>: <span class="hljs-literal">false</span>,
    <span class="hljs-attr">speed</span>: <span class="hljs-number">188</span>,
    <span class="hljs-attr">vehicles</span>: [
      { <span class="hljs-attr">initialTileIndex</span>: <span class="hljs-number">-4</span>, <span class="hljs-attr">color</span>: <span class="hljs-number">0xbdb638</span> },
      { <span class="hljs-attr">initialTileIndex</span>: <span class="hljs-number">-1</span>, <span class="hljs-attr">color</span>: <span class="hljs-number">0x78b14b</span> },
      { <span class="hljs-attr">initialTileIndex</span>: <span class="hljs-number">4</span>, <span class="hljs-attr">color</span>: <span class="hljs-number">0xa52523</span> },
    ],
  },
  {
    <span class="hljs-attr">type</span>: <span class="hljs-string">"forest"</span>,
    <span class="hljs-attr">trees</span>: [
      { <span class="hljs-attr">tileIndex</span>: <span class="hljs-number">-5</span>, <span class="hljs-attr">height</span>: <span class="hljs-number">50</span> },
      { <span class="hljs-attr">tileIndex</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">height</span>: <span class="hljs-number">30</span> },
      { <span class="hljs-attr">tileIndex</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">height</span>: <span class="hljs-number">50</span> },
    ],
  },
  {
    <span class="hljs-attr">type</span>: <span class="hljs-string">"car"</span>,
    <span class="hljs-attr">direction</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-attr">speed</span>: <span class="hljs-number">125</span>,
    <span class="hljs-attr">vehicles</span>: [
      { <span class="hljs-attr">initialTileIndex</span>: <span class="hljs-number">-4</span>, <span class="hljs-attr">color</span>: <span class="hljs-number">0x78b14b</span> },
      { <span class="hljs-attr">initialTileIndex</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">color</span>: <span class="hljs-number">0xbdb638</span> },
      { <span class="hljs-attr">initialTileIndex</span>: <span class="hljs-number">5</span>, <span class="hljs-attr">color</span>: <span class="hljs-number">0xbdb638</span> },
    ],
  },
  {
    <span class="hljs-attr">type</span>: <span class="hljs-string">"forest"</span>,
    <span class="hljs-attr">trees</span>: [
      { <span class="hljs-attr">tileIndex</span>: <span class="hljs-number">-8</span>, <span class="hljs-attr">height</span>: <span class="hljs-number">30</span> },
      { <span class="hljs-attr">tileIndex</span>: <span class="hljs-number">-3</span>, <span class="hljs-attr">height</span>: <span class="hljs-number">50</span> },
      { <span class="hljs-attr">tileIndex</span>: <span class="hljs-number">2</span>, <span class="hljs-attr">height</span>: <span class="hljs-number">30</span> },
    ],
  },
];

. . .
</code></pre>
<p>This article does not cover truck lanes, but they follow a similar structure. The code for it can be found at <a target="_blank" href="http://JavaScriptGameTutorials.com">JavaScriptGameTutorials.com</a>.</p>
<h2 id="heading-how-to-animate-the-cars">How to Animate the Cars</h2>
<p>Let's move on and animate the cars in their lanes according to their speed and direction. To move the vehicles, we first need to be able to access them. So far, we have added them to the scene, and theoretically, we could traverse the scene and figure out which object represents a vehicle. But it's much easier to collect their references in our metadata and access them through these references.</p>
<p>Let's modify the Map generation. After generating a car, we not only add them to the container group but also save the reference together with their metadata. After this, we can go to the metadata and access each vehicle in the scene.</p>
<pre><code class="lang-javascript">. . .

metadata.forEach(<span class="hljs-function">(<span class="hljs-params">rowData, index</span>) =&gt;</span> {
  <span class="hljs-keyword">const</span> rowIndex = index + <span class="hljs-number">1</span>;

  <span class="hljs-keyword">if</span> (rowData.type === <span class="hljs-string">"forest"</span>) {
    <span class="hljs-keyword">const</span> row = Grass(rowIndex);

    rowData.trees.forEach(<span class="hljs-function">(<span class="hljs-params">{ tileIndex, height }</span>) =&gt;</span> {
      <span class="hljs-keyword">const</span> three = Tree(tileIndex, height);
      row.add(three);
    });

    map.add(row);
  }

  <span class="hljs-keyword">if</span> (rowData.type === <span class="hljs-string">"car"</span>) {
    <span class="hljs-keyword">const</span> row = Road(rowIndex);

    rowData.vehicles.forEach(<span class="hljs-function">(<span class="hljs-params">vehicle</span>) =&gt;</span> {
      <span class="hljs-keyword">const</span> car = Car(
        vehicle.initialTileIndex,
        rowData.direction,
        vehicle.color
      );
      vehicle.ref = car; <span class="hljs-comment">// Add a reference to the car object in metadata</span>
      row.add(car);
    });

    map.add(row);
  }
});
</code></pre>
<p>Next, let's go to the main file and define an animate function that will be called on every animation frame. For now, we only call the <strong>animateVehicles</strong> function, which we will define next. Later, we will extend this function with logic to animate the player and to have hit detection.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> THREE <span class="hljs-keyword">from</span> <span class="hljs-string">"three"</span>;
<span class="hljs-keyword">import</span> { Renderer } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Renderer"</span>;
<span class="hljs-keyword">import</span> { Camera } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Camera"</span>;
<span class="hljs-keyword">import</span> { player } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Player"</span>;
<span class="hljs-keyword">import</span> { map } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Map"</span>;
<span class="hljs-keyword">import</span> { animateVehicles } <span class="hljs-keyword">from</span> <span class="hljs-string">"./animateVehicles"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">"./style.css"</span>;

<span class="hljs-keyword">const</span> scene = <span class="hljs-keyword">new</span> THREE.Scene();
scene.add(player);
scene.add(map);

<span class="hljs-keyword">const</span> ambientLight = <span class="hljs-keyword">new</span> THREE.AmbientLight();
scene.add(ambientLight);

<span class="hljs-keyword">const</span> dirLight = <span class="hljs-keyword">new</span> THREE.DirectionalLight();
dirLight.position.set(<span class="hljs-number">-100</span>, <span class="hljs-number">-100</span>, <span class="hljs-number">200</span>);
scene.add(dirLight);

<span class="hljs-keyword">const</span> camera = Camera();
scene.add(camera);

<span class="hljs-keyword">const</span> renderer = Renderer();
renderer.setAnimationLoop(animate);

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">animate</span>(<span class="hljs-params"></span>) </span>{
  animateVehicles();

  renderer.render(scene, camera);
}
</code></pre>
<p>We also move the renderer render call here to render the scene on every animation loop. To call this function on every frame, we pass it on to the renderer’s <strong>setAnimationLoop</strong> function. This is similar to <strong>requestAnimationFrame</strong> in plain JavaScript, except that it calls itself at the end of the function, so we don't have to call it again.</p>
<p>Now, let's implement the <strong>animateVehicles</strong> function. As this function is part of the animate function, this function is called on every animation frame. Here, we use a Three.js clock to calculate how much time passed between the animation frames. Then, we loop over the metadata, take every vehicle from every car or truck lane, and move them along the x-axis based on their speed, direction, and the time passed.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> THREE <span class="hljs-keyword">from</span> <span class="hljs-string">"three"</span>;
<span class="hljs-keyword">import</span> { metadata <span class="hljs-keyword">as</span> rows } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Map"</span>;
<span class="hljs-keyword">import</span> { minTileIndex, maxTileIndex, tileSize } <span class="hljs-keyword">from</span> <span class="hljs-string">"./constants"</span>;

<span class="hljs-keyword">const</span> clock = <span class="hljs-keyword">new</span> THREE.Clock();

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">animateVehicles</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> delta = clock.getDelta();

  <span class="hljs-comment">// Animate cars and trucks</span>
  rows.forEach(<span class="hljs-function">(<span class="hljs-params">rowData</span>) =&gt;</span> {
    <span class="hljs-keyword">if</span> (rowData.type === <span class="hljs-string">"car"</span> || rowData.type === <span class="hljs-string">"truck"</span>) {
      <span class="hljs-keyword">const</span> beginningOfRow = (minTileIndex - <span class="hljs-number">2</span>) * tileSize;
      <span class="hljs-keyword">const</span> endOfRow = (maxTileIndex + <span class="hljs-number">2</span>) * tileSize;

      rowData.vehicles.forEach(<span class="hljs-function">(<span class="hljs-params">{ ref }</span>) =&gt;</span> {
        <span class="hljs-keyword">if</span> (!ref) <span class="hljs-keyword">throw</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">"Vehicle reference is missing"</span>);

        <span class="hljs-keyword">if</span> (rowData.direction) {
          ref.position.x =
            ref.position.x &gt; endOfRow
              ? beginningOfRow
              : ref.position.x + rowData.speed * delta;
        } <span class="hljs-keyword">else</span> {
          ref.position.x =
            ref.position.x &lt; beginningOfRow
              ? endOfRow
              : ref.position.x - rowData.speed * delta;
        }
      });
    }
  });
}
</code></pre>
<p>If a car reaches the end of the lane, we respawn it at the other end, depending on its direction. This creates an infinite loop in which cars go from left to right or right to left, depending on their direction. Once they reach the end of the lane, they start over from the beginning. With this function, we should have a scene where all the cars are moving in their lanes.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739808359961/eca84295-09d7-463a-8d29-e5f8069bb673.png" alt="The cars move in an infinite loop" class="image--center mx-auto" width="3840" height="2160" loading="lazy"></p>
<h2 id="heading-how-to-move-the-player">How to Move the Player</h2>
<p>Now, let's move on to animating the player. Moving the player on the map is more complex than moving the vehicles. The player can move in all directions, bump into trees, or get hit by cars, and it shouldn't be able to move outside the map.</p>
<p>In this chapter, we are focusing on two parts: collecting user inputs and executing the movement commands. Player movement is not instant – we need to collect the movement commands into a queue and execute them one by one. We are going to collect user inputs and put them into a queue.</p>
<h3 id="heading-collecting-user-inputs">Collecting User Inputs</h3>
<p>To check the movement commands, let's extend the player component with state. We keep track of the player's position and the movement queue. The player starts at the middle of the first row, and the move queue is initially empty.</p>
<p>We will also export two functions: <strong>queueMove</strong> adds the movement command to the end of the move queue, and the <strong>stepCompleted</strong> function removes the first movement command from the queue and updates the player's position accordingly.</p>
<pre><code class="lang-javascript">. . .

export <span class="hljs-keyword">const</span> position = {
  <span class="hljs-attr">currentRow</span>: <span class="hljs-number">0</span>,
  <span class="hljs-attr">currentTile</span>: <span class="hljs-number">0</span>,
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> movesQueue = [];

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">queueMove</span>(<span class="hljs-params">direction</span>) </span>{
  movesQueue.push(direction);
}

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">stepCompleted</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> direction = movesQueue.shift();

  <span class="hljs-keyword">if</span> (direction === <span class="hljs-string">"forward"</span>) position.currentRow += <span class="hljs-number">1</span>;
  <span class="hljs-keyword">if</span> (direction === <span class="hljs-string">"backward"</span>) position.currentRow -= <span class="hljs-number">1</span>;
  <span class="hljs-keyword">if</span> (direction === <span class="hljs-string">"left"</span>) position.currentTile -= <span class="hljs-number">1</span>;
  <span class="hljs-keyword">if</span> (direction === <span class="hljs-string">"right"</span>) position.currentTile += <span class="hljs-number">1</span>;
}
</code></pre>
<p>Now, we can add event listeners for keyboard events to listen to the arrow keys. They all call the player's <strong>queueMove</strong> function, which we just defined for the player with the corresponding direction.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> { queueMove } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Player"</span>;

<span class="hljs-built_in">window</span>.addEventListener(<span class="hljs-string">"keydown"</span>, <span class="hljs-function">(<span class="hljs-params">event</span>) =&gt;</span> {
  <span class="hljs-keyword">if</span> (event.key === <span class="hljs-string">"ArrowUp"</span>) {
    queueMove(<span class="hljs-string">"forward"</span>);
  } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (event.key === <span class="hljs-string">"ArrowDown"</span>) {
    queueMove(<span class="hljs-string">"backward"</span>);
  } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (event.key === <span class="hljs-string">"ArrowLeft"</span>) {
    queueMove(<span class="hljs-string">"left"</span>);
  } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (event.key === <span class="hljs-string">"ArrowRight"</span>) {
    queueMove(<span class="hljs-string">"right"</span>);
  }
});
</code></pre>
<p>After defining the event listeners, we also have to import them into the main file so that they work.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> THREE <span class="hljs-keyword">from</span> <span class="hljs-string">"three"</span>;
<span class="hljs-keyword">import</span> { Renderer } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Renderer"</span>;
<span class="hljs-keyword">import</span> { Camera } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Camera"</span>;
<span class="hljs-keyword">import</span> { player } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Player"</span>;
<span class="hljs-keyword">import</span> { map } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Map"</span>;
<span class="hljs-keyword">import</span> { animateVehicles } <span class="hljs-keyword">from</span> <span class="hljs-string">"./animateVehicles"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">"./style.css"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">"./collectUserInput"</span>; <span class="hljs-comment">// Import event listeners</span>

. . .
</code></pre>
<h3 id="heading-executing-movement-commands">Executing Movement Commands</h3>
<p>So far, we have collected user inputs and put each command into the <strong>movesQueue</strong> array in the player component. Now, it's time to execute these commands one by one and animate the player.</p>
<p>Let's create a new function called <strong>animatePlayer</strong>. Its main goal is to take each move command from the <strong>moveQueue</strong> one by one, calculate the player's progress toward executing a step, and position the player accordingly.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739808997988/8adb70d2-bf5d-4e65-8ed9-29e8f297b487.png" alt="The player movement" class="image--center mx-auto" width="1590" height="892" loading="lazy"></p>
<p>This function animates the player frame by frame. It will also be part of the animate function. We also use a separate move clock that measures each step individually. We pass on false to the clock constructor so it doesn't start automatically. The clock only starts at the beginning of a step. At each animation frame, first, we check if there are any more steps to take, and if there are and we don't currently process a step, then we can start the clock. Once the clock is ticking we animate the player from tile to tile with each step.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> THREE <span class="hljs-keyword">from</span> <span class="hljs-string">"three"</span>;
<span class="hljs-keyword">import</span> { movesQueue, stepCompleted } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Player"</span>;

<span class="hljs-keyword">const</span> moveClock = <span class="hljs-keyword">new</span> THREE.Clock(<span class="hljs-literal">false</span>);

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">animatePlayer</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">if</span> (!movesQueue.length) <span class="hljs-keyword">return</span>;

  <span class="hljs-keyword">if</span> (!moveClock.running) moveClock.start();

  <span class="hljs-keyword">const</span> stepTime = <span class="hljs-number">0.2</span>; <span class="hljs-comment">// Seconds it takes to take a step</span>
  <span class="hljs-keyword">const</span> progress = <span class="hljs-built_in">Math</span>.min(<span class="hljs-number">1</span>, moveClock.getElapsedTime() / stepTime);

  setPosition(progress);

  <span class="hljs-comment">// Once a step has ended</span>
  <span class="hljs-keyword">if</span> (progress &gt;= <span class="hljs-number">1</span>) {
    stepCompleted();
    moveClock.stop();
  }
}

. . .
</code></pre>
<p>We use the move clock to calculate the progress between the two tiles. The progress indicator can be a number between zero and one. Zero means that the player is still at the beginning of the step, and one means that it’s arrived at its new position.</p>
<p>At each animation frame, we call the <strong>setPosition</strong> function to set the player’s position according to the progress. Once we finish a step, we call the <strong>stepCompleted</strong> function to update the player's position and stop the clock. If there are any more move commands in the <strong>movesQueue</strong>, the clock will restart in the following animation frame.</p>
<p>Now that we know how to calculate the progress for each step, let's look into how to set the player's position based on the progress. The player will jump from tile to tile. Let's break this down into two parts: the movement's horizontal and vertical components.</p>
<p>The player moves from the current tile to the next tile in the direction of the move command. We calculate the player's start and end position based on the current tile and the direction of the move command. Then, we use linear interpolation with a utility function that Three.js provides. This will interpolate between the start and end positions based on the progress.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> THREE <span class="hljs-keyword">from</span> <span class="hljs-string">"three"</span>;
<span class="hljs-keyword">import</span> {
  player,
  position,
  movesQueue,
  stepCompleted,
} <span class="hljs-keyword">from</span> <span class="hljs-string">"./components/Player"</span>;
<span class="hljs-keyword">import</span> { tileSize } <span class="hljs-keyword">from</span> <span class="hljs-string">"./constants"</span>;

. . .

function setPosition(progress) {
  <span class="hljs-keyword">const</span> startX = position.currentTile * tileSize;
  <span class="hljs-keyword">const</span> startY = position.currentRow * tileSize;
  <span class="hljs-keyword">let</span> endX = startX;
  <span class="hljs-keyword">let</span> endY = startY;

  <span class="hljs-keyword">if</span> (movesQueue[<span class="hljs-number">0</span>] === <span class="hljs-string">"left"</span>) endX -= tileSize;
  <span class="hljs-keyword">if</span> (movesQueue[<span class="hljs-number">0</span>] === <span class="hljs-string">"right"</span>) endX += tileSize;
  <span class="hljs-keyword">if</span> (movesQueue[<span class="hljs-number">0</span>] === <span class="hljs-string">"forward"</span>) endY += tileSize;
  <span class="hljs-keyword">if</span> (movesQueue[<span class="hljs-number">0</span>] === <span class="hljs-string">"backward"</span>) endY -= tileSize;

  player.position.x = THREE.MathUtils.lerp(startX, endX, progress);
  player.position.y = THREE.MathUtils.lerp(startY, endY, progress);
  player.children[<span class="hljs-number">0</span>].position.z = <span class="hljs-built_in">Math</span>.sin(progress * <span class="hljs-built_in">Math</span>.PI) * <span class="hljs-number">8</span> + <span class="hljs-number">10</span>;
}
</code></pre>
<p>For the vertical component, we use a sine function to make it look like jumping. We are basically mapping the progress to the first part of a sine wave.</p>
<p>Below you can see what a sine wave looks like. It goes from 0 to 2 Pi. So if you multiply the progress value, which is going from 0 to 1 with Pi, then the progress will map into the first half of this sine wave. The sign function then will give us a value between zero and one.</p>
<p>To make the jump look higher, we can multiply this with a value. In this case, we multiply the result of the sine function by eight, so as a result, the player will have a jump where the maximum height of the jump will be eight units.</p>
<p>We also need to add the original Z position to the value – otherwise, the player will sink halfway into the ground after the first step.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739810397620/89119f6e-ced5-4cdb-8254-96fbf66479ed.png" alt="For the vertical movement we use a sine wave" class="image--center mx-auto" width="2032" height="1146" loading="lazy"></p>
<p>Now that we’ve defined the <strong>animatePlayer</strong> function, let's add it to the animate loop.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> THREE <span class="hljs-keyword">from</span> <span class="hljs-string">"three"</span>;
<span class="hljs-keyword">import</span> { Renderer } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Renderer"</span>;
<span class="hljs-keyword">import</span> { Camera } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Camera"</span>;
<span class="hljs-keyword">import</span> { DirectionalLight } <span class="hljs-keyword">from</span> <span class="hljs-string">"./DirectionalLight"</span>;
<span class="hljs-keyword">import</span> { player } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Player"</span>;
<span class="hljs-keyword">import</span> { map, initializeMap } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Map"</span>;
<span class="hljs-keyword">import</span> { animateVehicles } <span class="hljs-keyword">from</span> <span class="hljs-string">"./animateVehicles"</span>;
<span class="hljs-keyword">import</span> { animatePlayer } <span class="hljs-keyword">from</span> <span class="hljs-string">"./animatePlayer"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">"./style.css"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">"./collectUserInput"</span>;

. . .

function animate() {
  animateVehicles();
  animatePlayer();

  renderer.render(scene, camera);
}
</code></pre>
<p>If you did everything right, the player should be able to move around the game board, moving forward, backward, left, and right. But we haven't added any hit detection. So far, the player can move through trees and vehicles and even get off the game board. Let's fix these issues in the following steps.</p>
<h3 id="heading-restricting-player-movement">Restricting Player Movement</h3>
<p>Let’s make sure that the player can’t end up in a position that’s invalid. We will check if a move is valid by calculating where it will take the player. If the player would end up in a position outside of the map or in a tile occupied by a tree, we will ignore that move command.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739810555283/310afd32-6e5a-4143-bbe2-e0da558bd6d7.png" alt="Calculating where the player will end up" class="image--center mx-auto" width="1662" height="1146" loading="lazy"></p>
<p>First, we need to calculate where the player would end up if they made a particular move. Whenever we add a new move to the queue, we need to calculate where the player would end up if they made all the moves in the queue and take the current move command. We create a utility function that takes the player's current position and an array of moves and returns the player's final position.</p>
<p>For instance, if the player's current position is 0,0, staying in the middle of the first row, and the moves are forward and left, then the final position will be row 1 tile -1.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">calculateFinalPosition</span>(<span class="hljs-params">currentPosition, moves</span>) </span>{
  <span class="hljs-keyword">return</span> moves.reduce(<span class="hljs-function">(<span class="hljs-params">position, direction</span>) =&gt;</span> {
    <span class="hljs-keyword">if</span> (direction === <span class="hljs-string">"forward"</span>)
      <span class="hljs-keyword">return</span> {
        <span class="hljs-attr">rowIndex</span>: position.rowIndex + <span class="hljs-number">1</span>,
        <span class="hljs-attr">tileIndex</span>: position.tileIndex,
      };
    <span class="hljs-keyword">if</span> (direction === <span class="hljs-string">"backward"</span>)
      <span class="hljs-keyword">return</span> {
        <span class="hljs-attr">rowIndex</span>: position.rowIndex - <span class="hljs-number">1</span>,
        <span class="hljs-attr">tileIndex</span>: position.tileIndex,
      };
    <span class="hljs-keyword">if</span> (direction === <span class="hljs-string">"left"</span>)
      <span class="hljs-keyword">return</span> {
        <span class="hljs-attr">rowIndex</span>: position.rowIndex,
        <span class="hljs-attr">tileIndex</span>: position.tileIndex - <span class="hljs-number">1</span>,
      };
    <span class="hljs-keyword">if</span> (direction === <span class="hljs-string">"right"</span>)
      <span class="hljs-keyword">return</span> {
        <span class="hljs-attr">rowIndex</span>: position.rowIndex,
        <span class="hljs-attr">tileIndex</span>: position.tileIndex + <span class="hljs-number">1</span>,
      };
    <span class="hljs-keyword">return</span> position;
  }, currentPosition);
}
</code></pre>
<p>Now that we have this utility function to calculate where the player will end up after taking a move, let's create another utility function to calculate whether the player would end up in a valid or invalid position. In this function, we use the <strong>calculateFinalPosition</strong> function that we just created. Then, we’ll check if the player would end up outside the map or on a tile occupied by a tree.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739810684106/71c45122-b10c-4623-b575-184c1cf1fecb.png" alt="Check if the player bumps into a tree" class="image--center mx-auto" width="1846" height="1146" loading="lazy"></p>
<p>If the move is invalid, we return false. First, we check if the final position is before the starting row or if the tile number is outside the range of the tiles. Then, we check the metadata of the row the player will end up in. Here, the index is off by one because the row metadata doesn't include the starting row. If we end up in a forest row, we check whether a tree occupies the tile we move to. If any of this is true, we return false.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> { calculateFinalPosition } <span class="hljs-keyword">from</span> <span class="hljs-string">"./calculateFinalPosition"</span>;
<span class="hljs-keyword">import</span> { minTileIndex, maxTileIndex } <span class="hljs-keyword">from</span> <span class="hljs-string">"./constants"</span>;
<span class="hljs-keyword">import</span> { metadata <span class="hljs-keyword">as</span> rows } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Map"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">endsUpInValidPosition</span>(<span class="hljs-params">currentPosition, moves</span>) </span>{
  <span class="hljs-comment">// Calculate where the player would end up after the move</span>
  <span class="hljs-keyword">const</span> finalPosition = calculateFinalPosition(
    currentPosition,
    moves
  );

  <span class="hljs-comment">// Detect if we hit the edge of the board</span>
  <span class="hljs-keyword">if</span> (
    finalPosition.rowIndex === <span class="hljs-number">-1</span> ||
    finalPosition.tileIndex === minTileIndex - <span class="hljs-number">1</span> ||
    finalPosition.tileIndex === maxTileIndex + <span class="hljs-number">1</span>
  ) {
    <span class="hljs-comment">// Invalid move, ignore move command</span>
    <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
  }

  <span class="hljs-comment">// Detect if we hit a tree</span>
  <span class="hljs-keyword">const</span> finalRow = rows[finalPosition.rowIndex - <span class="hljs-number">1</span>];
  <span class="hljs-keyword">if</span> (
    finalRow &amp;&amp;
    finalRow.type === <span class="hljs-string">"forest"</span> &amp;&amp;
    finalRow.trees.some(
      <span class="hljs-function">(<span class="hljs-params">tree</span>) =&gt;</span> tree.tileIndex === finalPosition.tileIndex
    )
  ) {
    <span class="hljs-comment">// Invalid move, ignore move command</span>
    <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
  }

  <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
}
</code></pre>
<p>Finally, let's extend the player's <strong>queueMove</strong> function with the <strong>endsUpInValidPosition</strong> function to check if a move is valid. If the <strong>endsUpInValidPosition</strong> function returns false, we cannot take this step. In this case, we return early from the function before the move is added to the <strong>movesQueue</strong> array. So we are ignoring the move.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> THREE <span class="hljs-keyword">from</span> <span class="hljs-string">"three"</span>;
<span class="hljs-keyword">import</span> { endsUpInValidPosition } <span class="hljs-keyword">from</span> <span class="hljs-string">"./endsUpInValidPosition"</span>;

. . .

export <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">queueMove</span>(<span class="hljs-params">direction</span>) </span>{
  <span class="hljs-keyword">const</span> isValidMove = endsUpInValidPosition(
    {
      <span class="hljs-attr">rowIndex</span>: position.currentRow,
      <span class="hljs-attr">tileIndex</span>: position.currentTile,
    },
    [...movesQueue, direction]
  );

  <span class="hljs-keyword">if</span> (!isValidMove) <span class="hljs-keyword">return</span>; <span class="hljs-comment">// Return if the move is invalid</span>

  movesQueue.push(direction);
}

. . .
</code></pre>
<p>This way, as you can see, you can move around the map – but you can never move before the first row, you can't go too far to the left or too far to the right, and you also can’t go through a tree anymore.</p>
<h2 id="heading-hit-detection">Hit Detection</h2>
<p>To finish the game, let's add hit detection. We check if the player gets hit by a vehicle, and if so, we show an alert popup.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739826639155/1497d588-bf60-4fdc-a185-a01a781beafb.png" alt="Calculating bounding boxes for hit detection" class="image--center mx-auto" width="1846" height="1146" loading="lazy"></p>
<p>Let's define another function to define hit detection. We check if the player intersects with any of the vehicles. In this function, we check which row the player is currently in. The index is off by one because the row metadata does not include the starting row. If the player is in the starting row, we get undefined. We ignore that case. If the player is in a car or truck lane, we loop over the vehicles in the row and check if they intersect with the player. We create bounding boxes for the player and the vehicle to check for intersections.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> THREE <span class="hljs-keyword">from</span> <span class="hljs-string">"three"</span>;
<span class="hljs-keyword">import</span> { metadata <span class="hljs-keyword">as</span> rows } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Map"</span>;
<span class="hljs-keyword">import</span> { player, position } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Player"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">hitTest</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> row = rows[position.currentRow - <span class="hljs-number">1</span>];
  <span class="hljs-keyword">if</span> (!row) <span class="hljs-keyword">return</span>;

  <span class="hljs-keyword">if</span> (row.type === <span class="hljs-string">"car"</span> || row.type === <span class="hljs-string">"truck"</span>) {
    <span class="hljs-keyword">const</span> playerBoundingBox = <span class="hljs-keyword">new</span> THREE.Box3();
    playerBoundingBox.setFromObject(player);

    row.vehicles.forEach(<span class="hljs-function">(<span class="hljs-params">{ ref }</span>) =&gt;</span> {
      <span class="hljs-keyword">if</span> (!ref) <span class="hljs-keyword">throw</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">"Vehicle reference is missing"</span>);

      <span class="hljs-keyword">const</span> vehicleBoundingBox = <span class="hljs-keyword">new</span> THREE.Box3();
      vehicleBoundingBox.setFromObject(ref);

      <span class="hljs-keyword">if</span> (playerBoundingBox.intersectsBox(vehicleBoundingBox)) {
        <span class="hljs-built_in">window</span>.alert(<span class="hljs-string">"Game over!"</span>);
        <span class="hljs-built_in">window</span>.location.reload();
      }
    });
  }
}
</code></pre>
<p>If the bounding boxes intersect, we show an alert. Once the user clicks OK on the alert, we reload the page. We call this function in the <strong>animate</strong> function, which will run it on every frame.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> THREE <span class="hljs-keyword">from</span> <span class="hljs-string">"three"</span>;
<span class="hljs-keyword">import</span> { Renderer } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Renderer"</span>;
<span class="hljs-keyword">import</span> { Camera } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Camera"</span>;
<span class="hljs-keyword">import</span> { player } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Player"</span>;
<span class="hljs-keyword">import</span> { map } <span class="hljs-keyword">from</span> <span class="hljs-string">"./Map"</span>;
<span class="hljs-keyword">import</span> { animateVehicles } <span class="hljs-keyword">from</span> <span class="hljs-string">"./animateVehicles"</span>;
<span class="hljs-keyword">import</span> { animatePlayer } <span class="hljs-keyword">from</span> <span class="hljs-string">"./animatePlayer"</span>;
<span class="hljs-keyword">import</span> { hitTest } <span class="hljs-keyword">from</span> <span class="hljs-string">"./hitTest"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">"./style.css"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">"./collectUserInput"</span>;

. . .

function animate() {
  animateVehicles();
  animatePlayer();
  hitTest(); <span class="hljs-comment">// Add hit detection</span>

  renderer.render(scene, camera);
}
</code></pre>
<h2 id="heading-next-steps">Next Steps</h2>
<p>Congratulations, you’ve reached the end of this tutorial, and we’ve covered all the main features of the game. We rendered a map, animated the vehicles, added event handling for the player, and added hit detection.</p>
<p>I hope you had great fun creating this game. This game, of course, is far from perfect, and there are various improvements you can make if you’d like to keep working on it.</p>
<p>You can find the extended tutorial with interactive demos on <a target="_blank" href="http://JavaScriptGameTutorials.com">JavaScriptGameTutorials.com</a>. There, we also cover how to add shadows and truck lanes and how to generate an infinite number of rows as the player moves forward. We also add UI elements for the controls and the score indicator, and we add a result screen with a button to reset the game.</p>
<p>Alternatively, you can find the extended tutorial on <a target="_blank" href="https://www.youtube.com/watch?v=vNr3_hQ3Bws&amp;ab_channel=HunorM%C3%A1rtonBorb%C3%A9ly">YouTube</a>.</p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/vNr3_hQ3Bws" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
