<?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" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by ashley fletcher on Medium]]></title>
        <description><![CDATA[Stories by ashley fletcher on Medium]]></description>
        <link>https://medium.com/@ashleymarkfletcher?source=rss-929ca5a3cd9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*u0ZdtP-6RTWSeOh8.</url>
            <title>Stories by ashley fletcher on Medium</title>
            <link>https://medium.com/@ashleymarkfletcher?source=rss-929ca5a3cd9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 22:24:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ashleymarkfletcher/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Physics in Spark AR]]></title>
            <link>https://medium.com/byte-mission-control/physics-in-spark-ar-10d3a921153a?source=rss-929ca5a3cd9------2</link>
            <guid isPermaLink="false">https://medium.com/p/10d3a921153a</guid>
            <category><![CDATA[physics]]></category>
            <category><![CDATA[spark-ar]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[cannon]]></category>
            <category><![CDATA[augmented-reality]]></category>
            <dc:creator><![CDATA[ashley fletcher]]></dc:creator>
            <pubDate>Fri, 05 Jul 2019 11:20:43 GMT</pubDate>
            <atom:updated>2019-07-05T11:20:43.902Z</atom:updated>
            <content:encoded><![CDATA[<p>How to use Cannon.js physics in Spark AR</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Aj0f5FhlnkI2DPBclBWqdw.png" /></figure><p>I’ve recently been playing with physics in Spark AR, and I thought I would share the journey so far. I’ve also made a project and an npm package that might help kickstart your physics exploration.</p><p>This is by no means a complete guide to physics engines, but it should be enough to get you started. For my experiments so far, I’ve been using Cannon.js.</p><h4>The concept</h4><p>The way I think about physics engines, is that there’s a visual world and a physics world. Rendering a simulation is all about keeping the two worlds in sync.</p><p>The physics engine keeps track of the object positions based on forces. To keep these two worlds in sync, we need to bind the positions of everything in the two worlds together. You don’t have to explicitly say where a scene object should move or how it should behave, the physics engine handles all of that for us!</p><h4>Getting started</h4><p>To help you along with the daunting task of integrating a physics engine, I’ve started an npm package to help with the binding of the Spark world and the Cannon physics world.</p><p><a href="https://github.com/ashleymarkfletcher/spark-ar-physics">ashleymarkfletcher/spark-ar-physics</a></p><h4>How to use</h4><p>The easiest way to get started will be to use the template within the example folder on the repository.</p><p>Run this command in the terminal at the directory of the example project. It will install all the dependencies.</p><pre>npm i</pre><p>Now you have all the dependencies run</p><pre>npm run build</pre><p>This should build a new script.js file in the physicsTest project.</p><p>Open up the physicsTest project, and you should see boxes and spheres falling down.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*UzAMZJKI6WsdppYCgqdceA.gif" /></figure><p>The main source file is the index.js in the src folder of the example. Look in there to see how I use the npm package for binding objects together.</p><h4>The module</h4><p>The module has helpers for keeping together the movement in the Cannon world and the Spark world. It does this by taking something I’ve called World Objects. World Objects contain a scene object and a physics body. Keeping the two parts together makes updating and syncing positions way more straightforward.</p><p>When we have our World Objects created, we pass them to the constructor, which sets up the Cannon world and adds our objects. It also exposes the original Cannon class so that it can be used as standard.</p><p>On every loop, you call update. It iterates over all the World Objects and updates the scene object’s position and rotation based on its physics object. It also steps the physics world forward by a delta time to get the new positions for all the objects.</p><h4><strong>Pitfalls</strong></h4><p>Spark seems to have no sense of a global variable/scope. However, in the Cannon module, it assigns a value to a ‘performance’ variable, that doesn’t seem to get explicitly declared. To fix this, in the Cannon module, I added the variable ‘performance’ into the source at /src/world/World.js on line 501. That is why the package has a local version of the cannon library that isn’t from npm.</p><p>Another pitfall is that the ground plane will be on the wrong axis at the start. That’s where this line comes in to flip it back 90 degrees to where it should be.</p><pre>groundBody.quaternion.setFromAxisAngle(new CANNON.Vec3(1, 0, 0), -Math.PI / 2)</pre><h4>Here’s one I made earlier</h4><p>The first effect I made with the physics engine was a bowling simulation on Instagram which you can try here: <a href="https://www.instagram.com/a/r/?effect_id=2299621396769037">https://www.instagram.com/a/r/?effect_id=2299621396769037</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*OYnTdPXFWa_Fo_LpVrd4lA.gif" /></figure><p>Here’s the repository for that project:</p><p><a href="https://github.com/ashleymarkfletcher/spark-ar-bowling">ashleymarkfletcher/spark-ar-bowling</a></p><p>This project shows a slightly more practical approach to how you might apply physics to an effect, rather than just having objects falling to the ground. It also shows how to use the original cannon object in combination with the module.</p><p>Without too much extra code, we can start to interact with the objects by applying impulses to the physics objects.</p><h4>What I’ve learned</h4><p>The reactive programming in Spark AR clashes with the more traditional style of Cannon.js, which causes quite a bit of a performance hit. An example of this has no frame update loop to plug into. My approach was to just use an interval for the update loop. This type of approach causes a lot of warnings that popup for using too many callbacks. So this is worth noting when starting your own physics-based effects.</p><p>Depending on your use case, you might get away with using physics in production. However, it might be too intensive on resources in Spark AR to get something more advanced.</p><h4>Contribute!</h4><p>This is only the beginning of the physics integration. There’s lots more to do so if you have anything to add just open a pull request or issue on GitHub.</p><p>Syncing positions from Spark to Cannon is still missing in the module. I ran into a lot of issues getting positions of objects as values, so any help on this would be appreciated! This is where the reactive programming becomes the biggest challenge.</p><h4>Start Creating!</h4><p>Over the next couple of weeks, I’ll start documenting the module and include any contributions made. So keep an eye out for improvements and progress updates.</p><p>If you want any more info on physics just drop me a message on Instagram: alt.reality</p><p>For more information about Byte’s use of AR, visit our website <a href="https://bytemissioncontrol.com/">bytemissioncontrol.com</a>.<br>And for any questions contact <a href="/service/mailto:hi@bytelondon.com">hi@bytelondon.com</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=10d3a921153a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/byte-mission-control/physics-in-spark-ar-10d3a921153a">Physics in Spark AR</a> was originally published in <a href="https://medium.com/byte-mission-control">Byte</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Spark AR x Webpack]]></title>
            <link>https://medium.com/byte-mission-control/spark-ar-x-webpack-3ba4dcd05a37?source=rss-929ca5a3cd9------2</link>
            <guid isPermaLink="false">https://medium.com/p/3ba4dcd05a37</guid>
            <category><![CDATA[webpack]]></category>
            <category><![CDATA[augmented-reality]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[spark-ar]]></category>
            <category><![CDATA[facebook]]></category>
            <dc:creator><![CDATA[ashley fletcher]]></dc:creator>
            <pubDate>Mon, 17 Jun 2019 10:29:10 GMT</pubDate>
            <atom:updated>2019-06-17T10:29:10.909Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BYP2Nv0IEYepmqOIPLeBgw.png" /></figure><p>If you’ve spent some time scripting in Spark AR, you’ve probably come across the limitation of one script file per project. This limitation creates a walled environment away from using all the great JavaScript packages that already exist. Using Webpack, we can get around this limitation.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F7JvlHfd7C2GDr7zfZF%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2F7JvlHfd7C2GDr7zfZF&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F7JvlHfd7C2GDr7zfZF%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/a63f8610f05b5ee16edd95b9466f8338/href">https://medium.com/media/a63f8610f05b5ee16edd95b9466f8338/href</a></iframe><h4><strong>Webpack to the rescue</strong></h4><p>If you haven’t come across it before, Webpack is a module bundler primarily for JavaScript, but you can also use it for CSS, images and a whole bunch of things.</p><p>The part we’re interested in is bundling multiple JavaScript modules into one file. This gets you around the one file limit in Spark AR.</p><p>If you want to find out more about Webpack, you can check that out <a href="https://webpack.js.org/"><strong>here</strong></a></p><h4><strong>Getting started</strong></h4><p>To help get you started with Webpack I’ve created a boilerplate template for you to use from the start. It has all the config required and is set up with a hello world example.</p><p><a href="https://github.com/ashleymarkfletcher/spark-ar-boilerplate">ashleymarkfletcher/spark-ar-boilerplate</a></p><h4><strong>How to use</strong></h4><p>Before you get started you’ll need <a href="https://nodejs.org/en/"><strong>Node.js</strong></a> and <a href="https://sparkar.facebook.com/ar-studio/download"><strong>Spark AR</strong></a>.</p><p>Once installed, run this from the terminal in the directory of the project:</p><pre>npm i</pre><p>This installs the dependencies to run Webpack.</p><p>Then run either of these two commands:</p><pre>npm run dev<br>// OR<br>npm run build</pre><p>Dev uses the “watch” flag to look for changes in your code and automatically compile the code into the script.js file. Use this during development to save manually compiling the code every time. The build command just runs the compiler once.</p><p>Once you have Webpack running, you can make changes and import all the files you want, and the Spark AR project should automatically pick up the changes.</p><h4><strong>Webpack config explained</strong></h4><p>All the magic for making Webpack work for Spark AR is in the webpack.config.js file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c1ffb5ec178738b895120e5e621ca001/href">https://medium.com/media/c1ffb5ec178738b895120e5e621ca001/href</a></iframe><p><strong>Output</strong> is where we specify where the bundled JS is going to go. For us, this needs to be the script.js file that Spark AR looks for in the scripts folder.</p><p><strong>Externals</strong> tell Webpack to ignore require statements. Ignoring require statements is vital because Spark has modules to require but doesn’t actually have the modules locally, they get added later on out of our scope. If we didn’t have them set as externals, Webpack would try and bundle those files and fail.</p><h4><strong>Here’s one I made earlier</strong></h4><p>As an experiment, I made an effect using the <a href="https://www.npmjs.com/package/simplex-noise">simplex-noise</a> module. The effect visualizes Simplex noise to generate a voxel map procedurally. Using Webpack made creating this effect much more straightforward by being able to pull in amazing packages to help with the heavy lifting.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*pjyoqr8R3tTxRvr26m7hWA.gif" /></figure><p>Try the effect on Instagram here: <a href="https://www.instagram.com/a/r/?effect_id=411882919538540">https://www.instagram.com/a/r/?effect_id=411882919538540</a></p><p>The code for the effect is here:</p><p><a href="https://github.com/ashleymarkfletcher/procedural-ar">ashleymarkfletcher/procedural-ar</a></p><p>Hopefully, this helps you create more advanced and amazing effects more efficiently.</p><p>For more information about Byte’s use of AR, visit our website <a href="https://bytemissioncontrol.com/">bytemissioncontrol.com</a>.<br>And for any questions contact <a href="/service/mailto:hi@bytelondon.com">hi@bytelondon.com</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3ba4dcd05a37" width="1" height="1" alt=""><hr><p><a href="https://medium.com/byte-mission-control/spark-ar-x-webpack-3ba4dcd05a37">Spark AR x Webpack</a> was originally published in <a href="https://medium.com/byte-mission-control">Byte</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>