I’m trying to implement Dyn4j into my game, and a question I have is: If I want Dyn4j to handle physics (including collisions), will I essentially have 2 worlds:
The jMonkey Scene where objects are visualized
The Dyn4j World-object where physical bodies and their interactions are handled (by Dyn4j)
It’s interesting because I would have logically considered those things to be exactly backwards.
In actual physics terms, force is timeless. f = m a
In physics engines, I’d expect this to be multiplied by time like any other acceleration… but have mass removed. I often find that mass must be premultiplied “out” of the force. For example, in my physics engines I think I treat force just like another acceleration/velocity accumulator.
An impulse is a force over a specific time. So impulse = f * t
A force would be being dragged by a car. Every frame that the force is active, you are being pulled forward.
An impulse would be being kicked in the back. It’s a one time force over a specific part of a second.
I’m curious where impulse is mentioned in dyn4j as it never came up for me. Because physics engines are already dealing with time slices, force and impulse are often interchangeable. A force applied this frame and not the next was a tpf-length impulse.
On a body you can choose to ApplyForce or ApplyImpulse. The distinction is mentioned in the documentation, but I think (based on Dyn4j’s) that you are right about what impulse and what force is:
Applying Forces, Torques, & Impulses
When applying forces and torques to a body, they are queued in an accumulator and not immediately applied. When a simulation step is performed, the stored forces and torques are applied to the bodies and placed in the force and torque member variables. This is done so that the previous simulation step’s force and torque values can be queried via the getForce and getTorque methods.
The force and torque classes also feature an isComplete method that can be overridden by sub classes. This allows a force to be created that may or may not be removed at the end of a world step given the return of the isComplete method.
Applying forces or torques to a body will immediately awaken the body if it was asleep.
Impulses are applied to bodies immediately (not stored in an accumulator). Applying an impulse will immediately awaken the body as well.
Forces accumulate (can be from different angles, on different coordinates, with different acceleration) over a given timestep and the sum of the forces are then applied in the end.
Impulse is applied immediately in the given timestep, no matter any accumulated force or other impulses that will be or have been applied in the same given timestep.
Well actually Force = m * a (so the mass-dependent acceleration), whereas Impulse = m * v or Integral(m * a).
That is also true. While you are dragged you experience a force which increases your impulse each frame. But as soon as you let go, you are full of impulse but the force is none (apart from air resistance and thelike).
The Kick: It’s a force for a short slice of time and then you are full of impulse. I guess it’s hard to explain force/impulse since they are nearly the same with the only difference being the time.
Technically I would always set the impulse (speed), because otherwise you might need to set the force each frame or the force would have to be nearly infinite. For example when you have a collision, you’d have a huge force for like one second. Or you could set the impulse of say 20m/s for x kg.
I used pspeeds simeth-es tutorial and successfully integrated dyn4j with no issues. I found it fairly straight forward.
Having some sort of character control I found a bit more challenging and followed williams (dyn4j authors) advice on how to setup a motorjoint for controlling my tank.
This is my understanding also. In Dyn4J forces are accumulated and then summed and applied when the world steps.
Impulses are applied to the velocity immediatly.
This is from the Dyn4J JavaDoc:
“NOTE: Applying an impulse differs from applying a force and/or torque. Forces and torques are stored in accumulators, but impulses are applied to the velocities of the body immediately.”
Another way to say it is that in Dyn4J using impulse is a way of overriding the physics simulation and abruptly changing the velocity instead of letting the simulation calculate what will happen to a body given all the forces acting upon it.
I just added world.update to simphysics, added world.add and remove to the functions that add/remove bodies. And run a ‘syncronizebody’ loop that runs to update locations between physics and spatials.
as far as using dyn4j goes, I just run a controller joined to a body using a motor joint, when the controller moves the body attached to it tries to follow but collides with anything that its set to collide with.
So if I understand this, you didn’t integrate between Dyn4j and Zay-ES, you integrated the Scene spatials (directly) with the Dyn4j bodies data? Could you elaborate on how (which data did you push to the Dyn4j and which did you pull from the Dyn4j).
Edit: Removed snippet because I changed the implementation.