Skip to main content
gamedev.net
🔒 Locked

question !?

Started by SyIH 2 replies 350+ views
Original Post
SyIH
SyIH
Hi to all, i have here a very noob question, plz hrlp me! some code: struct CVector3 { public: CVector3() {} CVector3(float X, float Y, float Z) { x = X; y = Y; z = Z; } CVector3 operator+(CVector3 vVector) { return CVector3(vVector.x + x, vVector.y + y, vVector.z + z); } CVector3 operator-(CVector3 vVector) { return CVector3(x - vVector.x, y - vVector.y, z - vVector.z); } CVector3 operator+=(CVector3 vVector) { return CVector3(x += vVector.x, y += vVector.y, z += vVector.z); } CVector3 operator=(CVector3 vVector) { return CVector3(x = vVector.x, y = vVector.y, z = vVector.z); } CVector3 operator*(float num) { return CVector3(x * num, y * num, z * num); } CVector3 operator/(float num) { return CVector3(x / num, y / num, z / num); } float x, y, z; }; // some where in my code player.cpp CVector3 AVT=CVector(30,0,0); CVector3 BVT=CVector(0,0,0); //somewhere in main.h extern CVector3 AVT; extern CVector3 BVT; //somewhere in WINAPI (in while) main.cpp AVT = NRotate(BVT, AVT, 12, 0, 1, 0); that's means that it shuld give a new AVT every frame, but it dois it only once and give me the same avt again and again and again .arggg PLZ HELP ME [edited by - SyIH on March 1, 2004 9:10:49 AM]
Evil Steve
Evil Steve
Unless NRotate modifys the variables BVT and AVT, it should give you the same result every call, since nothing has changed since the last call.
Qw3r7yU10p!
Qw3r7yU10p!
your assignment operators are completely wrong.

haven''t time to show you how but someone else will help if you need it.

operator= should modify the member variables as should all the others too.
rypyr
rypyr
Basically you need to learn two C++ concepts: 1) "this" and 2) references. Do a search for these two topics related to C++ on google.


CVector3& CVector3::operator=(const CVector3& o) {
if(&o != this) {
this->x = o.x;
this->y = o.y;
this->z = o.z;
}
return *this;
}

CVector3& CVector3::operator+=(const CVector3& o) {
this->x += o.x;
this->y += o.y;
this->z += o.z;
return *this;
}



And your add/sub functions should use references, otherwise you''re making copies of CVector3 objects all over the place.


CVector3 CVector3::operator+(const CVector3& o) {
return CVector3(this->x + o.x, this->y + o.y, this->z + o.z);
}



[ CodeDread ]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.