/// <summary> /// Performs translation to target /// </summary> /// <param name="gameTime">Game time</param> /// <param name="newInterest">New interest</param> /// <param name="radius">Radius to decelerate</param> /// <param name="slow">Slow</param> private void UpdateTranslations(GameTime gameTime) { if (this.translationMode != CameraTranslations.None) { Vector3 diff = this.translationInterest - this.Interest; Vector3 pos = this.Position + diff; Vector3 dir = pos - this.Position; float distanceToTarget = dir.Length(); float distanceThisMove = 0f; if (this.translationMode == CameraTranslations.UseDelta) { distanceThisMove = this.MovementDelta * gameTime.ElapsedSeconds; } else if (this.translationMode == CameraTranslations.UseSlowDelta) { distanceThisMove = this.SlowMovementDelta * gameTime.ElapsedSeconds; } else if (this.translationMode == CameraTranslations.Quick) { distanceThisMove = distanceToTarget * this.translationOutOfRadius; } Vector3 movingVector; if (distanceThisMove >= distanceToTarget) { //This movement goes beyond the destination. movingVector = Vector3.Normalize(dir) * distanceToTarget * this.translationIntoRadius; } else if (distanceToTarget < this.translationRadius) { //Into slow radius movingVector = Vector3.Normalize(dir) * distanceThisMove * (distanceToTarget / this.translationRadius); } else { //On flight movingVector = Vector3.Normalize(dir) * distanceThisMove; } if (movingVector != Vector3.Zero) { this.Position += movingVector; this.Interest += movingVector; } if (Vector3.NearEqual(this.Interest, this.translationInterest, new Vector3(0.1f, 0.1f, 0.1f))) { this.StopTranslations(); } } }
/// <summary> /// Rotate to target /// </summary> /// <param name="target">Target</param> /// <param name="up">Up vector</param> /// <param name="yAxisOnly">Rotate Y axis only</param> /// <param name="interpolationAmount">Interpolation amount for linear interpolation</param> /// <param name="updateState">Update internal state</param> public void RotateTo(Vector3 target, Vector3 up, bool yAxisOnly = true, float interpolationAmount = 0, bool updateState = false) { if (this.Parent != null) { //Set parameters to local space var parentTransform = Matrix.Invert(this.Parent.FinalTransform); target = Vector3.TransformCoordinate(target, parentTransform); } if (!Vector3.NearEqual(this.position, target, new Vector3(MathUtil.ZeroTolerance))) { var newRotation = Helper.LookAt(this.position, target, up, yAxisOnly); newRotation = Helper.RotateTowards(this.rotation, newRotation, interpolationAmount); this.SetRotation(newRotation, updateState); } }