public void Update(float desireDistance, float delatTime) { float diff = desireDistance - _currentDistance; if (diff == 0.0f) { } else if (diff > 0.0f) { float newSpd = _currentSpeed + _accelerate * delatTime; float maxSpd = ViMathDefine.Sqrt(diff * _accelerate * 2.0f); _currentSpeed = ViMathDefine.Min(ViMathDefine.Min(newSpd, maxSpd), _maxSpeed); float delatDist = _currentSpeed * delatTime; _currentDistance += delatDist; if (_currentDistance >= desireDistance) { _currentDistance = desireDistance; _currentSpeed = 0.0f; } } else { float newSpd = _currentSpeed - _accelerate * delatTime; float maxSpd = -ViMathDefine.Sqrt(-diff * _accelerate * 2.0f); _currentSpeed = ViMathDefine.Max(ViMathDefine.Max(newSpd, maxSpd), -_maxSpeed); float delatDist = _currentSpeed * delatTime; _currentDistance += delatDist; if (_currentDistance <= desireDistance) { _currentDistance = desireDistance; _currentSpeed = 0.0f; } } }
public bool Update(float deltaTime, float newDir) { float deltaAngle = newDir - _curDiretion; if (deltaAngle == 0.0f) { return(false); } ViAngle.Normalize(ref deltaAngle); float maxRot = 0.0f; if (deltaAngle > 0.0f) { _currentSpeed = Math.Abs(_currentSpeed); float newSpd = _currentSpeed + _accelerate * deltaTime; float maxSpd = ViMathDefine.Sqrt(deltaAngle * _accelerate * 2.0f); _currentSpeed = ViMathDefine.Min(ViMathDefine.Min(newSpd, maxSpd), _maxSpeed); maxRot = _currentSpeed * deltaTime; } else { _currentSpeed = -Math.Abs(_currentSpeed); float newSpd = _currentSpeed - _accelerate * deltaTime; float maxSpd = -ViMathDefine.Sqrt(-deltaAngle * _accelerate * 2.0f); _currentSpeed = ViMathDefine.Max(ViMathDefine.Max(newSpd, maxSpd), -_maxSpeed); maxRot = _currentSpeed * deltaTime; } if (Math.Abs(deltaAngle) < Math.Abs(maxRot)) { _curDiretion = newDir; _currentSpeed = 0.0f; } else { _curDiretion += maxRot; } ViAngle.Normalize(ref _curDiretion); return(true); }
public static ViVector3 Min(ViVector3 lhs, ViVector3 rhs) { return(new ViVector3(ViMathDefine.Min(lhs.x, rhs.x), ViMathDefine.Min(lhs.y, rhs.y), ViMathDefine.Min(lhs.z, rhs.z))); }