Advertisement

I cant initialise my Camera

Started by March 09, 2002 07:27 AM
2 comments, last by csxpcm 22 years, 11 months ago
Dear all, im having problems setting up my camera. I call the constructor but then when I want the results back the values aren''t the same. for instance: In main I have: (this has been edited so its easier to read) ######################### Main.cpp ############################ CCamera gCamera; bool init(void) { gCamera.PositionCamera(0, 0.3, 6, 0, 0.3, 0, 0, 1, 0); //################################################### char temp[10]; sprintf(temp,"%f",gCamera.mView.getx()); MessageBox(NULL, temp, "Title", MB_OK); //################################################### return 1; } But the message box always returns -107374176.000000 and not the value it was just set at. It shoudld return "0.3"!! Should i be using pointer? is this where im going wrong? Any suggestions would be much appreciated!! Thanks in advance! ##################### Vector.h: ########################## #include <math.h> // Here is our vector class class CVector { public: CVector() {v[0] = v[1] = v[2] = 0;} CVector(float px, float py, float pz) {v[0]=px; v[1] = py; v[2] = pz;} CVector(const CVector &pVec) {v[0]=pVec.v[0]; v[1] = pVec.v[1]; v[2] = pVec.v[2];} CVector(const float *pVec) {v[0]=pVec[0]; v[1] = pVec[1]; v[2] = pVec[2]; } virtual ~CVector() {} float getx(void) {return x;} float gety(void) {return y;} float getz(void) {return z;} float v[3]; float x, y, z; // We just want a float for a X Y and Z. }; ##################### CCamera.h: ########################## class CCamera { public: void rotateView(float X, float Y, float Z); void moveCamera(float speed); CCamera(); virtual ~CCamera(); void PositionCamera(float positionX, float positionY, float positionZ, float viewX, float viewY, float viewZ, float upVectorX, float upVectorY, float upVectorZ); CVector mPosition; CVector mView; CVector mUpVector; }; #endif // !defined(AFX_CAMERA_H__85F3F9D0_95F1_4B3B_BD22_4023DC27B429__INCLUDED_)
quote:
Original post by csxpcm
// Here is our vector classclass CVector {public:  // stuff omitted  float v[3];  float x, y, z;	// We just want a float for a X Y and Z.}; 



You would most likely want to put those in a union to make x occupy the same memory location as v[0], y being the same as v[1] and z the same as v[2]. As you have it now you initialize only the vector ''v'', and then in ''getx'' you return the value of ''x'', which has never been initialized. Change it to something like the following:

  // Here is our vector classclass CVector {public:  // stuff omitted  union {    float v[3];    struct {      float x, y, z;	// We just want a float for a X Y and Z.    };  };};  

Advertisement

Firstly thanks for the suggestion earlier about using union! i didnt know about that! It works now! Thanks!
But i have this other problem I can''t work out. I have the following code:


################################################################
class CVector {
public:
CVector() {v[0] = v[1] = v[2] = 0;}
//other constructors and stuff!

// This WORKS
void Set(float x, float y, float z)
{v[0] = x; v[1] = y; v[2] = z ;}


// ********* This DOESNT WORK ***********
void Set(CVector &p)
{v[0] = p[0]; v[1] = p[1]; v[2] = p[2] ;}

union {
float v[3];
struct {
float x, y, z;
};
};

################################################################



My problem is that when I want to call "Set" with a Vector. for instance:

CVector position;
n->pos.Set(position);

I get the following error.

c:\program\camera.h(26) : error C2676: binary ''['' : ''class CVector'' does not define this operator or a conversion to a type acceptable to the predefined operator


I can''t figure out how pass a Vector! However I can set it using the other way (with 3 parameters - as shown)

Any suggestions would be again much appreciated!!
Thanks in advance,
- Pete
You need to overload the operator[] to access your own vectors in a way such as

Vector vec;

vec[0] = x; ect.....

You can do that in your vector class like so
  float& operator [](int a_iIdx){ return v[a_iIdx];}  

Those who dance are considered insane by those who cannot hear the music.

This topic is closed to new replies.

Advertisement