简体   繁体   中英

DirectX::XMVECTOR function XMVectorSetByIndex() not setting float (C++)

I am having some trouble performing what seems like it should be a simple task in C++, where I am trying to change the values inside a vector (DirectX::XMVECTOR) using the command XMVectorSetByIndex().

In the code below, the command XMVectorGetByIndex() works fine, with new_y being set to 1.7 (after adding 0.2). However, XMVectorSetByIndex() does not lead to test becoming {0.0f, 1.7f, 2.0f, 0.0f} as I would expect (rather it remains unchanged).

    XMVECTOR test = { 0.0f, 1.5f, 2.0f, 0.0f };
    float new_y = XMVectorGetByIndex(test, 1) + 0.2;
    XMVectorSetByIndex(test, new_y, 1);

I've tried a few different things but had no luck getting the function to work. I just can't see what the issue is (especially given XMVectorGetByIndex() works with no issues.

Any help would be greatly appreciated :)

https://msdn.microsoft.com/en-us/library/hh404810(v=vs.85).aspx (XMVectorSetByIndex) https://msdn.microsoft.com/en-us/library/Hh404786(v=VS.85).aspx (XMVectorGetByIndex)

XMVectorSetByIndex function returns new vector with modified content, it does not modify input vector inplace. So you should overwrite test :

test = XMVectorSetByIndex(test, new_y, 1);

XMVectorSetByIndex does not modify the vector you pass in, it returns the new result instead.

XMVECTOR test = { 0.0f, 1.5f, 2.0f, 0.0f };
float new_y = XMVectorGetByIndex(test, 1) + 0.2;
test = XMVectorSetByIndex(test, new_y, 1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM