This topic is 1112 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello I'm going to be quick here. I don't now were I should post this since I sort of think it fits in in general programming, game programming and Directx and Xna. I'm working on my own vector class. And I want to be able to use it with the DirectX::XMVECTOR. So I created an assignment operator overloading function that looks like this:

Vector3& Vector3::operator=(const DirectX::XMVECTOR& vector)
{
this->X = DirectX::XMVectorGetX(vector);
this->Y = DirectX::XMVectorGetY(vector);
this->Z = DirectX::XMVectorGetZ(vector);

return *this;
}


I then tested it out with following code:

Vector3 vec = Vector3(10, 10, 10);
DirectX::XMVECTOR vector = DirectX::XMVectorSet(5, 5, 5, 5);

vec = vector;

OutputDebugStringA((std::to_string(vec.X) + '\n').c_str());

OUTPUT:

5


Ok so I get the correct output here, the thing is that now none of my geometry is showing up. An output of the camera lookUp vector (just tested with a random vector in my camera class) shows that it's increasing in value every frame.

It all worked when I had this code in my assignment function:

Vector3 Vector3::operator=(const DirectX::XMVECTOR& vector)
{
return Vector3(DirectX::XMVectorGetX(vector), DirectX::XMVectorGetY(vector), DirectX::XMVectorGetZ(vector));
}


But now I get this output:

10


As you see, wrong output, but the geometry shows up, and my lookAt vector gets the correct output

Neither did it work when I had this as my code in my assignment function:

Vector3 Vector3::operator=(const DirectX::XMVECTOR& vector)
{
this->X = DirectX::XMVectorGetX(vector);
this->Y = DirectX::XMVectorGetY(vector);
this->Z = DirectX::XMVectorGetZ(vector);

return *this;
}


With this code I get the same output in the console:

5


But No geometry showing up. I'm I not returning the same thing in the functions that doesn't return a reference?

This doesn't make any sense to me. The only reason to why I noticed this is because I read that you should always return the reference when you overload the assignment operator, so that you can do this:

Vector3 a, b, c;

a = b = c;


English isn't my first language so there cn be some miscommunication (spelling?) going on. I would love it if someone could be kind to explain what i'm acually doing when i'm returning an reference. I get sort of confused since when you do this:

Vector3 vec = Vector3(3, 5, 7);
Vector3* vecPtr = &vec;


I assign the adress of vec to vecPtr, but since none of a, b or c is an pointer I get confused and doesn't really have an idea of whats happening. And is it correct to say that in this code:

void func(int& number)
{
number = 5;
}

int main()
{
int variable = 0;
func(variable);

return 0;
} 

i'm changing the value of "variable" since I'm "sending" it into the function func()? Otherwise I have got it all wrong.

I thought that I show you my camera code to if someone should ask for it, maybe it's there I've f*cked up.

void Camera::render(Vector3 objectWorldPosition, Vector3 objectRotation, Vector3 objectScale, DirectX::XMFLOAT4X4& outputWorldMatrix)
{
//create and set matrices
DirectX::XMMATRIX worldMatrix;
DirectX::XMMATRIX objectRotationMatrix = DirectX::XMMatrixRotationRollPitchYawFromVector(objectRotation);
DirectX::XMMATRIX objectScalingMatrix = DirectX::XMMatrixScalingFromVector(objectScale);
DirectX::XMMATRIX objectWorldPositionMatrix = DirectX::XMMatrixTranslationFromVector(objectWorldPosition);
DirectX::XMMATRIX cameraRotationMatrix = DirectX::XMMatrixRotationRollPitchYawFromVector(this->rotation);

//OutputDebugStringA((std::to_string(this->lookAt.X) + " ," + std::to_string(this->lookAt.Y) + " ," + std::to_string(this->lookAt.Z)).c_str());

//calculate new lookAt and up- vectors with camera rotation
this->lookAt = DirectX::XMVector3TransformCoord(this->lookAt, cameraRotationMatrix);
this->up = DirectX::XMVector3TransformCoord(this->up, cameraRotationMatrix);

//create worldmatrix
worldMatrix = DirectX::XMMatrixMultiply(objectRotationMatrix, objectScalingMatrix);
worldMatrix = DirectX::XMMatrixMultiply(worldMatrix, objectWorldPositionMatrix);

//create matrices
DirectX::XMStoreFloat4x4(&this->viewMatrix, DirectX::XMMatrixLookToLH(this->position, this->lookAt, this->up));

//transpose the matrices before sending them to the vertex shader
worldMatrix = DirectX::XMMatrixTranspose(worldMatrix);

//output world matrix
DirectX::XMStoreFloat4x4(&outputWorldMatrix, worldMatrix);
} 

##### Share on other sites

I don't know what the exact problem is but this:

Vector3& Vector3::operator=(const DirectX::XMVECTOR& vector)
{
this->X = DirectX::XMVectorGetX(vector);
this->Y = DirectX::XMVectorGetY(vector);
this->Z = DirectX::XMVectorGetZ(vector);

return *this;
}

Looks correct so stick with it.

This one:

Vector3 Vector3::operator=(const DirectX::XMVECTOR& vector)
{
return Vector3(DirectX::XMVectorGetX(vector), DirectX::XMVectorGetY(vector), DirectX::XMVectorGetZ(vector));
}

Doesn't actually do anything to the vector.

You say your lookUp vector increases each time? Did you mean lookAt (I see that in your code) or do you mean this->up? It might be useful to see your copy constructor for the vector class if you have one (e.g Vector(const Vector&)) as that may be doing something odd when you are passing by value into that Camera::render method.

are this->up and this->lookAt your own Vector type?

i'm changing the value of "variable" since I'm "sending" it into the function func()? Otherwise I have got it all wrong

More or less, yes.

Edited by Nanoha

##### Share on other sites

Hehe.. oops, lookUp should be lookAt, and yes this->lookAt is my own Vector3. I haven't made an copy constructor so it's the default one. Yeah, I suspected that this function didn't do anything with the vector:

Vector3 Vector3::operator=(const DirectX::XMVECTOR& vector)
{
return Vector3(DirectX::XMVectorGetX(vector), DirectX::XMVectorGetY(vector), DirectX::XMVectorGetZ(vector));
}


But since it worked I thought it was very weird. I had it there since it were a while ago since I worked with the project (a month I think) and I have now learnt a bit more about operator overloading.

##### Share on other sites

You could check perhaps whether it gets actually called.

The compiler makes an assignment operator method for you if you don't supply one.

Maybe you used the generated method rather than your own?

##### Share on other sites

Note that the signature of the copy assignment operator is Vector3& Vector3::operator=(const DirectX::XMVECTOR& vector)which returns a reference to this.  All the other functions you have with the signature Vector3 Vector3::operator=(const DirectX::XMVECTOR& vector) are not really copy assignment functions because they invoke the copy constructor and return a copy of this.

If I make assumptions about all the Vector3 code you didn't post that could be incorrect, the first version of your copy assignment operator looks like the only correct one, and if you're having problems elsewhere I suspect the cause of the problem is elsewhere.

##### Share on other sites
I have checked it gets called. The differentierad functions are different versions of the same function. Different versions to see what the outcome were.

I'm gonna look into it abit more

##### Share on other sites

this->lookAt = DirectX::XMVectorAdd(this->position, this->lookAt);


It looks like you're calling this every frame, without resetting the lookAt vector in-between, so the lookAt vector should be increasing every frame with a correct assignment operator.

##### Share on other sites
this->lookAt = DirectX::XMVectorAdd(this->position, this->lookAt);


It looks like you're calling this every frame, without resetting the lookAt vector in-between, so the lookAt vector should be increasing every frame with a correct assignment operator.

Thank you for noticing. That must be the problem, gonna test and report back

Edited by Mumsfilibaba

1. 1
2. 2
Rutin
20
3. 3
khawk
16
4. 4
A4L
14
5. 5

• 11
• 16
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633755
• Total Posts
3013706
×