Discuss any questions about BEPUphysics or problems encountered.
b1fg2
Posts: 16
Joined: Mon Sep 24, 2018 3:36 pm

how to create unity like funtion with Beppu v2.
in the force mode there is "force" , "Acceleration" ,"Impulse" ,"VelocityChange" .But bepuv2 only have applyImulse function, how can i create new funtion?
https://docs.unity3d.com/ScriptReferenc ... Force.html

Norbo
Posts: 4884
Joined: Tue Jul 04, 2006 4:45 am

Re: V2 unity like addforce funtion

A brief summary of how all these relate to each other:
-A "force" is a change in momentum over time.
-An "impulse" is an instant change in momentum.
-The physics engine only tracks position and velocity, not momentum, so impulses- changes in momentum- are implemented as instant changes in velocity by transforming the impulse by the inverse mass/inertia. Likewise, you can go from a change in linear velocity to an equivalent impulse applied at the center of a body by just scaling by mass.
-If you want to change the velocity directly, you can modify the BodyReference.Velocity.
-If you want to change velocity over time (that is, an acceleration), you can apply a small change every frame. So if you want to accelerate by 1 unit per second and your simulation is stepping at 60 times per second, you can increase the velocity by 1/60 every timestep.
-Likewise, since 'force' and 'acceleration' are the same thing (separated only by an inertia transform- think F = M * A) you can create a force by applying impulses (or their equivalent velocity changes) every timestep.

b1fg2
Posts: 16
Joined: Mon Sep 24, 2018 3:36 pm

Re: V2 unity like addforce funtion

Thanks for the reply, i hope this could help with other that looking for similar staff.
Here is the function, correct me if i am wrong.

Code: Select all

``````public void AddImpulse(System.Numerics.Vector3 force)
{
bodyRef.ApplyImpulse(force, default);
}

{
bodyRef.Velocity.Linear = force * bodyRef.LocalInertia.InverseMass;
}

{
bodyRef.Velocity.Linear = force;
}
public void AddVelocityChange(System.Numerics.Vector3 force, float dt)
{
bodyRef.Velocity.Linear = force * dt;
}``````

Norbo
Posts: 4884
Joined: Tue Jul 04, 2006 4:45 am

Re: V2 unity like addforce funtion

A few things:
1) A "force" is over time, so an AddForce function that directly modifies velocity once and does nothing else can't really be a force.
2) Same with regards to AddAcceleration.
3) Setting the linear velocity directly to a change in velocity won't do what you want; you have to add it to the existing velocity.

b1fg2
Posts: 16
Joined: Mon Sep 24, 2018 3:36 pm

Re: V2 unity like addforce funtion

like this?
i got some infor from here

Code: Select all

``````public void AddImpulse(System.Numerics.Vector3 force) // Force per second
{
bodyRef.Velocity.Linear += force * bodyRef.LocalInertia.InverseMass;
}

public void AddForce(System.Numerics.Vector3 force, float dt) // Force per frame
{
bodyRef.Velocity.Linear = (force * bodyRef.LocalInertia.InverseMass) * dt;
}

{
bodyRef.Velocity.Linear = force;
}
public void AddVelocityChange(System.Numerics.Vector3 force, float dt)
{
bodyRef.Velocity.Linear = force * dt;
}``````

Norbo
Posts: 4884
Joined: Tue Jul 04, 2006 4:45 am

Re: V2 unity like addforce funtion

AddImpulse's comment says 'force per second', but an impulse is instantaneous. Functionally, though, without that comment and assuming the variable was named impulse, that works.

AddForce's 'force per frame' is also misleading, and directly setting the linear velocity doesn't work- it needs to add to it.

AddAcceleration doesn't make sense- a force must be scaled to be an acceleration, an acceleration is over time, and setting the linear velocity directly to the acceleration doesn't work.

I'm not sure what the intent of AddVelocityChange is, but it's currently broken. If you just want to change the velocity, you can just do body.Velocity.Linear += velocityChange.

lidgren
Posts: 21
Joined: Mon May 27, 2019 6:28 pm

Re: V2 unity like addforce funtion

What is the "correct" way to do these things? I've been using OneBodyLinearMotor to apply constant force; but for instant impulses it's a bit cumbersome (add, step, remove). Also; I'm a bit curious how the OneBodyLinearMotor actually works... it applies how much force, in what direction? Will TargetVelocity 0,0,0 actively slow the body down if it's moving for example? The code is right there to read but things like manifolds, jacobians and the vectorization makes it... challenging

Norbo
Posts: 4884
Joined: Tue Jul 04, 2006 4:45 am

Re: V2 unity like addforce funtion

What is the "correct" way to do these things? I've been using OneBodyLinearMotor to apply constant force; but for instant impulses it's a bit cumbersome (add, step, remove).
If you just need to apply a continuous acceleration on a target body without tight stability requirements, the simplest approach is just bodyReference.Velocity.Linear += acceleration * dt. I usually find it most intuitive to work at the velocity level so I rarely work with 'forces', but that would only require scaling the force by inverse mass to get the equivalent acceleration.

If that sort of thing is happening to most bodies, like gravity, it can make sense to include it in the IPoseIntegratorCallbacks.IntegrateVelocity callback.

Constraints like the OneBodyLinearMotor come in when you need the goal to interact robustly with other constraints, like collisions or joints. They're usually overkill when the forces/accelerations involved are low frequency. Gravity, for example, is typically implemented just as a velocity increment each frame. Wind blowing leaves around would work fine with direct velocity increments.

If instead you have a heavy duty mechanism that's trying to shove some other heavy object out of the way in a highly responsive way, you'd probably need constraint-based control.
Also; I'm a bit curious how the OneBodyLinearMotor actually works... it applies how much force, in what direction? Will TargetVelocity 0,0,0 actively slow the body down if it's moving for example? The code is right there to read but things like manifolds, jacobians and the vectorization makes it... challenging
A little bit

OneBodyLinearMotor in particular is a velocity motor that acts on three linear degrees of freedom. It applies force in any direction required to reduce the error between the measured velocity and the target velocity. So yes, targeting (0,0,0) will slow the body down. The magnitude of the force depends on the MotorSettings; it has a Damping/Softness value (they are just inverses of each other) and a MaximumForce. The constraint cannot apply more than MaximumForce, and the amount of force it'll try to apply (before clamping) is based on the damping/softness. The softer a motor is (or, equivalently, the lower the damping is), the looser the velocity motor is.

Other joints and 'servos' are similar, but use a damping ratio and frequency and have a position target rather than a velocity target.

lidgren
Posts: 21
Joined: Mon May 27, 2019 6:28 pm

Re: V2 unity like addforce funtion

Ah, thanks for explaining! I guess I was overcomplicating the problem.