# 3D Physics Engine Pt. 2

**URL:** <https://discourse.elm-lang.org/t/3d-physics-engine-pt-2/1887>\
**Category:** Show and Tell\
**Created:** [September 4, 2018, 8:36pm UTC](https://discourse.elm-lang.org/t/3d-physics-engine-pt-2/1887 "2018-09-04T20:36:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![unsoundscapes](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/unsoundscapes/32/410_2.png) [@unsoundscapes](https://discourse.elm-lang.org/u/unsoundscapes)\
**Post date:** [September 4, 2018, 8:36pm UTC](https://discourse.elm-lang.org/t/3d-physics-engine-pt-2/1887/1 "2018-09-04T20:36:08Z")

</div>

Hi. This is a continuation of the story about [elm-physics.](https://github.com/w0rm/elm-physics) You can read [the first part here.](https://discourse.elm-lang.org/t/3d-physics-engine/1206)

The most important thing that happened since the original post, is that the project got a second contributor, Paul, who did a lot of work to add tests and benchmarks, improved the performance and implemented support for spheres!

[![47](https://cdck-file-uploads-canada1.s3.dualstack.ca-central-1.amazonaws.com/flex035/uploads/elm_lang/original/2X/3/3b3a0eb17b76108361773f7317d7ab69761af953.png)](http://unsoundscapes.com/elm-physics/examples/spheres/)

I focused on improving demos, added simple shadows, the settings menu and most recently—the fps meter. Making it easier to create demos allowed us to identify and fix certain bugs. Like for example a bug when a sphere would circle around the corner, dive inside the box and then jump:

![43105273-9d07307e-8ed4-11e8-8dc7-f9f28e49e243](https://cdck-file-uploads-canada1.s3.dualstack.ca-central-1.amazonaws.com/flex035/uploads/elm_lang/original/2X/f/f62e577ed21aec0ba08338c277ad9d2c2d40671e.gif)

I set up [this demo](http://unsoundscapes.com/elm-physics/examples/collision-test/) and was able to constantly reproduce the bug. It turned out, we did not continue with collision tests against faces and edges after the first collision with a corner had been detected. A quick solution was to test against faces, edges and corners and find the closest collision point. Paul figured that we could test collisions [in the certain order](https://github.com/w0rm/elm-physics/pull/34). That really improved the performance, as you can see on this screenshot from the wonderful [benchmark](https://github.com/elm-explorations/benchmark):

![50](https://cdck-file-uploads-canada1.s3.dualstack.ca-central-1.amazonaws.com/flex035/uploads/elm_lang/original/2X/4/467394991e0fc864a30ecac157e75a80451da5be.png)

For the last part, I saved the most interesting finding. I’ve been following [Florian’s experiment](https://github.com/Zinggi/elm-webgl-math#plan)—a pure Elm implementation of [linear-algebra](https://github.com/elm-explorations/linear-algebra) (without any Kernel code to store vectors and matrices as typed JavaScript arrays). The physics engine seems like a perfect case to compare!

So I implemented another pure Elm version of linear-algebra with identical API, that defines vectors and matrices as record types, then [ran our existing elm-physics benchmarks](https://github.com/w0rm/elm-physics/pull/39) and came to the same results as Florian.

I think this happens because browsers can optimize objects that have the same shape and make them really fast, while reading data from an array requires some extra checks, and it is pretty costly to have lots of tiny arrays for `Vec3`. [This JavaScript benchmark](https://jsbench.github.io/#00c5236cfdf645df5656c3e680fd2da6) simulates a part of Elm’s WebGL, that packs data for an attribute buffer. It shows that objects are faster!

To conclude this, what if we remove linear-algebra, and use Elm records everywhere including for WebGL?

1. we will remove unnecessary coupling from the Elm compiler, that has linear-algebra types hardcoded for glsl shaders
2. we won’t have [to fix nasty typos in Kernel code](https://github.com/elm-explorations/linear-algebra/search?q=fix&type=Commits), writing Elm is a much better experience
3. we will enable alternative linear-algebra implementations with a reacher functionality, like [ianmackenzie/elm-geometry](https://github.com/ianmackenzie/elm-geometry) or already mentioned [elm-webgl-math](https://github.com/Zinggi/elm-webgl-math), to easily integrate with WebGL
4. and most importantly, it will make the code run faster!

In the meantime, we keep working on elm-physics. The current hot topics are: using spatial indexing to avoid O(n^2) collisions and adding features like user interactions and events. If you feel like you can contribute something like this, please get in touch! Also if you would like to use it for something, please let us know about the missing features, so that we can have a more clear prioritization.

---

<div class="post-metadata">

**Author:** ![ianmackenzie](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/ianmackenzie/32/86_2.png) [@ianmackenzie](https://discourse.elm-lang.org/u/ianmackenzie)\
**Post date:** [September 4, 2018, 9:12pm UTC](https://discourse.elm-lang.org/t/3d-physics-engine-pt-2/1887/2 "2018-09-04T21:12:14Z")

</div>

I’m definitely in favor of moving from the typed array-based types in `linear-algebra` to plain Elm records. I know there was concern that previous benchmarks may have showed that the typed array approach was faster than using Elm records/JavaScript objects (see e.g. [this thread](https://groups.google.com/forum/#!topic/elm-dev/6XJQydHCAeg)), but it really seems like the opposite is true now - perhaps JavaScript VMs do a better job nowadays of optimizing objects (using [hidden classes](https://blog.ghaiklor.com/optimizations-tricks-in-v8-d284b6c8b183) and the like), or Elm emits better/more easily-optimized JavaScript (or both!). If anyone knows what benchmarks were used when originally deciding to base `linear-algebra` on typed arrays, then it would be great to run them again with Elm 0.19 and see what the results are now.

---

<div class="post-metadata">

**Author:** ![unsoundscapes](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/unsoundscapes/32/410_2.png) [@unsoundscapes](https://discourse.elm-lang.org/u/unsoundscapes)\
**Post date:** [September 5, 2018, 8:27am UTC](https://discourse.elm-lang.org/t/3d-physics-engine-pt-2/1887/3 "2018-09-05T08:27:57Z")

</div>

Another reason why I think this might be the case, at least for elm-physics, is that the most time is spent working with `Vec3`. I believe typed arrays are good for storing large amount of data, but when they’re used for tiny things, the overhead is not worth it.

---

<div class="post-metadata">

**Author:** ![zinggi](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/zinggi/32/62_2.png) [@zinggi](https://discourse.elm-lang.org/u/zinggi)\
**Post date:** [September 14, 2018, 2:51pm UTC](https://discourse.elm-lang.org/t/3d-physics-engine-pt-2/1887/8 "2018-09-14T14:51:46Z")

</div>

I’m a bit late to this thread (I was on holidays), but I’ll bump this thread anyway.

Writing a physics engine in elm is an amazing project and I’m very impressed with the results so far!

As for the performance improvements from using a pure elm version of linear algebra, it’s great to have a real world benchmark backing up the results I obtained from micro benchmarking.  
It really looks like in practice, the pure elm approach is faster.  
So I also think it would be best to move to a pure elm implementation for vectors and matrices in WebGL.

I will not update my `elm-webgl-math` library for elm 0.19, as it uses tuples with up to 4 elements, which is no longer possible. But I think Ian’s `elm-geometry` is much nicer anyway. It would be super interesting to have a version of the benchmark with Ian’s library as well.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [September 24, 2018, 2:51pm UTC](https://discourse.elm-lang.org/t/3d-physics-engine-pt-2/1887/9 "2018-09-24T14:51:48Z")

</div>

Closed on September 24, 2018, 2:51pm UTC

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
