# Position and Rotation calculation algorithm in UrhoSharp

Member ✭✭

I am currently working in a WPF C# based 3D modelling application using UrhoSharp. I have a requirement where I need to implement movement of a node over time based on position and rotation values calculated using an algorithm. The parameters that are being sent to the algorithm(these are to be set from the UI) as input are:

float timeInAnimation: calculated from the scene updates received in the OnUpdate event handler of the component
float animationDuration: the total duration for the animation
float distance: the total distance for the animation
Quaternion to: the final rotation for the node
Quaternion from: the starting rotation for the node
Quaternion direction: the direction in which the node should move
Quaternion defaultDirection: the initial direction of the node in the scene model
Vector3 startPosition: the start position of the node

The intent is basically to simulate an object movement over a fixed time duration from a starting position and direction over a defined distance and in a defined direction along with a starting and final rotation.

The algorithm has been defined as:

public void Calculate(
float timeInAnimation, float animationDuration, float distance, Quaternion to, Quaternion from, Quaternion direction, Quaternion defaultDirection, Vector3 startPosition,
out Vector3 positionToSet, out Quaternion rotationToSet) {
var fractionOfAnimation = timeInAnimation / animationDuration;

var currentDirection = defaultDirection * direction;
var finalPosition = currentDirection * Vector3.Forward * distance;

positionToSet = Vector3.Lerp(startPosition, finalPosition, fractionOfAnimation);

if (Quaternion.Equals(from, to))
rotationToSet = from;
else
rotationToSet = Quaternion.Slerp(from, to, fractionOfAnimation);
}

Can somebody please validate the algortihm and comment on its effectiveness.

I have also been creating a set of xUnit unit tests to validate the alogorithm but it is failing with the following input where I am not sure whether it is the fault of the algorithm or it is the expected output data that has been wrongly specified:

new SimpleMovementTestData(1f, 4f, 4f, Quaternion.Identity, Quaternion.Identity, new Quaternion(+00f, +180f, +00f), Quaternion.Identity, Vector3.Zero, new Vector3(0f, 0f, -1f), Quaternion.Identity) };

public void AnimateUsingSimpleMovement_Test_Position(SimpleMovementTestData testData) {

var algorithm = new SimpleMovementAlgorithm();

algorithm.Calculate(testData.TimeInAnimation, testData.AnimationDuration, testData.Distance, testData.ToRotation, testData.FromRotation, testData.Direction, testData.DefaultDirection, testData.StartPosition, out Vector3 position, out Quaternion rotation);

Assert.Equal(testData.ExpectedPosition.X, position.X, FloatPrecision);
Assert.Equal(testData.ExpectedPosition.Y, position.Y, FloatPrecision);
Assert.Equal(testData.ExpectedPosition.Z, position.Z, FloatPrecision);
}

Can anybody find the fault in this implementation and help me to understand the flaws in the algorithm/test data?