0
\$\begingroup\$

Since XMFLOAT2 is just a structure, I'm sure it does not have operator overloading which is what I need to make things a lot simpler. Is there something like XMFLOAT2 where I can add two (a) + (b). I also need to use the =.

Thanks

\$\endgroup\$
1
\$\begingroup\$

XMFLOAT2 doesn't provide the usual operator overloads. It does provide an operator= overload, but you'll have to write the others yourself. You can do this fairly easily, for example:

XMFLOAT2 operator+(const XMFLOAT2& left, const XMFLOAT& right) {
  return XMFLOAT2(left.x + right.x, left.y + right.y);
}

...and so on for any other operators you'd like.

Alternatively, if you'd like to use only the facilities provided by the DirectXMath API, store your XMFLOAT2 objects into XMVECTORs using XMStoreFloat2. XMVECTOR provides the suite of operators you are likely looking for.

\$\endgroup\$
1
\$\begingroup\$

DirectXMath generally tries to minimize use of "C++ magic" so that it's easier to write high-performance computation using it. For good SIMD performance, you want to be careful about when you read to or write from memory, minimize conversions between vector & scalar, and try to keep the bulk of your computation 'in-register'. For this reason, you are strongly encouraged by the design to use types like XMFLOAT2 in your memory data structures, but to actually do all computation using XMVECTOR which is best thought of as the proxy for a SIMD register.

That said, some of the rules of usage around alignment and the resulting verbosity can be less than friendly to the developer just trying to write some code.

For this reason, I've also written a 'wrapper' library for DirectXMath that includes types like Vector2 with all the overloads you are expecting. This uses C++ constructors, conversion operators, and extensive overloading to achieve what you are asking for. See the SimpleMath header which is part of the DirectX Tool Kit.

If you'd rather write your own wrapper or use the same technique in your own math library 'outer types', feel free to look at the source (h / cpp)

See also DirectXMath on GitHub

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.