# [Solved] Dyn4j physics integration, and Zay-ES

Hi

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:

1. The jMonkey `Scene` where objects are visualized
2. The Dyn4j `World`-object where physical bodies and their interactions are handled (by Dyn4j)

?

Kind regards

1 Like

Yes, and you need some Controls to connect objects in the two worlds.

2 Likes

I am not planning to use controls, but I will integrate the data between the bodies and the spatials.

1 Like

I’m trying to grasp the different between Force and Impulse when applying my movement to the physical bodies. Anybody know a good analogy?

1 Like

The difference is Time. Just like Speed is to Distance.

Actually Force is Acceleration and Impulse is Velocity.

Force is when i kick you in your back and Impulse is when you are on your skateboard being dragged by a car and then Let go

2 Likes

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.

1 Like

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.

1 Like

Actually now that I re-read it,

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.

1 Like

Yeah, I read it as the difference being where/when the values are added.

My guess is that forces are added in with acceleration where as impulses affect velocity directly.

1 Like

My basic flow in my AbstractGameSystem (service on EntityData set)

1. Update listeners with begin time frame
2. Update entity list
3. Update listeners with changes to entity list
4. Send new forces from jMonkeyEngine scene to Dyn4j world (find the right components with velocities that should be applied etc.)
5. Update Dyn4j world (handled inside Dyn4j)
6. Fetch changes from Dyn4j world to the jMonkeyEngine scene (find changes to Dyn4j bodies and apply them to entity components)
7. Update listeners with changes to the physical bodies
8. Update listeners with end time frame

1 Like

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.

1 Like

So continuous impulse = force… ?

Thanks for helping me understand! I’ll was planning on setting impulse, so I’ll continue and see how it goes.

1 Like

No:
Impulse = Integral of Force → m * v = Integral(m * a) = m * a * t
Or: Force = d Impulse / dt → Force = m * v / t = m * a

So impulse is the accumulated force, or force is how the impulse will change

4 Likes

Thank you very much sir

2 Likes

Hello @asser_fahrenholz,

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.

Mithrin

1 Like

How did you integrate between the ES and Dyn4j? I did a schematic above - did you do anything similar?

I have no controls, so will only integrate data between the Dyn4j bodies and the entities and their components.

1 Like

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.”

http://forum.dyn4j.org/viewtopic.php?f=1&t=327#p1158

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.

1 Like

So if I’m looking for a way to simulate ‘real’ physics, I’d use forces. If I want to simplify and override, I’d use impulses

1 Like

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.

Mithrin

1 Like

What is simphysics?

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.

1 Like