Verlet integration explodes my physics engine

I create a physical engine, and I had some kind of "pseudo-verlet" thing, and I wanted to update it to a "real" verlet. So I found an article and decided to work. After I added what, in my opinion, is a good approximation, the engine no longer works. Can someone help me understand what I'm doing wrong?

My renewal of the physical class of the physical body, the use of force and the use of impulse systems:

public void Update(float timestepLength)
{
    if (!this._isStatic)
    {
        Vector2 velocity =  Vector2.Subtract(_position, _lastPosition);
        Vector2 nextPos = _position + (_position - _lastPosition) + _acceleration * (timestepLength * timestepLength);
        _lastPosition = _position;
        _position = nextPos;
        _acceleration = Vector2.Zero;
    }
}

public void ApplyForce(Vector2 accelerationValue)
{
    if (!this._isStatic)
        _acceleration += (accelerationValue) * _mass;
}
public void ApplyImpulse(Vector2 impulse)
{
    if (!this._isStatic)
        _acceleration +=-1 * impulse;
}

Edit: I fixed it and it works like a charm, but I have two questions about the following code:

  • Is the impulse application code applied correctly, and if it is not?
  • How to change the position property so that the setting retains the current speed of the body?

Here is the code:

public Vector2 Position
{
    get { return _position; }
    set { _position = value;}
}
public void Update(float timestepLength)
{
    if (!this._isStatic)
    {
        Vector2 velocity =  Vector2.Subtract(_position, _lastPosition);
        Vector2 velocityChange = Vector2.Subtract(velocity, Vector2.Subtract(_lastPosition, _twoStepsAgoPosition));
        Vector2 nextPos = _position + (_position - _lastPosition) + _acceleration * (timestepLength * timestepLength);
        _twoStepsAgoPosition = _lastPosition;
        _lastPosition = _position;
        _position = nextPos;
        _acceleration = Vector2.Multiply(velocityChange, timestepLength);
    }
}

public void ApplyForce(Vector2 force)
{
    if (!this._isStatic)
        _lastPosition -= force;
}
public void ApplyImpulse(Vector2 impulse)
{
    if (!this._isStatic)
        _acceleration +=-1 * impulse;
}
+5
1

... , , , , : , , hitman , ragdoll.

... , , :

 void ParticleSystem::Verlet() {
       for(int i=0; i<NUM_PARTICLES; i++) {
             Vector3& x = m_x[i];
             Vector3 temp = x;
             Vector3& oldx = m_oldx[i];
             Vector3& a = m_a[i];
             x += x-oldx+a*fTimeStep*fTimeStep;
             oldx = temp;
       }
 }

, .

, , . , , timestep, , . (, 30 / (, timestep 1/30)) . , ,

EDIT:

2: ( /), , ( newPosition-oldPosition) oldposs, .

1: - , . . , X ( ) applyForce .

+3

All Articles