Quote:Original post by johnnyBravo
Oh and also the D3DXVECTOR3 structure can do things like this:
D3DXVECTOR3 abc(1,2,3);
D3DXVECTOR3 v = 5.0f + abc;
Where as mine can't do that, but only can only do:
v = abc + 5.0f;
I don't get what I can do to be able to do that.
First of all, are you sure it is an addition? You probably meant multiplication.
Think about it like this. When you are making your operator that takes a float and scales your vector, you probably have something similar to this:
Vector3 Vector3::operator * (float s){ return Vector3(x*s, y*s, z*s); }
This is equivelant to a global function like this:
Vector3 MultiplyVector3ByScalar(const Vector3& lhs, float rhs){ return Vector3(lhs.x*rhs, lhs.y*rhs, lhs.z*rhs); }
This means that you can only multiply the vector by a scalar to the right of it.
In order to multiply it by a scalar to the left, you need to make a global operator, like so:
class Vector3 {... friend Vector3 operator * (float lhs, const Vector3& rhs); float x, y, z;}inline Vector3 operator * (float lhs, const Vector3& rhs){ return Vector3(rhs.x*lhs, rhs.y*lhs, rhs.z*lhs); }
Now, you can multiply by a scalar to the left and the right.
Vector3 test1(3.0f, 4.0f, 5.0f);Vector3 test2 = test1 * 3.0f; // Multiply by a scalar to the righttest1 = -3.0f * test2; // Multiply by a scalar to the left
Hope I wasn't beaten to the reply ;).
[EDIT] Forgot the inline keyword and added a little more to the explanation. And, I guess I wasn't beaten to the reply.
Slaru