Back to blog

Adding Physics to Simulations

Improve models by adding real-world laws of physics
June 28th, 2021
Adding Physics to Simulations

The open-source and newly-released HASH Physics Library provides behaviors that enable the easy addition of simple physics to simulations in HASH. The library is written in Rust, which means that it's optimized to run in our engine. The HASH docs contain more information about each of the behaviors.

You can add the physics library to your simulation from the "Add to Simulation" panel in the lefthand sidebar of hCore.

Let's look at a few simulations to better understand how to make use of the library:

A Simple Spring

Open the "Simple Spring" simulation ->

The key behavior for the physics library is @hash/physics/ This behavior translates forces exerted onto the object into changes in motion and position. You will typically pair it with other custom or library behaviors which will add forces to the agent.

The @hash/physics/ behavior is a great example of a behavior to pair with It simulates the effects of a spring attached between the agent and a neighbor. If we add both behaviors to an agent, and provide it with the proper parameters we should see the agent move in classic periodic motion. Here is the init.js file:

const init = (context) => {
  const { k, length, damping } = context.globals();
  let agents = [
      agent_id: "anchor",
      position: [0, 0, 0],
      behaviors: ["@hash/physics/", "@hash/physics/"],
      position: [4, 0, 0],
      mass: 10,
      color: "blue",
      shape: "sphere",
      search_radius: 10,
      springs: [
          agent_id: "anchor",
      network_neighbor_ids: ["anchor"],
  return agents;

There are three key parameters you can modify for a spring: the resting length, the spring constant k, and the damping constant B. Modify the global variables in the embedded simulation to see the different effects.


Open the "Pendulum" simulation ->

If we increase the value of k for a spring by orders of magnitude, we can use them to simulate rigid rods. The example below is a pendulum which uses a spring for this exact purpose.

To define the pendulum, we've attached an agent with mass to a pivot point. The distance between them is exactly the resting length of the "spring" (since it is mimicking a rigid object). We also need to add one more behavior to the mass agent, one that will simulate the force of gravity and cause it to swing down. The @hash/physics/ behavior does this. We've simply added it it into the behavior chain, and added a gravity constant in globals.json. Here is the new agent definition:

        "behaviors": ["@hash/physics/", "@hash/physics/", "@hash/physics/"],
        "position": [5, 0, 10],
        "mass": 10,
        "shape": "sphere",
        "force": [0, 0, 0],
        "agent_id": "ball1",
        "network_neighbor_ids": ["ball0"],
          "agent_id": "ball0",
          "k": 10000,
          "length": 5

We can easily create more complex systems by connecting more mass agents in a similar fashion. Change the slider for the pendulum_segments field and reset the simulation to experiment with this.

Colliding Agents

Open the "Colliding Agents" simulation ->

The final physics behavior to explore is @hash/physics/ This behavior, like, handles an interaction between agents. It will cause two agents to bounce off each other if they get too close (within 0.5 units). The collision is perfectly elastic, conserving both momentum and energy. Here is the definition for one of the colliding agents:

    "shape": "sphere",
    "color": "blue",
    "behaviors": ["@hash/physics/", "@hash/physics/", "custom.js"],
    "mass": 10,
    "velocity": [0, 0, 0]

You can fork and modify any of these simulations, and you can also build your own from scratch by importing the Physics Library. Let us know what you're building on the HASH Discord channel.

Create a free account

Sign up to try HASH out for yourself, and see what all the fuss is about

By signing up you agree to our terms and conditions and privacy policy