Using the new XMVECTOR and XMFLOAT3 classes, what's the best way to get the distance between two points? I could not find a function that does this in the XMVector * family of functions, so I came up with the following:
float distance(const XMFLOAT3& v1,const XMFLOAT3& v2)
{
XMVECTOR vector1 = XMLoadFloat3(&v1);
XMVECTOR vector2 = XMLoadFloat3(&v2);
XMVECTOR vectorSub = XMVectorSubtract(vector1,vector2);
XMVECTOR length = XMVector3Length(vectorSub);
float distance = 0.0f;
XMStoreFloat(&distance,length);
return distance;
}
Would it be faster than the regular Vector3 class with 3 floats for x, y, z, and then using sqrt, because it uses built-in optimizations? Namely:
float Distance(const Vector3& point1,const Vector3& point2)
{
float distance = sqrt( (point1.x - point2.x) * (point1.x - point2.x) +
(point1.y - point2.y) * (point1.y - point2.y) +
(point1.z - point2.z) * (point1.z - point2.z) );
return distance;
}
source
share