Processing multiple units.

I have created a Vehicle Object from The Nature of Code tutorials, and I have made the vehicle create a thread in itself to update the direction that it should take to reach the target point, the thread is called every 2 seconds.
When I create too many objects (1), the computer lagged so much, so I created another object called container_object, the container job is to have multiple vehicles and to use only one thread to update the direction for all child vehicles that the container has, and I removed the thread initialization from the vehicle object and add it to the container.
But when I created multiple containers (2), the program lagged even more than the first time. in the first program (1) the system.threads.length was 248 threads and the average fps was 15-20, and in the second program (2) the system.threads.length was 7 threads and the average fps was 5-9.

  • Why did this happen?
  • Can you tell me how to improve the code?

Set the code to public in settings and forward the link.


you can press A to select all containers. sorry I didn't document anything.

It works very well on my laptop. Chrome-109, Win 8.

I changed the physics engine version to 0.19.

60 fps when there is no movement, 40 when there is movement, at critical moments it drops to 20-30 fps.

version 0.17 - has 10 fps at the critical moment.

(Parameters for a critical situation - select all containers and indicate where to move towards the center of the red boxes.)

This is due to the principle of operation of the physics engine.

When the engine detects a collision, it calculates the location of the collision and sends a message about the event (collision start, collision end) to the object.

It also sends the object a list of the objects it collided with and the point of contact. This is very expensive.

When objects overlap, the physics engine tries to separate both objects (push one object out of the other). This is even more expensive. When there are a lot of objects very close to each other, it will definitely cause further collisions and push attempts.

The program slows down when many objects overlap.

  engine.positionIterations = 2
  engine.velocityIterations = 2

Setting these values allows you to determine how accurately the movement for each "tick" should be simulated.

The higher the value, the greater the accuracy.

For me, at setting 100 you can see that the objects block each other and no longer penetrate each other. There is also no such large effect that some objects move away from the indicated point and stop moving.

 Matter.Engine.update(engine, 1000/system.fps )

Call the update of the physics engine with a variable that determines the current FPS - to get a smooth animation without slowing down when FPS drops.

This is what I tried

 engine.positionIterations = 100
 engine.velocityIterations = 100

it really helped. But there's still the lag of 15-20 fps frame drop. And I noticed that the threads are not doing anything at all, it's the same with or without them.

I'm thinking of this ->

phyThread = every 167 milliseconds do

but I think it's not going to work too. Threads are quite not the right way. What I want to achieve is at least 2k vehicles at the same scene.

the default values for these fields are

  engine.positionIterations = 6
  engine.velocityIterations = 6

I think there is no need to set more than 2-3 times more than the default values.

You can run the entire simulation "outside" MicroScript's "update draw" loop.

Then all physical calculations will be performed by Matter at the frequency you set.

The disadvantage of this solution is that when you stop the execution of the MicroScript program, the physical simulation continues. (you will see this when you stop the program and let it run again).

To do this, in the "init" section to add

   runner = Matter.Runner.create()
   Matter.Runner.run(runner, engine)

by removing the reference to (comment) from the "update" section

// Matter.Engine.update(engine, 1000/system.fps )

I also noticed that in the Firefox browser the Matter engine performs worse (lower FPS, larger drops in critical situations)

Watch this video >> https://www.youtube.com/watch?v=9IULfQH7E90

2000 objects in JavaScript will be a challenge.


Even in Unit and Godot it is difficult to achieve such a result.

I have misunderstood position and velocity iterations, I thought these values represent how much the calculations should be skipped.😅 (it was 2 AM) Now I set them to engine.positionIterations = 2 and engine.velocityIterations = 1, well it's quite good but still drops.

Using the Matter Runner is actually the same as running it in the micro game loop performance-wise.
creating this project is as you said, quite the challenge since js isn't that fast.

Post a reply



Validate your e-mail address to participate in the community