The Physics Thread

Since Rayon is multithreaded, concurrency issues may arise if proper steps aren't taken in your mod. This is mainly whenever a change needs to be made to a rigid body or a physics space which is being actively simulated. When in doubt, there are several ways to execute code on the physics thread to ensure consistency.
Queue a task
Use an event
Queueing a task on the physics thread is the most versatile option since it can be done anywhere. The only thing you need is a Level , Minecraft , or MinecraftServer object. The only downside is the amount of time it may take to start executing is not a guaranteed amount. Here's a few examples where we change the gravity of the physics space:
PhysicsThread.get((Minecraft) client).execute(this::setGravity);
PhysicsThread.get((MinecraftServer) server).execute(this::setGravity);
PhysicsThread.get(level).execute(this::setGravity);
MineraftSpace.get(level).getWorkerThread().execute(this::setGravity);
public void setGravity(MinecraftSpace space) {
space.setGravity(new Vector3f(0, -9.81, 0));
}
Registering an event within PhysicsSpaceEvents is best when you need it to run consistently every tick. Here's how a registered event might look:
PhysicsSpaceEvents.STEP.register(space-> {
System.out.println("Stepping physics space!");
});

Force vs Impulse

When applying forces to a rigid body, there are two different calls that can be made:
  • applyCentralForce
  • applyCentralImpulse
The difference between them is that applyCentralForce must be called every step of the physics world in order to be effective since it is force applied over time. On the other hand, applyCentralImpulse can be called anytime and at whatever interval you prefer since it represents an instantaneous force.